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 { interface LucidaOptions {
modules: { [key: string]: Streamer | StreamerWithLogin } modules: { [key: string]: Streamer | StreamerWithLogin }
@@ -38,12 +38,12 @@ class Lucida {
const moduleNames = Object.keys(this.modules) const moduleNames = Object.keys(this.modules)
return Object.fromEntries(results.map((e, i) => [moduleNames[i], e])) 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) const urlObj = new URL(url)
for (const i in this.modules) { for (const i in this.modules) {
const matches = this.modules[i].hostnames.includes(urlObj.hostname) const matches = this.modules[i].hostnames.includes(urlObj.hostname)
if (!matches) continue 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}`) throw new Error(`Couldn't find module for hostname ${urlObj.hostname}`)
} }

View File

@@ -1,6 +1,7 @@
import crypto from 'crypto' import crypto from 'crypto'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import { import {
ItemType,
StreamerWithLogin, StreamerWithLogin,
SearchResults, SearchResults,
GetByUrlResponse, GetByUrlResponse,
@@ -214,7 +215,7 @@ export default class Qobuz implements StreamerWithLogin {
} }
return [urlParts[0], urlParts[1]] return [urlParts[0], urlParts[1]]
} }
getTypeFromUrl(url: string): 'artist' | 'album' | 'track' { async getTypeFromUrl(url: string): Promise<ItemType> {
return this.#getUrlParts(url)[0] return this.#getUrlParts(url)[0]
} }
async getByUrl(url: string): Promise<GetByUrlResponse> { async getByUrl(url: string): Promise<GetByUrlResponse> {

View File

@@ -1,6 +1,6 @@
import fetch, { HeadersInit } from 'node-fetch' import fetch, { HeadersInit } from 'node-fetch'
import { DEFAULT_HEADERS, SC_VERSION } from './constants.js' 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 { import {
parseAlbum, parseAlbum,
parseTrack, parseTrack,
@@ -97,7 +97,7 @@ export default class Soundcloud implements Streamer {
return client return client
} }
getTypeFromUrl(url: string): 'artist' | 'album' | 'track' { async getTypeFromUrl(url: string): Promise<ItemType> {
const { pathname } = new URL(url) const { pathname } = new URL(url)
if (pathname.split('/').slice(1).length == 1) return 'artist' if (pathname.split('/').slice(1).length == 1) return 'artist'
else { else {
@@ -125,7 +125,7 @@ export default class Soundcloud implements Streamer {
async #getMetadata(url: string): Promise<GetByUrlResponse> { async #getMetadata(url: string): Promise<GetByUrlResponse> {
// loosely based off: https://github.com/wukko/cobalt/blob/92c0e1d7b7df262fcd82ea7f5cf8c58c6d2ad744/src/modules/processing/services/soundcloud.js // 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() const client = await this.#getClient()
// getting the IDs and track authorization // getting the IDs and track authorization

View File

@@ -1,6 +1,6 @@
import Librespot, { LibrespotOptions } from 'librespot' import Librespot, { LibrespotOptions } from 'librespot'
import { parseArtist, parseAlbum, parseTrack } from './parse.js' 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 { class Spotify implements StreamerWithLogin {
client: Librespot client: Librespot
@@ -21,7 +21,7 @@ class Spotify implements StreamerWithLogin {
if (!parts[1]) throw new Error('Unknown Spotify URL') if (!parts[1]) throw new Error('Unknown Spotify URL')
return [parts[0], parts[1]] return [parts[0], parts[1]]
} }
getTypeFromUrl(url: string) { async getTypeFromUrl(url: string) {
return this.#getUrlParts(url)[0] return this.#getUrlParts(url)[0]
} }
async getByUrl(url: string, limit = 0): Promise<GetByUrlResponse> { async getByUrl(url: string, limit = 0): Promise<GetByUrlResponse> {

View File

@@ -1,6 +1,6 @@
import fetch from 'node-fetch' import fetch from 'node-fetch'
import { spawn } from 'child_process' 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 { TIDAL_AUTH_BASE, TIDAL_API_BASE } from './constants.js'
import { import {
Contributor, Contributor,
@@ -334,7 +334,7 @@ export default class Tidal implements Streamer {
} }
return [urlParts[0], urlParts[1]] return [urlParts[0], urlParts[1]]
} }
getTypeFromUrl(url: string): 'artist' | 'album' | 'track' { async getTypeFromUrl(url: string): Promise<ItemType> {
return this.#getUrlParts(url)[0] return this.#getUrlParts(url)[0]
} }
async getByUrl(url: string): Promise<GetByUrlResponse> { async getByUrl(url: string): Promise<GetByUrlResponse> {

View File

@@ -1,6 +1,6 @@
type Id = string | number type Id = string | number
type ItemType = 'artist' | 'album' | 'track' export type ItemType = 'artist' | 'album' | 'track'
export interface CoverArtwork { export interface CoverArtwork {
url: string url: string
@@ -84,7 +84,7 @@ export interface AlbumGetByUrlResponse {
export interface Streamer { export interface Streamer {
hostnames: string[] hostnames: string[]
search(query: string, limit: number): Promise<SearchResults> search(query: string, limit: number): Promise<SearchResults>
getTypeFromUrl(url: string): ItemType getTypeFromUrl(url: string): Promise<ItemType>
getByUrl: getByUrl:
| ((url: string) => Promise<GetByUrlResponse>) | ((url: string) => Promise<GetByUrlResponse>)
| ((url: string, limit?: number) => Promise<GetByUrlResponse>) | ((url: string, limit?: number) => Promise<GetByUrlResponse>)