generated from uwu/neptune-template
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RealMAX - WIP | TidalTags & fetch fixes
- Loading branch information
Showing
20 changed files
with
200 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export class Semaphore { | ||
private avalibleSlots: number; | ||
private readonly queued: (() => void)[] = []; | ||
|
||
constructor(slots: number) { | ||
this.avalibleSlots = slots; | ||
} | ||
|
||
public async obtain() { | ||
// If there is an available request slot, proceed immediately | ||
if (this.avalibleSlots > 0) return this.avalibleSlots--; | ||
|
||
// Otherwise, wait for a request slot to become available | ||
return new Promise((r) => this.queued.push(() => r(this.avalibleSlots--))); | ||
} | ||
|
||
public release(): void { | ||
this.avalibleSlots++; | ||
// If there are queued requests, resolve the first one in the queue | ||
this.queued.shift()?.(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const debounce = <T extends (...args: any[]) => any>(func: T, wait: number): ((...args: Parameters<T>) => void) => { | ||
let timeout: NodeJS.Timeout | null; | ||
return async function (this: ThisParameterType<T>, ...args: Parameters<T>) { | ||
const context = this; | ||
if (timeout) clearTimeout(timeout); | ||
timeout = setTimeout(async () => { | ||
timeout = null; | ||
func.apply(context, args); | ||
}, wait); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { intercept } from "@neptune"; | ||
import { ActionType, UninterceptFunction } from "neptune-types/api/intercept"; | ||
import { ActionTypes } from "neptune-types/tidal"; | ||
|
||
function convertToUpperCaseWithUnderscores(str: string) { | ||
return str | ||
.replace(/([a-z0-9])([A-Z])/g, "$1_$2") // Convert camelCase to snake_case | ||
.toUpperCase(); // Convert to uppercase | ||
} | ||
const neptuneActions = window.neptune.actions; | ||
export type ActionHandler = <AT extends ActionType>(interceptPath: AT, payload: ActionTypes[AT]) => void; | ||
export const interceptActions = (actionPath: RegExp, handler: ActionHandler) => { | ||
const unloadables: UninterceptFunction[] = []; | ||
for (const item in neptuneActions) { | ||
for (const action in window.neptune.actions[<keyof typeof neptuneActions>item]) { | ||
const interceptPath = `${item}/${convertToUpperCaseWithUnderscores(action)}`; | ||
if (!actionPath.test(interceptPath)) continue; | ||
unloadables.push(intercept(<ActionType>interceptPath, (payload) => handler(payload[1], payload[0]))); | ||
} | ||
} | ||
return () => unloadables.forEach((u) => u()); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
import { ISRCResponse } from "./types/ISRC"; | ||
import { ISRCResponse, TrackData } from "./types/ISRC"; | ||
import { requestStream, rejectNotOk, toJson } from "../fetch"; | ||
import { getToken } from "./auth"; | ||
|
||
export const fetchIsrc = async (isrc: string, limit?: number) => | ||
requestStream(`https://openapi.tidal.com/tracks/byIsrc?isrc=${isrc}&countryCode=US&limit=${limit ?? 100}`, { | ||
type ISRCOptions = { | ||
offset: number; | ||
limit: number; | ||
}; | ||
export const fetchIsrc = async (isrc: string, options?: ISRCOptions) => { | ||
const { limit, offset } = options ?? { limit: 100, offset: 0 }; | ||
return requestStream(`https://openapi.tidal.com/tracks/byIsrc?isrc=${isrc}&countryCode=US&limit=${limit}&offset=${offset}`, { | ||
headers: { | ||
Authorization: `Bearer ${await getToken()}`, | ||
"Content-Type": "application/vnd.tidal.v1+json", | ||
}, | ||
}) | ||
.then(rejectNotOk) | ||
.then(toJson<ISRCResponse>); | ||
}; | ||
|
||
export async function* fetchIsrcIterable(isrc: string): AsyncIterable<TrackData> { | ||
let offset = 0; | ||
const limit = 100; | ||
while (true) { | ||
const response = await fetchIsrc(isrc, { limit, offset }); | ||
if (response?.data !== undefined) yield* response.data; | ||
if (response.data.length < limit) break; | ||
offset += limit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,17 @@ | ||
type ImageResource = { | ||
url: string; | ||
width: number; | ||
height: number; | ||
}; | ||
|
||
type SimpleArtist = { | ||
id: string; | ||
name: string; | ||
picture: ImageResource[]; | ||
main: boolean; | ||
}; | ||
|
||
type SimpleAlbum = { | ||
id: string; | ||
title: string; | ||
imageCover: ImageResource[]; | ||
videoCover: ImageResource[]; | ||
}; | ||
|
||
type ProviderInfo = { | ||
providerId?: string; | ||
providerName?: string; | ||
}; | ||
import type { TrackItem } from "neptune-types/tidal"; | ||
|
||
type MediaMeta = { | ||
tags: string[]; | ||
}; | ||
|
||
type Track = { | ||
id: string; | ||
version: string; | ||
duration: number; | ||
album: SimpleAlbum; | ||
title: string; | ||
copyright: string; | ||
artists: SimpleArtist[]; | ||
popularity?: number; | ||
isrc: string; | ||
trackNumber: number; | ||
volumeNumber: number; | ||
tidalUrl: string; | ||
providerInfo?: ProviderInfo; | ||
artifactType: string; | ||
mediaMetadata: MediaMeta; | ||
}; | ||
|
||
type TrackProperties = { | ||
content: string[]; | ||
}; | ||
|
||
type MultiStatusResponseDataTrack = { | ||
resource: Track; | ||
properties: TrackProperties; | ||
export type TrackData = { | ||
resource: TrackItem; | ||
id: string; | ||
status: number; | ||
message: string; | ||
}; | ||
|
||
type MultiStatusResponseMetadata = { | ||
requested: number; | ||
success: number; | ||
failure: number; | ||
}; | ||
|
||
export type ISRCResponse = { | ||
data: MultiStatusResponseDataTrack[]; | ||
metadata: MultiStatusResponseMetadata; | ||
data: TrackData[]; | ||
metadata: { | ||
requested: number; | ||
success: number; | ||
failure: number; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "RealMAX", | ||
"description": "Always ensure that the highest quality available of a track is played", | ||
"author": "Inrixia", | ||
"main": "./src/index.js" | ||
} |
Oops, something went wrong.