Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Commit

Permalink
Improves TuneInProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
davipatury committed Aug 27, 2020
1 parent c1c0075 commit 2459625
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 10 deletions.
137 changes: 135 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"main": "src/index.js",
"dependencies": {
"@sentry/node": "^5.15.4",
"cheerio": "^1.0.0-rc.3",
"cors": "^2.8.5",
"erlpack": "^0.1.3",
"express": "^4.17.1",
Expand Down
12 changes: 12 additions & 0 deletions src/apis/TuneIn.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
const fetch = require('node-fetch')
const cheerio = require('cheerio')
const fs = require('fs')

const API_URL = 'https://opml.radiotime.com'
const TUNEIN_URL = 'https://tunein.com/radio/'

class TuneInAPI {
static get (id) {
return this.request('/Tune.ashx', { id }).then(({ body: [result] }) => result)
}

static async fetch (path, code) {
const res = await fetch(TUNEIN_URL + path).then(res => res.text())
const $ = cheerio.load(res)
const scriptContent = $('#initialStateEl').get([0]).children[0].data.trim()
const json = JSON.parse(scriptContent.replace('window.INITIAL_STATE=', '').replace(/;$/, ''))
const { title, description, actions: { share: { shareUrl: shortUrl, logoUrl: image } } } = json.profiles[code]
return { title, description, image, shortUrl }
}

// Internal
static async request (endpoint, queryParams = {}) {
const qParams = new URLSearchParams({ ...queryParams, render: 'json', formats: 'mp3,aac,ogg,flash,html,hls' })
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const MusicManager = require('./structures/MusicManager')

const manager = new MusicManager()
manager.connect().then(async () => {
const song = await manager.songProvider.get('https://tunein.com/radio/Rdio-Clube-FM-(Braslia)-1055-s126960/') // ytsearch:girassol da cor do seu cabelo
const song = await manager.songProvider.get('https://tunein.com/radio/Rdio-Executiva-927-s97450/') // ytsearch:girassol da cor do seu cabelo
const player = await manager.lavalink.join({
guild: '445203868624748555',
channel: '701928171519344801',
node: '1'
}, { selfdeaf: true })

console.log('Connected')
console.log(song)
console.log(song.title)

if (song) {
await player.play(song)
Expand Down
3 changes: 2 additions & 1 deletion src/structures/lavacord/Song.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ const TRACK_INFO_VERSION = 2
class Song {
constructor (track, info, provider) {
this.track = track
this.info = info
this.info = info || {}
this.provider = provider

try {
if (track) {
const decodedInfo = this.constructor.decodeTrack(track)
console.log(info, decodedInfo)
// TODO: Fix length/position in live streams
Utils.compareProperties(info, decodedInfo, { ignoreExtraKeys: true })
this.info = { ...info, ...decodedInfo }
}
Expand Down
2 changes: 1 addition & 1 deletion src/structures/providers/spotify/SpotifySong.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SpotifySong extends Song {
}

get length () {
return this.info.length
return super.length || this.spotifyTrack.duration_ms
}

get identifier () {
Expand Down
13 changes: 9 additions & 4 deletions src/structures/providers/tunein/TuneInProvider.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const TuneInAPI = require('../../../apis/TuneIn')
const TuneInSong = require('./TuneInSong')

const TUNEIN_REGEX = /tunein\.com\/radio\/[0-9A-Za-z-_()]*(s\d+)/g
const TUNEIN_REGEX = /^(?:https?:\/\/|)?(?:www\.)?tunein\.com\/radio\/([0-9A-Za-z-_()]*(s\d+))/g

class TuneInProvider {
static async get (provider, identifier) {
const regexResult = TUNEIN_REGEX.exec(identifier)
if (regexResult) {
const [, id] = regexResult
const { url } = await TuneInAPI.get(id)
return provider.get(url, true)
const [, path, id] = regexResult
const [{ url }, radioInfo] = await Promise.all([TuneInAPI.get(id), TuneInAPI.fetch(path, id)])
const { tracks } = await provider.loadTracks(url, 1)
if (tracks && tracks.length) {
const [{ track, info }] = tracks
return new TuneInSong(radioInfo, track, info, provider)
}
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/structures/providers/tunein/TuneInSong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const TuneInAPI = require('../../../apis/TuneIn')
const Song = require('../../lavacord/Song')

class TuneInSong extends Song {
constructor (radioInfo, track, info, provider) {
super(track, info, provider)
this.radioInfo = radioInfo
}

get title () {
return this.radioInfo.title
}

get uri () {
return this.radioInfo.shortUrl
}

get source () {
return 'tunein'
}
}

module.exports = TuneInSong

0 comments on commit 2459625

Please sign in to comment.