Skip to content

Commit

Permalink
Shazam - Use RealMAX
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Oct 30, 2024
1 parent 2d85bdd commit 85f9cb0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
30 changes: 17 additions & 13 deletions plugins/Shazam/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const trace = Tracer("[Shazam]");

import { settings } from "./Settings";
import { recognizeTrack } from "./shazam.native";
import { MaxTrack } from "@inrixia/lib/MaxTrack";
export { Settings } from "./Settings";

const addToPlaylist = async (playlistUUID: string, mediaItemIdsToAdd: string[]) => {
Expand Down Expand Up @@ -44,23 +45,26 @@ const handleDrop = async (event: DragEvent) => {
});
if (matches.length === 0) return trace.msg.warn(`No matches for ${file.name}`);
for (const shazamData of matches) {
const trackName = shazamData.track?.share?.subject ?? "Unknown";
const trackName = shazamData.track?.share?.subject ?? `${shazamData.track?.title ?? "unknown"} by ${shazamData.track?.artists?.[0] ?? "unknown"}"`;
const prefix = `[File: ${file.name}, Match: "${trackName}]`;
const isrc = shazamData.track?.isrc;
const isrcData = isrc !== undefined ? await fetchIsrc(isrc).catch(() => undefined) : undefined;
const ids = (isrcData?.data ?? []).map((track) => track.id);
if (ids.length > 0) {
trace.msg.log(`Adding ${trackName} to playlist`);
await addToPlaylist(
playlistUUID,
ids.filter((id) => id !== undefined)
);
} else {
trace.log(shazamData);
trace.msg.log(`Track ${trackName} is not avalible in Tidal`);
if (isrc === undefined) {
trace.msg.log(`${prefix} No isrc returned from Shazam cannot add to playlist.`);
continue;
}
let trackToAdd;
for await (trackToAdd of MaxTrack.getMaxTrackFromISRC(isrc)) {
if (MaxTrack.hasHiRes(trackToAdd)) break;
}
if (trackToAdd !== undefined) {
trace.msg.log(`Adding ${prefix}...`);
return await addToPlaylist(playlistUUID, [trackToAdd.id!.toString()]);
}
trace.log(shazamData);
trace.msg.err(`${prefix} Not avalible in Tidal.`);
}
} catch (err) {
trace.msg.err.withContext(`Failed to recognize ${file.name}`)(<Error>err);
trace.msg.err.withContext(`[File: ${file.name}] Failed to recognize!`)(<Error>err);
}
}
};
Expand Down
25 changes: 15 additions & 10 deletions plugins/_lib/MaxTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { MediaItemCache } from "./Caches/MediaItemCache";
import { ItemId, TrackItem } from "neptune-types/tidal";

export class MaxTrack {
private static readonly _maxTrackMap: Record<ItemId, Promise<Resource | false>> = {};
public static async fastCacheMaxId(itemId: ItemId): Promise<Resource | false> {
private static readonly _maxTrackMap: Record<ItemId, Promise<TrackItem | false>> = {};
public static async fastCacheMaxId(itemId: ItemId): Promise<TrackItem | false> {
if (itemId === undefined) return false;
return MaxTrack._maxTrackMap[itemId];
}
public static async getMaxTrack(itemId: ItemId | undefined): Promise<Resource | false> {
public static async getMaxTrack(itemId: ItemId | undefined): Promise<TrackItem | false> {
if (itemId === undefined) return false;

const maxTrack = MaxTrack._maxTrackMap[itemId];
Expand All @@ -25,18 +25,23 @@ export class MaxTrack {

return (this._maxTrackMap[itemId] = (async () => {
for (const isrc of isrcs) {
for await (const { resource } of fetchIsrcIterable(isrc)) {
if (resource?.id !== undefined && this.hasHiRes(<TrackItem>resource)) {
if (resource.artifactType !== "track") continue;
const maxTrackItem = await MediaItemCache.ensureTrack(resource?.id);
if (maxTrackItem !== undefined && !this.hasHiRes(maxTrackItem)) continue;
else return resource;
}
for await (const trackItem of this.getMaxTrackFromISRC(isrc)) {
if (this.hasHiRes(trackItem)) return trackItem;
}
}
return false;
})());
}
public static async *getMaxTrackFromISRC(isrc: string): AsyncGenerator<TrackItem> {
for await (const { resource } of fetchIsrcIterable(isrc)) {
if (resource?.id !== undefined && this.hasHiRes(<TrackItem>resource)) {
if (resource.artifactType !== "track") continue;
const trackItem = await MediaItemCache.ensureTrack(resource?.id);
if (trackItem !== undefined) yield trackItem;
}
}
return false;
}
public static hasHiRes(trackItem: TrackItem): boolean {
const tags = trackItem.mediaMetadata?.tags;
if (tags === undefined) return false;
Expand Down

0 comments on commit 85f9cb0

Please sign in to comment.