add dispatcher support for all platforms

This commit is contained in:
aria
2024-08-05 01:04:01 -04:00
parent 99df8075b1
commit d1c262e31e
4 changed files with 65 additions and 26 deletions

View File

@@ -1,4 +1,4 @@
import { fetch } from 'undici'
import { Dispatcher, fetch } from 'undici'
import { spawn } from 'child_process'
import os from 'node:os'
import fs from 'node:fs'
@@ -32,6 +32,7 @@ interface TidalOptions {
refreshToken: string
expires: number
countryCode: string
dispatcher?: Dispatcher
}
interface LoginData {
@@ -71,6 +72,7 @@ export default class Tidal implements Streamer {
expires: number
countryCode: string
userId: number | undefined
dispatcher: Dispatcher | undefined
hostnames = ['tidal.com', 'www.tidal.com', 'listen.tidal.com']
testData = {
'https://tidal.com/browse/artist/3908662': {
@@ -96,6 +98,7 @@ export default class Tidal implements Streamer {
this.refreshToken = options.refreshToken
this.expires = options.expires
this.countryCode = options.countryCode
if (options.dispatcher) this.dispatcher = options.dispatcher
const getReady = async () => {
if (!this.refreshToken) return
@@ -130,7 +133,8 @@ export default class Tidal implements Streamer {
const response = await fetch(
`${base}${url}?${new URLSearchParams(<{ [key: string]: string }>params)}`,
{
headers: this.headers()
headers: this.headers(),
dispatcher: this.dispatcher
}
)
if (!response.ok) {
@@ -156,13 +160,15 @@ export default class Tidal implements Streamer {
}
async sessionValid() {
const resp = await fetch('https://api.tidal.com/v1/sessions', {
headers: this.headers()
headers: this.headers(),
dispatcher: this.dispatcher
})
return resp.ok
}
async getCountryCode() {
const sessionResponse = await fetch('https://api.tidal.com/v1/sessions', {
headers: this.headers()
headers: this.headers(),
dispatcher: this.dispatcher
})
if (sessionResponse.status != 200) return false
const sessionData = <SessionData>await sessionResponse.json()
@@ -176,7 +182,8 @@ export default class Tidal implements Streamer {
body: new URLSearchParams({
client_id: this.tvToken,
scope: 'r_usr w_usr'
})
}),
dispatcher: this.dispatcher
})
if (deviceAuthResponse.status != 200) throw new Error(`Couldn't authorize Tidal`)
interface DeviceAuth {
@@ -199,7 +206,8 @@ export default class Tidal implements Streamer {
await new Promise((r) => setTimeout(r, 1000))
const loginResponse = await fetch(`${TIDAL_AUTH_BASE}oauth2/token`, {
method: 'post',
body: new URLSearchParams(params)
body: new URLSearchParams(params),
dispatcher: this.dispatcher
})
statusCode = loginResponse.status
loginData = <LoginData>await loginResponse.json()
@@ -235,7 +243,8 @@ export default class Tidal implements Streamer {
client_id: this.tvToken,
client_secret: this.tvSecret,
grant_type: 'refresh_token'
})
}),
dispatcher: this.dispatcher
})
if (refreshResponse.status == 200) {
@@ -350,7 +359,7 @@ export default class Tidal implements Streamer {
if (playbackInfoResponse.manifestMimeType != 'application/dash+xml') {
const manifest = <Manifest>JSON.parse(manifestStr)
const streamResponse = await fetch(manifest.urls[0])
const streamResponse = await fetch(manifest.urls[0], { dispatcher: this.dispatcher })
return {
mimeType: manifest.mimeType,
sizeBytes: parseInt(<string>streamResponse.headers.get('Content-Length')),
@@ -500,7 +509,8 @@ export default class Tidal implements Streamer {
if (this.userId == undefined || this.countryCode == undefined) {
if (Date.now() > this.expires) await this.refresh()
const sessionResponse = await fetch('https://api.tidal.com/v1/sessions', {
headers: this.headers()
headers: this.headers(),
dispatcher: this.dispatcher
})
const sessionData = <SessionData>await sessionResponse.json()