forked from remixz/umi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
837 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,40 @@ | ||
const http = require('http') | ||
const compress = require('compression')() | ||
const autocompleteHandler = require('./autocomplete') | ||
const openingHandler = require('./opening') | ||
const bifHandler = require('./bif') | ||
|
||
const srv = http.createServer((req, res) => { | ||
compress(req, res, () => { | ||
if (req.url.indexOf('/autocomplete') === 0) { | ||
autocompleteHandler(req, res) | ||
} else if (req.url.indexOf('/opening') === 0) { | ||
openingHandler(req, res) | ||
} else if (req.url.indexOf('/bif') === 0) { | ||
bifHandler(req, res) | ||
} else if (req.url.indexOf('/update') === 0) { | ||
updateHandler(req, res) | ||
} else { | ||
res.end('love arrow shoot!') | ||
} | ||
}) | ||
}) | ||
const express = require('express') | ||
const compression = require('compression') | ||
const cors = require('cors') | ||
|
||
const setupIo = require('./io') | ||
const autocomplete = require('./routes/autocomplete') | ||
const bif = require('./routes/bif') | ||
const opening = require('./routes/opening') | ||
const mal = require('./routes/mal') | ||
|
||
// create app | ||
const app = express() | ||
const srv = require('http').Server(app) | ||
app.use(compression()) | ||
app.use(cors()) | ||
|
||
// setup socket.io | ||
const io = require('socket.io')(srv) | ||
io.origins('*:*') | ||
setupIo(io) | ||
|
||
function findRoom (rooms) { | ||
return Object.keys(rooms).find((k) => k.startsWith('umi//')) | ||
} | ||
|
||
function updateHandler (req, res) { | ||
const token = req.url.split('/update/')[1] | ||
// setup routes | ||
app.get('/', (req, res) => res.send({status: 'ok'})) | ||
app.post('/update/:token', (req, res) => { | ||
const token = req.params.token | ||
if (token !== process.env.UPDATE_TOKEN) { | ||
return res.end('not ok') | ||
return res.end({status: 'not ok'}) | ||
} | ||
io.emit('app-update') | ||
res.end('ok') | ||
} | ||
|
||
io.on('connection', (socket) => { | ||
socket.on('join-room', (room) => { | ||
const currentRoom = findRoom(socket.rooms) | ||
if (currentRoom) return | ||
|
||
socket.join(room) | ||
socket.broadcast.to(room).emit('user-joined') | ||
const ioRoom = io.sockets.adapter.rooms[room] | ||
if (ioRoom) { | ||
socket.broadcast.to(room).emit('room-count', ioRoom.length) | ||
socket.emit('room-count', ioRoom.length) | ||
} | ||
}) | ||
|
||
socket.on('leave-room', () => { | ||
const room = findRoom(socket.rooms) | ||
socket.leave(room) | ||
socket.broadcast.to(room).emit('user-left') | ||
const ioRoom = io.sockets.adapter.rooms[room] | ||
if (ioRoom) { | ||
socket.broadcast.to(room).emit('room-count', ioRoom.length) | ||
} | ||
}) | ||
|
||
socket.once('disconnecting', () => { | ||
const room = findRoom(socket.rooms) | ||
if (!room) return | ||
|
||
socket.leave(room) | ||
socket.broadcast.to(room).emit('user-left') | ||
const ioRoom = io.sockets.adapter.rooms[room] | ||
if (ioRoom) { | ||
socket.broadcast.to(room).emit('room-count', ioRoom.length) | ||
} | ||
}) | ||
|
||
socket.on('update-status', (obj) => { | ||
const room = findRoom(socket.rooms) | ||
socket.broadcast.to(room).emit('update-status', obj) | ||
}) | ||
|
||
socket.on('player-event', (obj) => { | ||
const room = findRoom(socket.rooms) | ||
socket.broadcast.to(room).emit('player-event', obj) | ||
}) | ||
|
||
socket.on('change', (mediaId) => { | ||
const room = findRoom(socket.rooms) | ||
socket.broadcast.to(room).emit('change', mediaId) | ||
}) | ||
|
||
socket.on('emoji', (name) => { | ||
const room = findRoom(socket.rooms) | ||
socket.broadcast.to(room).emit('emoji', name) | ||
}) | ||
res.send({status: 'ok'}) | ||
}) | ||
app.get('/autocomplete/:country', autocomplete) | ||
app.get('/bif', bif) | ||
app.get('/opening', opening) | ||
app.use('/mal', mal) | ||
|
||
// listen | ||
srv.listen(3001, () => { | ||
console.log('listening on 3001') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
function findRoom (rooms) { | ||
return Object.keys(rooms).find((k) => k.startsWith('umi//')) | ||
} | ||
|
||
function setupIo (io) { | ||
io.on('connection', (socket) => { | ||
socket.on('join-room', (room) => { | ||
const currentRoom = findRoom(socket.rooms) | ||
if (currentRoom) return | ||
|
||
socket.join(room) | ||
socket.broadcast.to(room).emit('user-joined') | ||
const ioRoom = io.sockets.adapter.rooms[room] | ||
if (ioRoom) { | ||
socket.broadcast.to(room).emit('room-count', ioRoom.length) | ||
socket.emit('room-count', ioRoom.length) | ||
} | ||
}) | ||
|
||
socket.on('leave-room', () => { | ||
const room = findRoom(socket.rooms) | ||
socket.leave(room) | ||
socket.broadcast.to(room).emit('user-left') | ||
const ioRoom = io.sockets.adapter.rooms[room] | ||
if (ioRoom) { | ||
socket.broadcast.to(room).emit('room-count', ioRoom.length) | ||
} | ||
}) | ||
|
||
socket.once('disconnecting', () => { | ||
const room = findRoom(socket.rooms) | ||
if (!room) return | ||
|
||
socket.leave(room) | ||
socket.broadcast.to(room).emit('user-left') | ||
const ioRoom = io.sockets.adapter.rooms[room] | ||
if (ioRoom) { | ||
socket.broadcast.to(room).emit('room-count', ioRoom.length) | ||
} | ||
}) | ||
|
||
socket.on('update-status', (obj) => { | ||
const room = findRoom(socket.rooms) | ||
socket.broadcast.to(room).emit('update-status', obj) | ||
}) | ||
|
||
socket.on('player-event', (obj) => { | ||
const room = findRoom(socket.rooms) | ||
socket.broadcast.to(room).emit('player-event', obj) | ||
}) | ||
|
||
socket.on('change', (mediaId) => { | ||
const room = findRoom(socket.rooms) | ||
socket.broadcast.to(room).emit('change', mediaId) | ||
}) | ||
|
||
socket.on('emoji', (name) => { | ||
const room = findRoom(socket.rooms) | ||
socket.broadcast.to(room).emit('emoji', name) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = setupIo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const express = require('express') | ||
const axios = require('axios') | ||
const {json} = require('body-parser') | ||
const popura = require('popura') | ||
const Iron = require('iron') | ||
|
||
const {IRON_TOKEN} = process.env | ||
|
||
const router = express.Router() | ||
router.use(json()) | ||
|
||
router.post('/login', function malLogin (req, res) { | ||
const {username, password} = req.body | ||
if (!username || !password) return res.status(400).send({status: 'not ok', error: 'Invalid login'}) | ||
|
||
const client = popura(username, password) | ||
|
||
client.verifyAuth() | ||
.then((obj) => { | ||
Iron.seal(password, IRON_TOKEN, Iron.defaults, (err, sealed) => { | ||
if (err) return res.status(500).send({status: 'not ok', error: 'Internal error'}) | ||
|
||
res.send({ | ||
status: 'ok', | ||
username: obj.username, | ||
password: sealed | ||
}) | ||
}) | ||
}) | ||
.catch((err) => { | ||
res.status(err.statusCode).send({ | ||
status: 'not ok', | ||
error: err.message | ||
}) | ||
}) | ||
}) | ||
|
||
router.get('/series', function malSeries (req, res) { | ||
const {name} = req.query | ||
if (!name) return res.status(400).send({status: 'not ok', error: 'Invalid payload'}) | ||
|
||
axios({ | ||
method: 'GET', | ||
url: 'https://myanimelist.net/search/prefix.json', | ||
params: { | ||
type: 'anime', | ||
keyword: name, | ||
v: 1 | ||
} | ||
}) | ||
.then(({data}) => { | ||
const {items} = data.categories[0] | ||
|
||
let item = items.find((i) => i.name.toLowerCase() === name.toLowerCase()) || items[0] | ||
if (name === 'My Hero Academia Season 2') { | ||
item = items.find((i) => i.name === 'Boku no Hero Academia 2nd Season') || item | ||
} | ||
if (!item) return res.status(404).send({status: 'not ok', error: `Couldn't find anime for "${name}"`}) | ||
|
||
res.send({status: 'ok', item}) | ||
}) | ||
}) | ||
|
||
router.post('/update', function malUpdate (req, res) { | ||
const {username, password, id, episode, status} = req.body | ||
if (!username || !password) return res.status(400).send({status: 'not ok', error: 'Invalid login'}) | ||
if (!id || !episode || !status) return res.status(400).send({status: 'not ok', error: 'Invalid payload'}) | ||
|
||
Iron.unseal(password, IRON_TOKEN, Iron.defaults, (err, unsealed) => { | ||
if (err) return res.status(500).send({status: 'not ok', error: 'Internal error'}) | ||
|
||
const client = popura(username, unsealed) | ||
client.updateAnime(id, {episode, status}) | ||
.then(() => { | ||
res.send({status: 'ok'}) | ||
}) | ||
.catch((err) => { | ||
console.error(err) | ||
res.status(500).send({status: 'not ok', error: 'Internal error'}) | ||
}) | ||
}) | ||
}) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.