added album and playlist support, cleaned up code
This commit is contained in:
parent
54dbc4e08d
commit
fdd1151cbe
3 changed files with 89 additions and 30 deletions
109
src/index.js
109
src/index.js
|
|
@ -7,6 +7,11 @@ const CLIENT_ID = 'dN2N95wCyEBTllu4' // mobile atmos
|
|||
const AUDIO_QUALITIES = ['LOW', 'HIGH', 'LOSSLESS', 'HI_RES']
|
||||
const VIDEO_QUALITIES = ['AUDIO_ONLY', 'LOW', 'MEDIUM', 'HIGH']
|
||||
|
||||
const TYPE_QUALITY_INFO = {
|
||||
tracks: { default: 'LOSSLESS', list: AUDIO_QUALITIES, param: 'audioquality' },
|
||||
videos: { default: 'HIGH', list: VIDEO_QUALITIES, param: 'videoquality' },
|
||||
}
|
||||
|
||||
function to_form_data(obj) {
|
||||
return Object.entries(obj).map(([k, v]) => `${k}=${v}`).join('&')
|
||||
}
|
||||
|
|
@ -62,25 +67,28 @@ async function api_call(method, path, params={}, body=null) {
|
|||
return { status, json }
|
||||
}
|
||||
|
||||
router.get('/track/:id', async ({ params, query }) => {
|
||||
let id = parseInt(params.id)
|
||||
async function playbackinfopostpaywall(type, id, q) {
|
||||
id = parseInt(id)
|
||||
if (isNaN(id)) {
|
||||
return new Response('Invalid track id', { status: 400 })
|
||||
return new Response('Invalid id', { status: 400 })
|
||||
}
|
||||
|
||||
let quality = query.q || 'LOSSLESS'
|
||||
const quality_info = TYPE_QUALITY_INFO[type]
|
||||
let quality = q || quality_info.default
|
||||
quality = quality.toUpperCase()
|
||||
if (!AUDIO_QUALITIES.includes(quality)) {
|
||||
if (!quality_info.list.includes(quality)) {
|
||||
return new Response('Invalid quality', { status: 400 })
|
||||
}
|
||||
|
||||
const res = await api_call('GET', `tracks/${id}/playbackinfopostpaywall`, {
|
||||
audioquality: quality,
|
||||
let params = {
|
||||
assetpresentation: 'FULL',
|
||||
playbackmode: 'OFFLINE'
|
||||
})
|
||||
}
|
||||
params[quality_info.param] = quality
|
||||
|
||||
console.log(res.status, res.json)
|
||||
const res = await api_call('GET', `${type}/${id}/playbackinfopostpaywall`, params)
|
||||
|
||||
console.log('playbackinfopostpaywall', res.status, res.json)
|
||||
|
||||
if (res.status !== 200) {
|
||||
return new Response('Couldn\'t get manifest', { status: 403 })
|
||||
|
|
@ -92,38 +100,89 @@ router.get('/track/:id', async ({ params, query }) => {
|
|||
|
||||
const manifest = JSON.parse(atob(res.json.manifest))
|
||||
return Response.redirect(manifest.urls[0], 302)
|
||||
}
|
||||
|
||||
router.get('/track/:id', async ({ params, query }) => {
|
||||
return await playbackinfopostpaywall('tracks', params.id, query.q)
|
||||
})
|
||||
|
||||
router.get('/video/:id', async ({ params, query }) => {
|
||||
let id = parseInt(params.id)
|
||||
if (isNaN(id)) {
|
||||
return new Response('Invalid video id', { status: 400 })
|
||||
return await playbackinfopostpaywall('videos', params.id, query.q)
|
||||
})
|
||||
|
||||
function m3u8(items, audio_quality, video_quality, host) {
|
||||
let list = '#EXTM3U\n'
|
||||
|
||||
for (const item_info of items) {
|
||||
const item = item_info.item
|
||||
const type = item_info.type
|
||||
|
||||
let quality
|
||||
if (type === 'track') {
|
||||
quality = audio_quality
|
||||
} else {
|
||||
quality = video_quality
|
||||
}
|
||||
|
||||
const url = `https://${host}/${type}/${item.id}?q=${quality}`
|
||||
list += `#EXTINF:${item.duration},${item.title}\n${url}\n`
|
||||
}
|
||||
|
||||
let quality = query.q || 'HIGH'
|
||||
quality = quality.toUpperCase()
|
||||
if (!VIDEO_QUALITIES.includes(quality)) {
|
||||
return new Response('Invalid quality', { status: 400 })
|
||||
return list
|
||||
}
|
||||
|
||||
async function get_items(type, id, aq, vq, host) {
|
||||
let audio_quality = aq || 'LOSSLESS'
|
||||
audio_quality = audio_quality.toUpperCase()
|
||||
if (!AUDIO_QUALITIES.includes(audio_quality)) {
|
||||
return new Response('Invalid audio quality', { status: 400 })
|
||||
}
|
||||
|
||||
const res = await api_call('GET', `videos/${id}/playbackinfopostpaywall`, {
|
||||
videoquality: quality,
|
||||
assetpresentation: 'FULL',
|
||||
playbackmode: 'OFFLINE'
|
||||
let video_quality = vq || 'HIGH'
|
||||
video_quality = video_quality.toUpperCase()
|
||||
if (!VIDEO_QUALITIES.includes(video_quality)) {
|
||||
return new Response('Invalid video quality', { status: 400 })
|
||||
}
|
||||
|
||||
const res = await api_call('GET', `${type}/${id}/items`, {
|
||||
offset: 0,
|
||||
limit: 4294967295,
|
||||
countryCode: 'GB',
|
||||
})
|
||||
|
||||
console.log(res.status, res.json)
|
||||
|
||||
if (res.status !== 200) {
|
||||
return new Response('Couldn\'t get manifest', { status: 403 })
|
||||
return new Response('Couldn\'t get items', { status: 403 })
|
||||
}
|
||||
|
||||
if (res.json.manifestMimeType !== 'application/vnd.tidal.bts') {
|
||||
return new Response('Invalid manifest mime type', { status: 500 })
|
||||
const m3u = m3u8(res.json.items, audio_quality, video_quality, host)
|
||||
|
||||
return new Response(m3u, {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-mpegurl'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
router.get('/album/:id', async ({ params, query, headers }) => {
|
||||
let id = parseInt(params.id)
|
||||
if (isNaN(id)) {
|
||||
return new Response('Invalid album id', { status: 400 })
|
||||
}
|
||||
|
||||
const manifest = JSON.parse(atob(res.json.manifest))
|
||||
return Response.redirect(manifest.urls[0], 302)
|
||||
return await get_items('albums', id, query.aq, query.vq, headers.get('host'))
|
||||
})
|
||||
|
||||
router.get('/playlist/:id', async ({ params, query, headers }) => {
|
||||
let id = params.id
|
||||
|
||||
const uuid_regex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi;
|
||||
if (!uuid_regex.test(id)) {
|
||||
return new Response('Invalid playlist id', { status: 400 })
|
||||
}
|
||||
|
||||
return await get_items('playlists', id, query.aq, query.vq, headers.get('host'))
|
||||
})
|
||||
|
||||
addEventListener('fetch', event =>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue