Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
small refactor (#66)
Browse files Browse the repository at this point in the history
* small refactor

* use strict equals
  • Loading branch information
ChunkyProgrammer authored Apr 8, 2022
1 parent 73850fa commit b128a77
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 189 deletions.
34 changes: 5 additions & 29 deletions app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class YoutubeGrabberHelper {
},
httpsAgent: httpsAgent
})

this.cookies = null
this.test = 'hello'
}

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -489,20 +479,6 @@ class YoutubeGrabberHelper {
}
}

/**
* Get the existing status of resource
* @param { string } url The url of youtube resource
* @returns { Promise<boolean> } 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)
Expand Down
182 changes: 50 additions & 132 deletions app/youtube-grabber.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ class YoutubeGrabber {
* @param httpAgent
* @return { Promise<Object> } 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Loading

0 comments on commit b128a77

Please sign in to comment.