Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
Add only official YT Music playlist option
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Mar 31, 2021
1 parent 34058d2 commit fa905e7
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 32 deletions.
5 changes: 4 additions & 1 deletion dist/examples/searchPlaylists.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const src_1 = require("../src");
const main = () => src_1.default.searchPlaylists('Jazz', { lang: 'fr', country: 'FR' });
const main = () => src_1.default.searchPlaylists('Dubstep', {
lang: 'en',
onlyOfficialPlaylists: true,
});
main().then((results) => console.log(results));
2 changes: 1 addition & 1 deletion dist/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ declare const _default: {
}[];
};
};
}) => import("./models").PlaylistPreview[];
}, onlyOfficialPlaylists: boolean) => import("./models").PlaylistPreview[];
parsePlaylist: (body: {
contents: {
singleColumnBrowseResultsRenderer: {
Expand Down
2 changes: 1 addition & 1 deletion dist/src/parsers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export declare const parsePlaylistsSearchResults: (content: {
};
};
};
}) => PlaylistPreview | null;
}, onlyOfficialPlaylists: boolean) => PlaylistPreview | null;
export declare const parseMusicFromPlaylist: (content: {
musicResponsiveListItemRenderer: {
thumbnail: {
Expand Down
8 changes: 7 additions & 1 deletion dist/src/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,18 @@ const parseSuggestion = (content) => {
};
};
exports.parseSuggestion = parseSuggestion;
const parsePlaylistsSearchResults = (content) => {
const parsePlaylistsSearchResults = (content, onlyOfficialPlaylists) => {
var _a;
if (!content.musicResponsiveListItemRenderer.navigationEndpoint.browseEndpoint
.browseId) {
return null;
}
if (onlyOfficialPlaylists &&
content.musicResponsiveListItemRenderer.flexColumns[1]
.musicResponsiveListItemFlexColumnRenderer.text.runs[0].text !==
'YouTube Music') {
return null;
}
return {
title: content.musicResponsiveListItemRenderer.flexColumns[0]
.musicResponsiveListItemFlexColumnRenderer.text.runs[0].text,
Expand Down
3 changes: 2 additions & 1 deletion dist/src/searchPlaylists.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export declare const parsePlaylistsSearchBody: (body: {
}[];
};
};
}) => PlaylistPreview[];
}, onlyOfficialPlaylists: boolean) => PlaylistPreview[];
export default function searchPlaylists(query: string, options?: {
lang?: string;
country?: string;
onlyOfficialPlaylists?: boolean;
}): Promise<PlaylistPreview[]>;
8 changes: 4 additions & 4 deletions dist/src/searchPlaylists.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ exports.parsePlaylistsSearchBody = void 0;
const got_1 = require("got");
const context_1 = require("./context");
const parsers_1 = require("./parsers");
const parsePlaylistsSearchBody = (body) => {
const parsePlaylistsSearchBody = (body, onlyOfficialPlaylists) => {
const { contents, } = body.contents.sectionListRenderer.contents[0].musicShelfRenderer;
const results = [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
contents.forEach((content) => {
try {
const playlist = parsers_1.parsePlaylistsSearchResults(content);
const playlist = parsers_1.parsePlaylistsSearchResults(content, onlyOfficialPlaylists);
if (playlist) {
results.push(playlist);
}
Expand All @@ -32,7 +32,7 @@ const parsePlaylistsSearchBody = (body) => {
};
exports.parsePlaylistsSearchBody = parsePlaylistsSearchBody;
function searchPlaylists(query, options) {
var _a;
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const response = yield got_1.default.post('https://music.youtube.com/youtubei/v1/search?alt=json&key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30', {
json: Object.assign(Object.assign({}, context_1.default.body(options === null || options === void 0 ? void 0 : options.lang, options === null || options === void 0 ? void 0 : options.country)), { params: 'EgWKAQIoAWoKEAoQAxAEEAUQCQ%3D%3D', query }),
Expand All @@ -43,7 +43,7 @@ function searchPlaylists(query, options) {
},
});
try {
return exports.parsePlaylistsSearchBody(JSON.parse(response.body));
return exports.parsePlaylistsSearchBody(JSON.parse(response.body), (_b = options === null || options === void 0 ? void 0 : options.onlyOfficialPlaylists) !== null && _b !== void 0 ? _b : false);
}
catch (e) {
console.error(e);
Expand Down
5 changes: 4 additions & 1 deletion examples/searchPlaylists.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import ytMusic from '../src';

const main = () =>
ytMusic.searchPlaylists('Jazz', { lang: 'fr', country: 'FR' });
ytMusic.searchPlaylists('Dubstep', {
lang: 'en',
onlyOfficialPlaylists: true,
});

main().then((results) => console.log(results));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "node-youtube-music",
"description": "Unofficial YouTube Music API for Node.js",
"version": "0.3.0",
"version": "0.3.1",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"repository": "https://github.com/baptisteArno/node-youtube-music",
Expand Down
37 changes: 24 additions & 13 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,38 @@ export const parseSuggestion = (content: {
};
};

export const parsePlaylistsSearchResults = (content: {
musicResponsiveListItemRenderer: {
flexColumns: {
musicResponsiveListItemFlexColumnRenderer: {
text: { runs: { text: string }[] };
};
}[];
thumbnail: {
musicThumbnailRenderer: {
thumbnail: { thumbnails: { url: string | undefined }[] };
export const parsePlaylistsSearchResults = (
content: {
musicResponsiveListItemRenderer: {
flexColumns: {
musicResponsiveListItemFlexColumnRenderer: {
text: { runs: { text: string }[] };
};
}[];
thumbnail: {
musicThumbnailRenderer: {
thumbnail: { thumbnails: { url: string | undefined }[] };
};
};
navigationEndpoint: { browseEndpoint: { browseId: string } };
};
navigationEndpoint: { browseEndpoint: { browseId: string } };
};
}): PlaylistPreview | null => {
},
onlyOfficialPlaylists: boolean
): PlaylistPreview | null => {
if (
!content.musicResponsiveListItemRenderer.navigationEndpoint.browseEndpoint
.browseId
) {
return null;
}
if (
onlyOfficialPlaylists &&
content.musicResponsiveListItemRenderer.flexColumns[1]
.musicResponsiveListItemFlexColumnRenderer.text.runs[0].text !==
'YouTube Music'
) {
return null;
}
return {
title:
content.musicResponsiveListItemRenderer.flexColumns[0]
Expand Down
26 changes: 18 additions & 8 deletions src/searchPlaylists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import context from './context';
import { PlaylistPreview } from './models';
import { parsePlaylistsSearchResults } from './parsers';

export const parsePlaylistsSearchBody = (body: {
contents: {
sectionListRenderer: {
contents: { musicShelfRenderer: { contents: [] } }[];
export const parsePlaylistsSearchBody = (
body: {
contents: {
sectionListRenderer: {
contents: { musicShelfRenderer: { contents: [] } }[];
};
};
};
}): PlaylistPreview[] => {
},
onlyOfficialPlaylists: boolean
): PlaylistPreview[] => {
const {
contents,
} = body.contents.sectionListRenderer.contents[0].musicShelfRenderer;
Expand All @@ -18,7 +21,10 @@ export const parsePlaylistsSearchBody = (body: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
contents.forEach((content: any) => {
try {
const playlist = parsePlaylistsSearchResults(content);
const playlist = parsePlaylistsSearchResults(
content,
onlyOfficialPlaylists
);
if (playlist) {
results.push(playlist);
}
Expand All @@ -34,6 +40,7 @@ export default async function searchPlaylists(
options?: {
lang?: string;
country?: string;
onlyOfficialPlaylists?: boolean;
}
): Promise<PlaylistPreview[]> {
const response = await got.post(
Expand All @@ -53,7 +60,10 @@ export default async function searchPlaylists(
}
);
try {
return parsePlaylistsSearchBody(JSON.parse(response.body));
return parsePlaylistsSearchBody(
JSON.parse(response.body),
options?.onlyOfficialPlaylists ?? false
);
} catch (e) {
console.error(e);
return [];
Expand Down

0 comments on commit fa905e7

Please sign in to comment.