diff options
author | 2023-04-10 21:57:33 +0200 | |
---|---|---|
committer | 2023-04-10 21:57:33 +0200 | |
commit | f8463676a40656893c2048655e8807099e3adb39 (patch) | |
tree | 9b38927a293e38ec38da2707434786d92327edd0 /pages/api/radio | |
parent | f4e00553c4acf44de48af958afc8747128b55562 (diff) | |
download | my_apps-f8463676a40656893c2048655e8807099e3adb39.tar.gz my_apps-f8463676a40656893c2048655e8807099e3adb39.tar.bz2 my_apps-f8463676a40656893c2048655e8807099e3adb39.zip |
add radio app
Diffstat (limited to 'pages/api/radio')
-rw-r--r-- | pages/api/radio/stations.js | 45 | ||||
-rw-r--r-- | pages/api/radio/stream.js | 34 |
2 files changed, 79 insertions, 0 deletions
diff --git a/pages/api/radio/stations.js b/pages/api/radio/stations.js new file mode 100644 index 0000000..9ebb55e --- /dev/null +++ b/pages/api/radio/stations.js @@ -0,0 +1,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 + } +}) diff --git a/pages/api/radio/stream.js b/pages/api/radio/stream.js new file mode 100644 index 0000000..42e8521 --- /dev/null +++ b/pages/api/radio/stream.js @@ -0,0 +1,34 @@ +import withSession from 'hocs/withSession' + +export default withSession(async (req, res) => { + switch (req.method) { + case 'POST': + try { + const user = req.session.get('user') + const { station } = JSON.parse(req.body) + + if (!user || !user?.isVerified || !station) { + throw new Error('Something went wrong') + } + + const response = await fetch(station.replace('radios', 'radios/play')) + const data = await response.text() + + const script = /(?<=ST\.radio = {)[\s\S]*?(?=script)/.exec(data)?.[0] + const url = /(?<=url: ").*(?=")/.exec(script)?.[0] + const mimeType = /(?<=content_type: ").*(?=")/.exec(script)?.[0] + + const songImage = /(?<="song-image")[\s\S]*?(?=div)/.exec(data)?.[0] + const thumbnail = /(?<=src\=").*(?=")/.exec(songImage)?.[0] + + res.status(200).json({url, mimeType, thumbnail}) + } catch (error) { + console.log(error) + res.status(400).send('') + } + break + default: + res.status(400).send('') + break + } +}) |