Skip to content

Commit

Permalink
Fix #96 ensure item being searched is not already HiRes
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Nov 6, 2024
1 parent c85e29f commit 9bf65a9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
1 change: 0 additions & 1 deletion plugins/RealMAX/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const unloadIntercept = intercept(
const maxItem = await MaxTrack.getMaxTrack(queueId);
if (maxItem === false) return;
if (maxItem.id !== undefined && nextQueueId !== maxItem.id) {
await MediaItemCache.ensure(maxItem.id);
if (settings.displayInfoPopups) trace.msg.log(`Found Max quality for ${maxItem.title}! Adding to queue and skipping...`);
actions.playQueue.addNext({ mediaItemIds: [maxItem.id], context: { type: "user" } });
actions.playQueue.moveNext();
Expand Down
8 changes: 4 additions & 4 deletions plugins/_lib/Caches/ExtendedTrackItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export class ExtendedMediaItem {
return this.get(playbackContext.actualProductId);
}

public static async get(trackId?: ItemId) {
if (trackId === undefined) return undefined;
const trackItem = await MediaItemCache.ensure(trackId);
public static async get(itemId?: ItemId) {
if (itemId === undefined) return undefined;
const trackItem = await MediaItemCache.ensure(itemId);
if (trackItem === undefined) return undefined;
return new this(trackId, trackItem);
return new this(itemId, trackItem);
}

public async isrcs() {
Expand Down
27 changes: 15 additions & 12 deletions plugins/_lib/MaxTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ import { fetchIsrcIterable, Resource } from "./api/tidal";
import { AsyncCachable } from "./Caches/AsyncCachable";
import { ExtendedMediaItem } from "./Caches/ExtendedTrackItem";
import { MediaItemCache } from "./Caches/MediaItemCache";
import { ItemId, TrackItem } from "neptune-types/tidal";
import { ItemId, MediaItem, TrackItem } from "neptune-types/tidal";

export type TrackFilter = (trackItem: Resource) => boolean;
export class MaxTrack {
public static getMaxTrack = AsyncCachable(async (itemId: ItemId): Promise<TrackItem | false> => {
for await (const trackItem of this.getTracksFromItemId(itemId, this.hasHiRes)) {
if (trackItem.id !== itemId) return trackItem;
const extTrackItem = await ExtendedMediaItem.get(itemId);
if (extTrackItem === undefined) return false;
if (extTrackItem.tidalTrack.contentType === "track" && this.hasHiRes(extTrackItem.tidalTrack)) return false;

for await (const trackItem of this.getTracksFromMediaItem(extTrackItem, this.hasHiRes)) {
return trackItem;
}
return false;
});
public static getLatestMaxTrack = AsyncCachable(async (itemId: ItemId): Promise<TrackItem | false> => {
const extTrackItem = await ExtendedMediaItem.get(itemId);
if (extTrackItem === undefined) return false;

let currentTrackItem: TrackItem | false = false;
for await (const trackItem of this.getTracksFromItemId(itemId)) {
if (trackItem.id === itemId) continue;
for await (const trackItem of this.getTracksFromMediaItem(extTrackItem)) {
if (currentTrackItem === undefined) {
currentTrackItem = trackItem;
continue;
Expand All @@ -35,18 +41,15 @@ export class MaxTrack {
}
return currentTrackItem;
});
public static async *getTracksFromItemId(itemId: ItemId, filter?: TrackFilter): AsyncGenerator<TrackItem> {
const extTrackItem = await ExtendedMediaItem.get(itemId);
if (extTrackItem === undefined) return;

const { tidalTrack } = extTrackItem;
if (tidalTrack.contentType !== "track") return;
public static async *getTracksFromMediaItem(extMediaItem: ExtendedMediaItem, filter?: TrackFilter): AsyncGenerator<TrackItem> {
const { tidalTrack } = extMediaItem;

const isrcs = await extTrackItem.isrcs();
const isrcs = await extMediaItem.isrcs();
if (isrcs.size === 0) return;

for (const isrc of isrcs) {
for await (const trackItem of this.getTracksFromISRC(isrc, filter)) {
if (trackItem.id === tidalTrack.id) continue;
yield trackItem;
}
}
Expand Down

0 comments on commit 9bf65a9

Please sign in to comment.