aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api/radio/stations.js
blob: 9ebb55eb2acf9587fef4914fe07fea67060294dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import withSession from 'hocs/withSession'
import * as cheerio from 'cheerio';

const DOMAIN = 'https://streema.com'

export default withSession(async (req, res) => {
  switch (req.method) {
    case 'POST':
      try {
        const user = req.session.get('user')
        const { query } = JSON.parse(req.body)

        if (!user || !user?.isVerified || !query) {
          throw new Error('Something went wrong')
        }

        const response = await fetch(`${DOMAIN}/radios/search/?q=${query.replaceAll(' ', '+')}`)
        const data = await response.text()
        const $ = cheerio.load(data);
        const results = []

        $('.item', '.items-list').each((_, item) => {
          const link = $(item).find('.item-name a')
          results.push({
            logo: $(item).find('.item-logo img').attr('src'),
            title: link.attr('title').replace('Listen to', ''),
            url: DOMAIN + link.attr('href'),
            genres: $(item).find('.item-extra .genre span')
              .map((_, g) => ($(g).text().trim())).toArray(),
            locations: $(item).find('.item-extra .location span')
              .map((_, l) => ($(l).text().trim())).toArray(),
          })
        });

        res.status(200).json(results)
      } catch (e) {
        console.error(e)
        res.status(400).send('')
      }
      break
    default:
      res.status(400).send('')
      break
  }
})