Skip to content

Commit

Permalink
Merge pull request #557 from lovegaoshi/dev
Browse files Browse the repository at this point in the history
fix: ytbi thumbnail resolve
  • Loading branch information
lovegaoshi authored Sep 11, 2024
2 parents 5610aba + a4c4020 commit da671dd
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 57 deletions.
2 changes: 0 additions & 2 deletions src/hooks/useSetupPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import useInitializeStore from '@stores/initializeStores';
import { IntentData } from '@enums/Intent';
import { useNoxSetting } from '@stores/useApp';
import usePlayStore from './usePlayStore';
import { awaitYtbiSetup } from '../utils/mediafetch/ytbi';

const { NoxAndroidAutoModule } = NativeModules;

Expand All @@ -25,7 +24,6 @@ export default ({ intentData }: NoxComponent.AppProps) => {
const { checkPlayStoreUpdates } = usePlayStore();

useEffect(() => {
awaitYtbiSetup();
let unmounted = false;
(async () => {
const {
Expand Down
34 changes: 28 additions & 6 deletions src/utils/mediafetch/resolveURL.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import steriatkFetch from './steriatk';
import biliaudioFetch from './biliaudio';
import ytbvideoFetch from '@utils/mediafetch/ytbvideo';
import ytbvideoFetch, { fetchAudioInfo } from '@utils/mediafetch/ytbvideo';
import bililiveFetch from './bililive';
import biliBangumiFetch from './biliBangumi';
import localFetch from '@utils/mediafetch/local';
Expand All @@ -16,11 +16,11 @@ import { NULL_TRACK } from '@objects/Song';

const MUSICFREESources: NoxMedia.SongSource[] = Object.values(MUSICFREE);

type regResolve = NoxUtils.RegexMatchResolve<
type RegResolve = NoxUtils.RegexMatchResolve<
Promise<NoxNetwork.ParsedNoxMediaURL>
>;

const regexResolveURLs: regResolve = [
const regexResolveURLs: RegResolve = [
[steriatkFetch.regexResolveURLMatch, steriatkFetch.resolveURL],
[biliaudioFetch.regexResolveURLMatch, biliaudioFetch.resolveURL],
[ytbvideoFetch.regexResolveURLMatch, ytbvideoFetch.resolveURL],
Expand All @@ -32,6 +32,19 @@ const regexResolveURLs: regResolve = [
[alistFetch.regexResolveURLMatch, alistFetch.resolveURL],
];

const regexRefreshMetadata: NoxUtils.RegexMatchResolve<
Promise<Partial<NoxMedia.Song>>
> = [
[
ytbvideoFetch.regexResolveURLMatch,
async s => (await fetchAudioInfo(s.bvid))[0],
],
[
ytbvideoFetch.regexResolveURLMatch2,
async s => (await fetchAudioInfo(s.bvid))[0],
],
];

interface FetchPlayUrl {
song: NoxMedia.Song;
iOS?: boolean;
Expand All @@ -49,7 +62,7 @@ export const fetchPlayUrlPromise = async ({
const bvid = song.bvid;
const cid = song.id;
const resolveUrlArray = regexResolveURLs;
const regexResolveURLsWrapped: regResolve = resolveUrlArray.map(entry => [
const regexResolveURLsWrapped: RegResolve = resolveUrlArray.map(entry => [
entry[0],
(song: NoxMedia.Song) => entry[1](song, iOS),
]);
Expand Down Expand Up @@ -79,9 +92,18 @@ export const fetchPlayUrlPromise = async ({
export const refreshMetadata = async (
song: NoxMedia.Song
): Promise<Partial<NoxMedia.Song>> => {
const metadata = await fetchPlayUrlPromise({ song });
logger.debug(`[refreshMetadata] ${song.id}`);
const metadata = await regexMatchOperations({
song,
regexOperations: regexRefreshMetadata,
regexMatching: song => song.id,
fallback: async (): Promise<Partial<NoxMedia.Song>> => {
logger.warn(`[refreshMetadata] ${song.id} did not match regex`);
return {};
},
});
return {
...(metadata.cover && { cover: metadata.cover }),
...metadata,
metadataOnLoad: false,
};
};
Expand Down
1 change: 0 additions & 1 deletion src/utils/mediafetch/ytbSearch.ytbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const ytbiVideoToNoxSong = (val: Video) => {
lyric: '',
page: 1,
duration: val.duration.seconds,
album: 'ytbSearch',
source: Source.ytbvideo,
metadataOnLoad: true,
});
Expand Down
23 changes: 8 additions & 15 deletions src/utils/mediafetch/ytbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,16 @@ global.CustomEvent = CustomEvent as any;

// === END === Making Youtube.js work

let ytClient: Promise<Innertube>;

export const resetYtClient = (retrievePlayer = false) => {
ytClient = Innertube.create({
retrieve_player: retrievePlayer,
enable_session_cache: false,
generate_session_locally: false,
client_type: ClientType.IOS,
});
return ytClient;
};

resetYtClient();
const ytClient = Innertube.create({
retrieve_player: true,
enable_session_cache: false,
generate_session_locally: false,
client_type: ClientType.IOS,
});

export default () => ytClient;
export default ytClient;

export const ytClientWeb: Promise<Innertube> = Innertube.create({
export const ytClientWeb = Innertube.create({
retrieve_player: false,
enable_session_cache: false,
generate_session_locally: false,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/mediafetch/ytbvideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from './ytbvideo.muse';

const resolveURL = (song: NoxMedia.Song, iOS = false) =>
resolveURLYtbi(song, iOS).catch(() => resolveURLMuse(song));
resolveURLMuse(song).catch(() => resolveURLYtbi(song, iOS));

export const fetchAudioInfo = (bvid: string, progressEmitter?: () => void) =>
biliApiLimiter.schedule(() => {
Expand Down
45 changes: 13 additions & 32 deletions src/utils/mediafetch/ytbvideo.ytbi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SongTS from '@objects/Song';
import { Source } from '@enums/MediaFetch';
import { logger } from '@utils/Logger';
import ytClient, { resetYtClient } from '@utils/mediafetch/ytbi';
import ytClient, { ytClientWeb } from '@utils/mediafetch/ytbi';
import { isIOS } from '@utils/RNUtils';
import { Thumbnail } from 'youtubei.js/dist/src/parser/misc';

Expand All @@ -10,47 +10,28 @@ const getHiResThumbnail = (thumbnails?: Thumbnail[]) => {
return thumbnails.sort((a, b) => b.width - a.width)[0]!.url;
};

interface ResolveURL {
song: NoxMedia.Song;
iOS?: boolean;
reset?: boolean;
}

const _resolveURL = async ({
song,
iOS = false,
reset = false,
}: ResolveURL): Promise<NoxNetwork.ParsedNoxMediaURL> => {
export const resolveURL = async (song: NoxMedia.Song, iOS = false) => {
logger.debug(`[ytbi.js] fetch YTB playURL promise:${song.bvid}`);
const yt = await ytClient();
const yt = await ytClient;
const extractedVideoInfo = await yt.getBasicInfo(song.bvid, 'IOS');
const maxAudioQualityStream = extractedVideoInfo.chooseFormat({
quality: 'best',
type: 'audio',
});
const thumbnails = extractedVideoInfo.basic_info.thumbnail;
const url =
iOS && isIOS && extractedVideoInfo.streaming_data?.hls_manifest_url
? extractedVideoInfo.streaming_data?.hls_manifest_url
: maxAudioQualityStream.decipher(yt.actions.session.player);
if (url || reset) {
return {
url,
cover: getHiResThumbnail(thumbnails),
loudness: maxAudioQualityStream.loudness_db,
};
}
logger.warn('[ytbi] resetting ytClient to retrive player. This takes time.');
await resetYtClient();
return _resolveURL({ song, iOS, reset: true });
return {
url:
iOS && isIOS && extractedVideoInfo.streaming_data?.hls_manifest_url
? extractedVideoInfo.streaming_data?.hls_manifest_url
: maxAudioQualityStream.decipher(yt.actions.session.player),
cover: getHiResThumbnail(thumbnails),
loudness: maxAudioQualityStream.loudness_db,
};
};

export const resolveURL = async (song: NoxMedia.Song, iOS = false) =>
_resolveURL({ song, iOS });

export const fetchAudioInfo = async (sid: string) => {
const yt = await ytClient();
const videoInfo = (await yt.getBasicInfo(sid, 'IOS')).basic_info;
const yt = await ytClientWeb;
const videoInfo = (await yt.getBasicInfo(sid)).basic_info;
return [
SongTS({
cid: `${Source.ytbvideo}-${sid}`,
Expand Down

0 comments on commit da671dd

Please sign in to comment.