Skip to content

Commit

Permalink
mix parser fallback strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Oct 23, 2023
1 parent 95b7b7a commit d564803
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "youtube-sr",
"version": "4.3.8",
"version": "4.3.9",
"description": "Simple package to make YouTube search.",
"files": [
"dist"
Expand Down
13 changes: 9 additions & 4 deletions src/Structures/Playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class Playlist {
thumbnail?: Thumbnail;
videos: Video[];
mix?: boolean;
fake = false;
private _continuation: { api?: string; token?: string; clientVersion?: string } = {};

constructor(data = {}, searchResult = false) {
Expand All @@ -55,15 +56,16 @@ export class Playlist {
this.videoCount = data.videoCount || 0;
this.lastUpdate = data.lastUpdate || null;
this.views = data.views || 0;
this.url = data.url || null;
this.link = data.link || null;
this.url = data.url || data.link || this.id ? `https://www.youtube.com/playlist?list=${this.id}` : null;
this.link = data.link || data.url || null;
this.channel = data.author || null;
this.thumbnail = new Thumbnail(data.thumbnail || {});
this.videos = data.videos || [];
this._continuation.api = data.continuation?.api ?? null;
this._continuation.token = data.continuation?.token ?? null;
this._continuation.clientVersion = data.continuation?.clientVersion ?? "<important data>";
this.mix = data.mix || false;
this.fake = Boolean(data.fake);
}

private _patchSearch(data: any) {
Expand All @@ -73,10 +75,12 @@ export class Playlist {
this.channel = data.channel || null;
this.videos = data.videos || [];
this.videoCount = data.videos?.length || 0;
this.url = this.id ? `https://www.youtube.com/playlist?list=${this.id}` : null;
this.link = data.link || null;
this.url = data.url || data.link || this.id ? `https://www.youtube.com/playlist?list=${this.id}` : null;
this.link = data.link || data.url || null;
this.lastUpdate = null;
this.views = 0;
this.mix = data.mix || false;
this.fake = Boolean(data.fake);
}

/**
Expand Down Expand Up @@ -144,6 +148,7 @@ export class Playlist {
id: this.channel.id,
icon: this.channel?.iconURL?.()
},
mix: this.mix,
url: this.url,
videos: this.videos
};
Expand Down
3 changes: 2 additions & 1 deletion src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ class Util {
thumbnail: videos[0]?.thumbnail?.toJSON() || null,
channel: {
name: data.ownerName.simpleText
}
},
mix: true
},
true
);
Expand Down
34 changes: 34 additions & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,40 @@ class YouTube {
const html = await Util.getHTML(`${url}&hl=en`, options && options.requestOptions);
const res = resolved.mix ? Util.getMix(html) : Util.getPlaylist(html, options && options.limit);

// fallback
try {
if (!res && resolved.mix) {
const videoId = new URL(url).searchParams.get("v");
if (videoId) {
const vid: Video = await YouTube.getVideo(`https://www.youtube.com/watch?v=${videoId}`).catch(() => null);
if (vid) {
// return fake playlist when mix could not be parsed
return new Playlist(
{
id: vid.id,
title: `Mix - ${vid.title}`,
videoCount: 1,
lastUpdate: null,
views: vid.views,
url: `https://www.youtube.com/watch?v=${vid.id}`,
link: `https://www.youtube.com/watch?v=${vid.id}`,
channel: {
name: "youtube-sr"
},
thumbnail: vid.thumbnail?.toJSON() || null,
videos: [vid],
mix: true,
fake: true
},
true
);
}
}
}
} catch {
//
}

if (res && res instanceof Playlist && options.fetchAll) {
return await res.fetch(options && options.limit).catch(() => res);
}
Expand Down

0 comments on commit d564803

Please sign in to comment.