From b128a7702a1614350f40cc7676c75aee5abde54a Mon Sep 17 00:00:00 2001 From: ChunkyProgrammer <78101139+ChunkyProgrammer@users.noreply.github.com> Date: Thu, 7 Apr 2022 21:56:37 -0400 Subject: [PATCH] small refactor (#66) * small refactor * use strict equals --- app/helper.js | 34 ++------ app/youtube-grabber.js | 182 +++++++++++------------------------------ package-lock.json | 33 ++------ package.json | 7 +- 4 files changed, 67 insertions(+), 189 deletions(-) diff --git a/app/helper.js b/app/helper.js index aa159f9..b9f138d 100644 --- a/app/helper.js +++ b/app/helper.js @@ -12,9 +12,6 @@ class YoutubeGrabberHelper { }, httpsAgent: httpsAgent }) - - this.cookies = null - this.test = 'hello' } /** @@ -135,25 +132,18 @@ class YoutubeGrabberHelper { subscriberNumber = parseFloat(subscriberSplit[0]) } - let subscriberCount + let subscriberCount = subscriberNumber let verified = false let officialArtist = false if ('ownerBadges' in author) { verified = author.ownerBadges.some((badge) => badge.metadataBadgeRenderer.style === 'BADGE_STYLE_TYPE_VERIFIED') officialArtist = author.ownerBadges.some((badge) => badge.metadataBadgeRenderer.style === 'BADGE_STYLE_TYPE_VERIFIED_ARTIST') } - - switch (subscriberMultiplier) { - case 'k': - subscriberCount = subscriberNumber * 1000 - break - case 'm': - subscriberCount = subscriberNumber * 1000000 - break - default: - subscriberCount = subscriberNumber + if (subscriberMultiplier === 'k') { + subscriberCount *= 1000 + } else if (subscriberMultiplier === 'm') { + subscriberCount *= 1000000 } - return { channelName: channelName, channelId: channelId, @@ -489,20 +479,6 @@ class YoutubeGrabberHelper { } } - /** - * Get the existing status of resource - * @param { string } url The url of youtube resource - * @returns { Promise } Return TRUE if resource is exists - * */ - async isResourceExists(url) { - const response = await YoutubeGrabberHelper.getResource(url) - if (!response) return false - - const $ = cheerio.load(response.data) - const metaTags = $('meta[name="title"]') - return metaTags.length !== 0 - } - async decideUrlRequestType(channelId, urlAppendix, channelIdType) { switch (channelIdType) { case 0: return this.performChannelPageRequestWithFallbacks(channelId, urlAppendix) diff --git a/app/youtube-grabber.js b/app/youtube-grabber.js index d801426..d189df9 100644 --- a/app/youtube-grabber.js +++ b/app/youtube-grabber.js @@ -13,11 +13,7 @@ class YoutubeGrabber { * @param httpAgent * @return { Promise } Return channel information * */ - static async getChannelInfo(payload) { - const channelId = payload.channelId - const channelIdType = payload.channelIdType ?? 0 - const httpAgent = payload.httpAgent ?? null - + static async getChannelInfo({ channelId, channelIdType = 0, httpAgent = null }) { const ytGrabHelp = YoutubeGrabberHelper.create(httpAgent) const decideResponse = await ytGrabHelp.decideUrlRequestType(channelId, 'channels?flow=grid&view=0&pbj=1', channelIdType) const channelPageResponse = decideResponse.response @@ -115,17 +111,11 @@ class YoutubeGrabber { subscriberNumber = parseFloat(subscriberSplit[0]) } - let subscriberCount - - switch (subscriberMultiplier) { - case 'k': - subscriberCount = subscriberNumber * 1000 - break - case 'm': - subscriberCount = subscriberNumber * 1000000 - break - default: - subscriberCount = subscriberNumber + let subscriberCount = subscriberNumber + if (subscriberMultiplier === 'k') { + subscriberCount *= 1000 + } else if (subscriberMultiplier === 'm') { + subscriberCount *= 1000000 } let isVerified = false @@ -161,21 +151,10 @@ class YoutubeGrabber { return channelInfo } - - static async getRelatedChannelsMore(payload) { - const continuation = payload.continuation - const httpAgent = payload.httpAgent ?? null - + + static async getRelatedChannelsMore({ continuation, httpAgent = null }) { const ytGrabHelp = YoutubeGrabberHelper.create(httpAgent) - const urlParams = { - context: { - client: { - clientName: 'WEB', - clientVersion: '2.20201021.03.00', - }, - }, - continuation: continuation - } + const urlParams = this.GetContinuationUrlParams(continuation) const ajaxUrl = 'https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8' const channelPageResponse = await ytGrabHelp.makeChannelPost(ajaxUrl, urlParams) @@ -224,38 +203,19 @@ class YoutubeGrabber { } } - static async getChannelVideos(payload) { - const channelId = payload.channelId - const sortBy = payload.sortBy ?? 'newest' - const channelIdType = payload.channelIdType ?? 0 - const httpAgent = payload.httpAgent ?? null - - switch (sortBy) { - case 'popular': - return await YoutubeChannelFetcher.getChannelVideosPopular(channelId, channelIdType, httpAgent) - case 'newest': - return await YoutubeChannelFetcher.getChannelVideosNewest(channelId, channelIdType, httpAgent) - case 'oldest': - return await YoutubeChannelFetcher.getChannelVideosOldest(channelId, channelIdType, httpAgent) - default: - return await YoutubeChannelFetcher.getChannelVideosNewest(channelId, channelIdType, httpAgent) + static async getChannelVideos({ channelId, sortBy = 'newest', channelIdType = 0, httpAgent = null }) { + if (sortBy === 'popular') { + return await YoutubeChannelFetcher.getChannelVideosPopular(channelId, channelIdType, httpAgent) + } else if (sortBy === 'oldest') { + return await YoutubeChannelFetcher.getChannelVideosOldest(channelId, channelIdType, httpAgent) + } else { // newest + return await YoutubeChannelFetcher.getChannelVideosNewest(channelId, channelIdType, httpAgent) } } - static async getChannelVideosMore(payload) { - const continuation = payload.continuation - const httpAgent = payload.httpAgent - + static async getChannelVideosMore({ continuation, httpAgent = null }) { const ytGrabHelp = YoutubeGrabberHelper.create(httpAgent) - const urlParams = { - context: { - client: { - clientName: 'WEB', - clientVersion: '2.20201021.03.00', - }, - }, - continuation: continuation - } + const urlParams = this.GetContinuationUrlParams(continuation) const ajaxUrl = 'https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8' const channelPageResponse = await ytGrabHelp.makeChannelPost(ajaxUrl, urlParams) @@ -297,39 +257,20 @@ class YoutubeGrabber { } } - static async getChannelPlaylistInfo(payload) { - const channelId = payload.channelId - const sortBy = payload.sortBy ?? 'last' - const channelIdType = payload.channelIdType ?? 0 - const httpAgent = payload.httpAgent ?? null - - switch (sortBy) { - case 'last': - return await YoutubePlaylistFetcher.getChannelPlaylistLast(channelId, channelIdType, httpAgent) - case 'oldest': - console.warn("yt-channel-info: Fetching by oldest isn't available in YouTube any more. This option will be removed in a later update.") - return await YoutubePlaylistFetcher.getChannelPlaylistOldest(channelId, channelIdType, httpAgent) - case 'newest': - return await YoutubePlaylistFetcher.getChannelPlaylistNewest(channelId, channelIdType, httpAgent) - default: - return await YoutubePlaylistFetcher.getChannelPlaylistLast(channelId, channelIdType, httpAgent) + static async getChannelPlaylistInfo({ channelId, sortBy = 'last', channelIdType = 0, httpAgent = null }) { + if (sortBy === 'newest') { + return await YoutubePlaylistFetcher.getChannelPlaylistNewest(channelId, channelIdType, httpAgent) + } else if (sortBy === 'oldest') { + console.warn("yt-channel-info: Fetching by oldest isn't available in YouTube any more. This option will be removed in a later update.") + return await YoutubePlaylistFetcher.getChannelPlaylistOldest(channelId, channelIdType, httpAgent) + } else { // last + return await YoutubePlaylistFetcher.getChannelPlaylistLast(channelId, channelIdType, httpAgent) } } - static async getChannelPlaylistsMore(payload) { - const continuation = payload.continuation - const httpAgent = payload.httpAgent ?? null - + static async getChannelPlaylistsMore({ continuation, httpAgent = null }) { const ytGrabHelp = YoutubeGrabberHelper.create(httpAgent) - const urlParams = { - context: { - client: { - clientName: 'WEB', - clientVersion: '2.20201021.03.00', - }, - }, - continuation: continuation - } + const urlParams = this.GetContinuationUrlParams(continuation) const ajaxUrl = 'https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8' const channelPageResponse = await ytGrabHelp.makeChannelPost(ajaxUrl, urlParams) @@ -372,14 +313,9 @@ class YoutubeGrabber { } } - static async searchChannel(payload) { - const channelId = payload.channelId - const query = payload.query ?? '' - const channelIdType = payload.channelIdType ?? 0 - const httpAgent = payload.httpAgent ?? null - + static async searchChannel({ channelId, query = '', channelIdType = 0, httpAgent = null }) { const ytGrabHelp = YoutubeGrabberHelper.create(httpAgent) - const urlParams = queryString.stringify({ + const urlParams = new URLSearchParams({ query: query, flow: 'grid', view: 0, @@ -443,20 +379,9 @@ class YoutubeGrabber { } } - static async searchChannelMore(payload) { - const continuation = payload.continuation - const httpAgent = payload.httpAgent ?? null - + static async searchChannelMore({ continuation, httpAgent = null }) { const ytGrabHelp = YoutubeGrabberHelper.create(httpAgent) - const urlParams = { - context: { - client: { - clientName: 'WEB', - clientVersion: '2.20201021.03.00', - }, - }, - continuation: continuation - } + const urlParams = this.GetContinuationUrlParams(continuation) const ajaxUrl = 'https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8' const channelPageResponse = await ytGrabHelp.makeChannelPost(ajaxUrl, urlParams) @@ -499,31 +424,17 @@ class YoutubeGrabber { } } - static async getChannelCommunityPosts(payload) { - const channelId = payload.channelId - const channelIdType = payload.channelIdType ?? 0 - const httpAgent = payload.httpAgent ?? null - + static async getChannelCommunityPosts({ channelId, channelIdType = 0, httpAgent = null }) { const ytGrabHelp = YoutubeGrabberHelper.create(httpAgent) const channelPageResponse = await ytGrabHelp.decideUrlRequestType(channelId, 'community', channelIdType) return ytGrabHelp.parseCommunityPage(channelPageResponse.response, channelPageResponse.channelIdType) } - static async getChannelCommunityPostsMore(payload) { - const continuation = payload.continuation - const innerAPIKey = payload.innerTubeApi - const httpAgent = payload.httpAgent ?? null - + static async getChannelCommunityPostsMore({ continuation, innerTubeApi, httpAgent = null}) { const ytGrabHelp = YoutubeGrabberHelper.create(httpAgent) - const channelPageResponse = await ytGrabHelp.makeChannelPost(`https://www.youtube.com/youtubei/v1/browse?key=${innerAPIKey}`, { - context: { - client: { - clientName: 'WEB', - clientVersion: '2.20210314.08.00', - }, - }, - continuation: continuation - }) + const channelPageResponse = await ytGrabHelp.makeChannelPost(`https://www.youtube.com/youtubei/v1/browse?key=${innerAPIKey}`, + this.GetContinuationUrlParams(continuation) + ) if (channelPageResponse.error) { return Promise.reject(channelPageResponse.message) } @@ -532,15 +443,11 @@ class YoutubeGrabber { return { items: ytGrabHelp.createCommunityPostArray(postDataArray), continuation: contValue, - innerTubeApi: innerAPIKey + innerTubeApi: innerTubeApi } } - static async getChannelStats(payload) { - const channelId = payload.channelId - const channelIdType = payload.channelIdType ?? 0 - const httpAgent = payload.httpAgent ?? null - + static async getChannelStats({ channelId, channelIdType = 0, httpAgent = null }) { const ytGrabHelp = YoutubeGrabberHelper.create(httpAgent) const decideResponse = await ytGrabHelp.decideUrlRequestType(channelId, 'about?flow=grid&view=0&pbj=1', channelIdType) const channelPageResponse = decideResponse.response @@ -652,6 +559,17 @@ class YoutubeGrabber { items: homeItems } } + static GetContinuationUrlParams(continuation) { + return { + context: { + client: { + clientName: 'WEB', + clientVersion: '2.20201021.03.00', + }, + }, + continuation: continuation + } + } } module.exports = YoutubeGrabber diff --git a/package-lock.json b/package-lock.json index 378e8af..aa00669 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,7 @@ "version": "3.0.1", "license": "ISC", "dependencies": { - "axios": "^0.21.1", - "querystring": "^0.2.0" + "axios": "^0.26.1" }, "devDependencies": { "eslint": "^7.6.0", @@ -1479,11 +1478,11 @@ "dev": true }, "node_modules/axios": { - "version": "0.21.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.2.tgz", - "integrity": "sha512-87otirqUw3e8CzHTMO+/9kh/FSgXt/eVDvipijwDtEuwbkySWZ9SBm6VEubmJ/kLKEoLQV/POhxXFb66bfekfg==", + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", "dependencies": { - "follow-redirects": "^1.14.0" + "follow-redirects": "^1.14.8" } }, "node_modules/babel-jest": { @@ -4946,15 +4945,6 @@ "node": ">=6" } }, - "node_modules/querystring": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", - "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=", - "deprecated": "The querystring API is considered Legacy. new code should use the URLSearchParams API instead.", - "engines": { - "node": ">=0.4.x" - } - }, "node_modules/react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", @@ -7098,11 +7088,11 @@ "dev": true }, "axios": { - "version": "0.21.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.2.tgz", - "integrity": "sha512-87otirqUw3e8CzHTMO+/9kh/FSgXt/eVDvipijwDtEuwbkySWZ9SBm6VEubmJ/kLKEoLQV/POhxXFb66bfekfg==", + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", "requires": { - "follow-redirects": "^1.14.0" + "follow-redirects": "^1.14.8" } }, "babel-jest": { @@ -9757,11 +9747,6 @@ "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", "dev": true }, - "querystring": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", - "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" - }, "react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", diff --git a/package.json b/package.json index b8ae235..033003b 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "Grab YouTube channel information without any official API", "main": "index.js", "scripts": { - "test": "jest --watchAll --verbose", - "test-ci": "jest --verbose --ci", + "test": "jest --watchAll --verbose --coverage", + "test-ci": "jest --verbose --ci --coverage", "lint-fix": "eslint --fix --ext .js,.ts ./", "lint": "eslint --ext .js,.ts ./" }, @@ -24,8 +24,7 @@ "author": "PrestonFT", "license": "ISC", "dependencies": { - "axios": "^0.21.1", - "querystring": "^0.2.0" + "axios": "^0.26.1" }, "repository": { "type": "git",