From 172b1cf05607b1d659facf978b9a018ada97e58d Mon Sep 17 00:00:00 2001 From: hazycora Date: Mon, 15 Apr 2024 20:51:32 -0500 Subject: [PATCH] make getTypeFromUrl async --- src/index.ts | 6 +++--- src/streamers/qobuz/main.ts | 3 ++- src/streamers/soundcloud/main.ts | 6 +++--- src/streamers/spotify/main.ts | 4 ++-- src/streamers/tidal/main.ts | 4 ++-- src/types.ts | 4 ++-- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1f27868..8a7d360 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { GetByUrlResponse, SearchResults, Streamer, StreamerWithLogin } from './types.js' +import { ItemType, GetByUrlResponse, SearchResults, Streamer, StreamerWithLogin } from './types.js' interface LucidaOptions { modules: { [key: string]: Streamer | StreamerWithLogin } @@ -38,12 +38,12 @@ class Lucida { const moduleNames = Object.keys(this.modules) return Object.fromEntries(results.map((e, i) => [moduleNames[i], e])) } - getTypeFromUrl(url: string): 'artist' | 'album' | 'track' { + async getTypeFromUrl(url: string): Promise { const urlObj = new URL(url) for (const i in this.modules) { const matches = this.modules[i].hostnames.includes(urlObj.hostname) if (!matches) continue - return this.modules[i].getTypeFromUrl(url) + return await this.modules[i].getTypeFromUrl(url) } throw new Error(`Couldn't find module for hostname ${urlObj.hostname}`) } diff --git a/src/streamers/qobuz/main.ts b/src/streamers/qobuz/main.ts index a1c5a8b..ff21429 100644 --- a/src/streamers/qobuz/main.ts +++ b/src/streamers/qobuz/main.ts @@ -1,6 +1,7 @@ import crypto from 'crypto' import fetch from 'node-fetch' import { + ItemType, StreamerWithLogin, SearchResults, GetByUrlResponse, @@ -214,7 +215,7 @@ export default class Qobuz implements StreamerWithLogin { } return [urlParts[0], urlParts[1]] } - getTypeFromUrl(url: string): 'artist' | 'album' | 'track' { + async getTypeFromUrl(url: string): Promise { return this.#getUrlParts(url)[0] } async getByUrl(url: string): Promise { diff --git a/src/streamers/soundcloud/main.ts b/src/streamers/soundcloud/main.ts index 0cd218f..e32cf97 100644 --- a/src/streamers/soundcloud/main.ts +++ b/src/streamers/soundcloud/main.ts @@ -1,6 +1,6 @@ import fetch, { HeadersInit } from 'node-fetch' import { DEFAULT_HEADERS, SC_VERSION } from './constants.js' -import { Streamer, SearchResults, GetByUrlResponse, GetStreamResponse } from '../../types.js' +import { ItemType, Streamer, SearchResults, GetByUrlResponse, GetStreamResponse } from '../../types.js' import { parseAlbum, parseTrack, @@ -97,7 +97,7 @@ export default class Soundcloud implements Streamer { return client } - getTypeFromUrl(url: string): 'artist' | 'album' | 'track' { + async getTypeFromUrl(url: string): Promise { const { pathname } = new URL(url) if (pathname.split('/').slice(1).length == 1) return 'artist' else { @@ -125,7 +125,7 @@ export default class Soundcloud implements Streamer { async #getMetadata(url: string): Promise { // loosely based off: https://github.com/wukko/cobalt/blob/92c0e1d7b7df262fcd82ea7f5cf8c58c6d2ad744/src/modules/processing/services/soundcloud.js - const type = this.getTypeFromUrl(url) + const type = await this.getTypeFromUrl(url) const client = await this.#getClient() // getting the IDs and track authorization diff --git a/src/streamers/spotify/main.ts b/src/streamers/spotify/main.ts index b250efd..4ffc6b1 100644 --- a/src/streamers/spotify/main.ts +++ b/src/streamers/spotify/main.ts @@ -1,6 +1,6 @@ import Librespot, { LibrespotOptions } from 'librespot' import { parseArtist, parseAlbum, parseTrack } from './parse.js' -import { GetByUrlResponse, SearchResults, StreamerWithLogin } from '../../types.js' +import { ItemType, GetByUrlResponse, SearchResults, StreamerWithLogin } from '../../types.js' class Spotify implements StreamerWithLogin { client: Librespot @@ -21,7 +21,7 @@ class Spotify implements StreamerWithLogin { if (!parts[1]) throw new Error('Unknown Spotify URL') return [parts[0], parts[1]] } - getTypeFromUrl(url: string) { + async getTypeFromUrl(url: string) { return this.#getUrlParts(url)[0] } async getByUrl(url: string, limit = 0): Promise { diff --git a/src/streamers/tidal/main.ts b/src/streamers/tidal/main.ts index 707ff39..b2337e9 100644 --- a/src/streamers/tidal/main.ts +++ b/src/streamers/tidal/main.ts @@ -1,6 +1,6 @@ import fetch from 'node-fetch' import { spawn } from 'child_process' -import { GetByUrlResponse, GetStreamResponse, SearchResults, Streamer, Track } from '../../types.js' +import { ItemType, GetByUrlResponse, GetStreamResponse, SearchResults, Streamer, Track } from '../../types.js' import { TIDAL_AUTH_BASE, TIDAL_API_BASE } from './constants.js' import { Contributor, @@ -334,7 +334,7 @@ export default class Tidal implements Streamer { } return [urlParts[0], urlParts[1]] } - getTypeFromUrl(url: string): 'artist' | 'album' | 'track' { + async getTypeFromUrl(url: string): Promise { return this.#getUrlParts(url)[0] } async getByUrl(url: string): Promise { diff --git a/src/types.ts b/src/types.ts index 2c02b69..df195f0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,6 @@ type Id = string | number -type ItemType = 'artist' | 'album' | 'track' +export type ItemType = 'artist' | 'album' | 'track' export interface CoverArtwork { url: string @@ -84,7 +84,7 @@ export interface AlbumGetByUrlResponse { export interface Streamer { hostnames: string[] search(query: string, limit: number): Promise - getTypeFromUrl(url: string): ItemType + getTypeFromUrl(url: string): Promise getByUrl: | ((url: string) => Promise) | ((url: string, limit?: number) => Promise)