Skip to content

Commit

Permalink
Remove old server routes and update /opening to async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
remixz committed Sep 21, 2017
1 parent d66a5e6 commit 80ebdad
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 53 deletions.
4 changes: 0 additions & 4 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ 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')

Expand All @@ -29,8 +27,6 @@ app.post('/update/:token', (req, res) => {
io.emit('app-update')
res.send({status: 'ok'})
})
app.get('/autocomplete/:country', autocomplete)
app.get('/bif', bif)
app.get('/opening', opening)
app.use('/mal', mal)

Expand Down
27 changes: 0 additions & 27 deletions server/routes/autocomplete.js

This file was deleted.

18 changes: 0 additions & 18 deletions server/routes/bif.js

This file was deleted.

11 changes: 7 additions & 4 deletions server/routes/opening.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
const axios = require('axios')
const cheerio = require('cheerio')

function openingHandler (req, res) {
async function openingHandler (req, res) {
const {search} = req.query
if (!search) return res.send({result: null})

axios.post('https://themes.moe/includes/anime_search.php', `search=-1&name=${search}`).then(({data}) => {
const [html, id] = data
try {
const {data: [html, id]} = await axios.post('https://themes.moe/includes/anime_search.php', `search=-1&name=${search}`)
if (!id) return res.send({result: null})

const $ = cheerio.load(html)
const openings = $('a.vid-popup[data-type^="OP"]').toArray().map((el) => el.attribs.href)
const result = openings[Math.floor(Math.random() * openings.length)]

res.send({result})
})
} catch (err) {
console.error(err.message)
res.send({result: null})
}
}

module.exports = openingHandler

0 comments on commit 80ebdad

Please sign in to comment.