make getTypeFromUrl async

This commit is contained in:
hazycora
2024-04-15 21:51:32 -04:00
parent 94c5134af2
commit 172b1cf056
6 changed files with 14 additions and 13 deletions

View File

@@ -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<ItemType> {
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}`)
}

View File

@@ -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<ItemType> {
return this.#getUrlParts(url)[0]
}
async getByUrl(url: string): Promise<GetByUrlResponse> {

View File

@@ -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<ItemType> {
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<GetByUrlResponse> {
// 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

View File

@@ -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<GetByUrlResponse> {

View File

@@ -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<ItemType> {
return this.#getUrlParts(url)[0]
}
async getByUrl(url: string): Promise<GetByUrlResponse> {

View File

@@ -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<SearchResults>
getTypeFromUrl(url: string): ItemType
getTypeFromUrl(url: string): Promise<ItemType>
getByUrl:
| ((url: string) => Promise<GetByUrlResponse>)
| ((url: string, limit?: number) => Promise<GetByUrlResponse>)