diff --git a/src/streamers/soundcloud/main.ts b/src/streamers/soundcloud/main.ts index 36534b7..b7bb4b2 100644 --- a/src/streamers/soundcloud/main.ts +++ b/src/streamers/soundcloud/main.ts @@ -46,11 +46,12 @@ interface SoundcloudTranscoding { export default class Soundcloud implements Streamer { hostnames = ['soundcloud.com', 'm.soundcloud.com', 'www.soundcloud.com'] oauthToken?: string + client?: ScClient constructor(options: SoundcloudOptions) { this.oauthToken = options?.oauthToken } async search(query: string, limit = 20): Promise { - const client = await this.#getClient() + const client = this.client || (await this.#getClient()) const response = await fetch( this.#formatURL( `https://api-v2.soundcloud.com/search?q=${encodeURIComponent( @@ -83,7 +84,7 @@ export default class Soundcloud implements Streamer { else if (resultResponse.collection[i].kind == 'playlist') items.albums.push(await parseAlbum(resultResponse.collection[i])) else if (resultResponse.collection[i].kind == 'user') - items.artists.push(parseArtist(resultResponse.collection[i])) + items.artists.push((await parseArtist(resultResponse.collection[i]))) } return items @@ -103,6 +104,7 @@ export default class Soundcloud implements Streamer { id: await fetchKey(response) } + this.client = client return client } @@ -134,7 +136,7 @@ export default class Soundcloud implements Streamer { // loosely based off: https://github.com/wukko/cobalt/blob/92c0e1d7b7df262fcd82ea7f5cf8c58c6d2ad744/src/modules/processing/services/soundcloud.js const type = await this.getTypeFromUrl(url) - const client = await this.#getClient() + const client = this.client || (await this.#getClient()) // getting the IDs and track authorization const html = await (await fetch(url, { method: 'get', headers: headers() })).text() @@ -179,17 +181,13 @@ export default class Soundcloud implements Streamer { const parsed: GetByUrlResponse = { type: 'album', metadata: await parseAlbum(data), - tracks: await Promise.all( - data.tracks.map(async (track: RawTrack) => { - if (!track.title) track = await this.#getRawTrackInfo(track.id, client) - track.user = data.user - return parseTrack(track) - }) - ) + tracks: [] } for (const i in data.tracks) { - const track = data.tracks[i] + let track = data.tracks[i] + + if (!track.title) track = await this.#getRawTrackInfo(track.id, client) const parsedTrack = { type: 'track', @@ -217,7 +215,7 @@ export default class Soundcloud implements Streamer { return { type: 'artist', - metadata: parseArtist(data) + metadata: (await parseArtist(data)) } } default: @@ -232,9 +230,9 @@ export default class Soundcloud implements Streamer { headers: headers(this.oauthToken) }) ).text() - )[0] + ) - return api + return {...api, id} } async getAccountInfo(): Promise { const track = await this.getByUrl('https://soundcloud.com/ween/polka-dot-tail') diff --git a/src/streamers/soundcloud/parse.ts b/src/streamers/soundcloud/parse.ts index d3fb9b3..138f1d6 100644 --- a/src/streamers/soundcloud/parse.ts +++ b/src/streamers/soundcloud/parse.ts @@ -36,7 +36,7 @@ export interface Headers { 'User-Agent': string } -export function parseArtist(raw: RawArtist): Artist { +export async function parseArtist(raw: RawArtist): Promise { const artist: Artist = { id: raw.id, url: raw.permalink_url, @@ -56,6 +56,7 @@ export interface RawAlbum { track_count: number release_date: string user: RawArtist + user_id: number | string kind: 'playlist' } @@ -66,7 +67,7 @@ export async function parseAlbum(raw: RawAlbum): Promise { url: raw.permalink_url, trackCount: raw.track_count, releaseDate: new Date(raw.release_date), - artists: [parseArtist(raw.user)] + artists: [(await parseArtist(raw.user))] } if (raw.tracks?.[0]?.artwork_url != undefined) { album.coverArtwork = [await parseCoverArtwork(raw?.tracks?.[0]?.artwork_url)] @@ -79,7 +80,7 @@ export interface RawTrack { transcodings?: { duration: number }[] } kind: 'track' - id: number + id: number | string title: string duration: number, created_at: string, @@ -89,6 +90,7 @@ export interface RawTrack { user: RawArtist, last_modified: string, description: string + user_id: number | string } export async function parseTrack(raw: RawTrack): Promise { @@ -96,7 +98,7 @@ export async function parseTrack(raw: RawTrack): Promise { id: raw.id, title: raw.title, url: raw.permalink_url, - artists: [parseArtist(raw.user)], + artists: [(await parseArtist(raw.user))], durationMs: (raw.full_duration || raw.media?.transcodings?.[0]?.duration), releaseDate: new Date(raw.created_at), description: raw.description