From 55ecd9416dce02da4b5f83fe2824c48c124d65bc Mon Sep 17 00:00:00 2001 From: PyvesB Date: Sat, 3 Apr 2021 12:57:11 +0100 Subject: [PATCH] Slim down YouTubeBase implementation --- services/youtube/youtube-base.js | 88 +++++++------------ .../youtube/youtube-channel-views.service.js | 14 ++- services/youtube/youtube-comments.service.js | 14 ++- services/youtube/youtube-comments.tester.js | 2 +- services/youtube/youtube-likes.service.js | 16 ++-- services/youtube/youtube-likes.tester.js | 4 +- .../youtube/youtube-subscribers.service.js | 10 +-- services/youtube/youtube-views.service.js | 14 ++- services/youtube/youtube-views.tester.js | 2 +- 9 files changed, 64 insertions(+), 100 deletions(-) diff --git a/services/youtube/youtube-base.js b/services/youtube/youtube-base.js index 4b1ab16590cc3..ad2bfd83fc3c6 100644 --- a/services/youtube/youtube-base.js +++ b/services/youtube/youtube-base.js @@ -9,34 +9,25 @@ const documentation = `

By using the YouTube badges provided by Shields.io, you are agreeing to be bound by the YouTube Terms of Service. These can be found here: https://www.youtube.com/t/terms

` -const channelSchema = Joi.object({ +const schema = Joi.object({ pageInfo: Joi.object({ totalResults: nonNegativeInteger, resultsPerPage: nonNegativeInteger, }).required(), items: Joi.array().items( Joi.object({ - statistics: Joi.object({ - viewCount: nonNegativeInteger, - subscriberCount: nonNegativeInteger, - }), - }) - ), -}).required() - -const videoSchema = Joi.object({ - pageInfo: Joi.object({ - totalResults: nonNegativeInteger, - resultsPerPage: nonNegativeInteger, - }).required(), - items: Joi.array().items( - Joi.object({ - statistics: Joi.object({ - viewCount: nonNegativeInteger, - likeCount: nonNegativeInteger, - dislikeCount: nonNegativeInteger, - commentCount: nonNegativeInteger, - }), + statistics: Joi.alternatives( + Joi.object({ + viewCount: nonNegativeInteger, + likeCount: nonNegativeInteger, + dislikeCount: nonNegativeInteger, + commentCount: nonNegativeInteger, + }), + Joi.object({ + viewCount: nonNegativeInteger, + subscriberCount: nonNegativeInteger, + }) + ), }) ), }).required() @@ -56,40 +47,24 @@ class YouTubeBase extends BaseJsonService { namedLogo: 'youtube', } - static renderSingleStat({ statistics, statisticName, channelId, videoId }) { - let link = '' - if (channelId) { - link = `https://www.youtube.com/channel/${encodeURIComponent(channelId)}` - } - if (videoId) { - link = `https://www.youtube.com/watch?v=${encodeURIComponent(videoId)}` - } + static renderSingleStat({ statistics, statisticName, id }) { return { label: `${statisticName}s`, message: metric(statistics[`${statisticName}Count`]), style: 'social', - link, + link: `https://www.youtube.com/${this.type}/${encodeURIComponent(id)}`, } } - async fetch({ channelId, videoId }) { - let schema, url - if (channelId) { - schema = channelSchema - url = 'https://www.googleapis.com/youtube/v3/channels' - } - if (videoId) { - schema = videoSchema - url = `https://www.googleapis.com/youtube/v3/videos` - } + async fetch({ id }) { return this._requestJson( this.authHelper.withQueryStringAuth( { passKey: 'key' }, { schema, - url, + url: `https://www.googleapis.com/youtube/v3/${this.constructor.type}s`, options: { - qs: { id: channelId || videoId, part: 'statistics' }, + qs: { id, part: 'statistics' }, }, } ) @@ -97,21 +72,24 @@ class YouTubeBase extends BaseJsonService { } async handle({ channelId, videoId }, queryParams) { - const json = await this.fetch({ channelId, videoId }) + const id = channelId || videoId + const json = await this.fetch({ id }) if (json.pageInfo.totalResults === 0) { - if (channelId) { - throw new NotFound({ prettyMessage: 'channel not found' }) - } - if (videoId) { - throw new NotFound({ prettyMessage: 'video not found' }) - } + throw new NotFound({ + prettyMessage: `${this.constructor.type} not found`, + }) } const statistics = json.items[0].statistics - return this.constructor.render( - { statistics, channelId, videoId }, - queryParams - ) + return this.constructor.render({ statistics, id }, queryParams) } } -module.exports = { documentation, YouTubeBase } +class YouTubeVideoBase extends YouTubeBase { + static type = 'video' +} + +class YouTubeChannelBase extends YouTubeBase { + static type = 'channel' +} + +module.exports = { documentation, YouTubeVideoBase, YouTubeChannelBase } diff --git a/services/youtube/youtube-channel-views.service.js b/services/youtube/youtube-channel-views.service.js index 936f3f44a1e3f..27712d5c83ebf 100644 --- a/services/youtube/youtube-channel-views.service.js +++ b/services/youtube/youtube-channel-views.service.js @@ -1,8 +1,8 @@ 'use strict' -const { documentation, YouTubeBase } = require('./youtube-base') +const { documentation, YouTubeChannelBase } = require('./youtube-base') -module.exports = class YouTubeChannelViews extends YouTubeBase { +module.exports = class YouTubeChannelViews extends YouTubeChannelBase { static route = { base: 'youtube/channel/views', pattern: ':channelId', @@ -11,7 +11,7 @@ module.exports = class YouTubeChannelViews extends YouTubeBase { static get examples() { const preview = this.render({ statistics: { viewCount: 30543 }, - channelId: 'UC8butISFwT-Wl7EV0hUK0BQ', + id: 'UC8butISFwT-Wl7EV0hUK0BQ', }) // link[] is not allowed in examples delete preview.link @@ -25,11 +25,7 @@ module.exports = class YouTubeChannelViews extends YouTubeBase { ] } - static render({ statistics, channelId }) { - return super.renderSingleStat({ - statistics, - statisticName: 'view', - channelId, - }) + static render({ statistics, id }) { + return super.renderSingleStat({ statistics, statisticName: 'view', id }) } } diff --git a/services/youtube/youtube-comments.service.js b/services/youtube/youtube-comments.service.js index 000a03a533eee..679605684cc6a 100644 --- a/services/youtube/youtube-comments.service.js +++ b/services/youtube/youtube-comments.service.js @@ -1,8 +1,8 @@ 'use strict' -const { documentation, YouTubeBase } = require('./youtube-base') +const { documentation, YouTubeVideoBase } = require('./youtube-base') -module.exports = class YouTubeComments extends YouTubeBase { +module.exports = class YouTubeComments extends YouTubeVideoBase { static route = { base: 'youtube/comments', pattern: ':videoId', @@ -11,7 +11,7 @@ module.exports = class YouTubeComments extends YouTubeBase { static get examples() { const preview = this.render({ statistics: { commentCount: 209 }, - videoId: 'wGJHwc5ksMA', + id: 'wGJHwc5ksMA', }) // link[] is not allowed in examples delete preview.link @@ -25,11 +25,7 @@ module.exports = class YouTubeComments extends YouTubeBase { ] } - static render({ statistics, videoId }) { - return super.renderSingleStat({ - statistics, - statisticName: 'comment', - videoId, - }) + static render({ statistics, id }) { + return super.renderSingleStat({ statistics, statisticName: 'comment', id }) } } diff --git a/services/youtube/youtube-comments.tester.js b/services/youtube/youtube-comments.tester.js index 0d9930a11810a..dfdbde45d49c2 100644 --- a/services/youtube/youtube-comments.tester.js +++ b/services/youtube/youtube-comments.tester.js @@ -12,7 +12,7 @@ t.create('video comment count') label: 'comments', message: isMetric, color: 'red', - link: ['https://www.youtube.com/watch?v=wGJHwc5ksMA'], + link: ['https://www.youtube.com/video/wGJHwc5ksMA'], }) t.create('video not found') diff --git a/services/youtube/youtube-likes.service.js b/services/youtube/youtube-likes.service.js index 211a8d60e4bbb..e2133cd392c1a 100644 --- a/services/youtube/youtube-likes.service.js +++ b/services/youtube/youtube-likes.service.js @@ -2,7 +2,7 @@ const Joi = require('joi') const { metric } = require('../text-formatters') -const { documentation, YouTubeBase } = require('./youtube-base') +const { documentation, YouTubeVideoBase } = require('./youtube-base') const documentationWithDislikes = ` ${documentation} @@ -16,7 +16,7 @@ const queryParamSchema = Joi.object({ withDislikes: Joi.equal(''), }).required() -module.exports = class YouTubeLikes extends YouTubeBase { +module.exports = class YouTubeLikes extends YouTubeVideoBase { static route = { base: 'youtube/likes', pattern: ':videoId', @@ -26,16 +26,14 @@ module.exports = class YouTubeLikes extends YouTubeBase { static get examples() { const previewLikes = this.render({ statistics: { likeCount: 7 }, - videoId: 'abBdk8bSPKU', + id: 'abBdk8bSPKU', }) const previewVotes = this.render( { statistics: { likeCount: 10236, dislikeCount: 396 }, - videoId: 'pU9Q6oiQNd0', + id: 'pU9Q6oiQNd0', }, - { - withDislikes: '', - } + { withDislikes: '' } ) // link[] is not allowed in examples delete previewLikes.link @@ -59,11 +57,11 @@ module.exports = class YouTubeLikes extends YouTubeBase { ] } - static render({ statistics, videoId }, queryParams) { + static render({ statistics, id }, queryParams) { let renderedBadge = super.renderSingleStat({ statistics, statisticName: 'like', - videoId, + id, }) if (queryParams && typeof queryParams.withDislikes !== 'undefined') { renderedBadge = { diff --git a/services/youtube/youtube-likes.tester.js b/services/youtube/youtube-likes.tester.js index cd4adf86b9c44..5c266e0987323 100644 --- a/services/youtube/youtube-likes.tester.js +++ b/services/youtube/youtube-likes.tester.js @@ -13,7 +13,7 @@ t.create('video like count') label: 'likes', message: isMetric, color: 'red', - link: ['https://www.youtube.com/watch?v=pU9Q6oiQNd0'], + link: ['https://www.youtube.com/video/pU9Q6oiQNd0'], }) t.create('video vote count') @@ -25,7 +25,7 @@ t.create('video vote count') /^([1-9][0-9]*[kMGTPEZY]?|[1-9]\.[1-9][kMGTPEZY]) 👍 ([1-9][0-9]*[kMGTPEZY]?|[1-9]\.[1-9][kMGTPEZY]) 👎$/ ), color: 'red', - link: ['https://www.youtube.com/watch?v=pU9Q6oiQNd0'], + link: ['https://www.youtube.com/video/pU9Q6oiQNd0'], }) t.create('video not found') diff --git a/services/youtube/youtube-subscribers.service.js b/services/youtube/youtube-subscribers.service.js index 1fe1e3ed25780..408b451bae908 100644 --- a/services/youtube/youtube-subscribers.service.js +++ b/services/youtube/youtube-subscribers.service.js @@ -1,8 +1,8 @@ 'use strict' -const { documentation, YouTubeBase } = require('./youtube-base') +const { documentation, YouTubeChannelBase } = require('./youtube-base') -module.exports = class YouTubeSubscribes extends YouTubeBase { +module.exports = class YouTubeSubscribes extends YouTubeChannelBase { static route = { base: 'youtube/channel/subscribers', pattern: ':channelId', @@ -11,7 +11,7 @@ module.exports = class YouTubeSubscribes extends YouTubeBase { static get examples() { const preview = this.render({ statistics: { subscriberCount: 14577 }, - channelId: 'UC8butISFwT-Wl7EV0hUK0BQ', + id: 'UC8butISFwT-Wl7EV0hUK0BQ', }) // link[] is not allowed in examples delete preview.link @@ -25,11 +25,11 @@ module.exports = class YouTubeSubscribes extends YouTubeBase { ] } - static render({ statistics, channelId }) { + static render({ statistics, id }) { return super.renderSingleStat({ statistics, statisticName: 'subscriber', - channelId, + id, }) } } diff --git a/services/youtube/youtube-views.service.js b/services/youtube/youtube-views.service.js index 2516df780b7ce..5259e6818add2 100644 --- a/services/youtube/youtube-views.service.js +++ b/services/youtube/youtube-views.service.js @@ -1,8 +1,8 @@ 'use strict' -const { documentation, YouTubeBase } = require('./youtube-base') +const { documentation, YouTubeVideoBase } = require('./youtube-base') -module.exports = class YouTubeViews extends YouTubeBase { +module.exports = class YouTubeViews extends YouTubeVideoBase { static route = { base: 'youtube/views', pattern: ':videoId', @@ -11,7 +11,7 @@ module.exports = class YouTubeViews extends YouTubeBase { static get examples() { const preview = this.render({ statistics: { viewCount: 14577 }, - videoId: 'abBdk8bSPKU', + id: 'abBdk8bSPKU', }) // link[] is not allowed in examples delete preview.link @@ -25,11 +25,7 @@ module.exports = class YouTubeViews extends YouTubeBase { ] } - static render({ statistics, videoId }) { - return super.renderSingleStat({ - statistics, - statisticName: 'view', - videoId, - }) + static render({ statistics, id }) { + return super.renderSingleStat({ statistics, statisticName: 'view', id }) } } diff --git a/services/youtube/youtube-views.tester.js b/services/youtube/youtube-views.tester.js index 1c44b4b0ae8f9..af7c036daff38 100644 --- a/services/youtube/youtube-views.tester.js +++ b/services/youtube/youtube-views.tester.js @@ -12,7 +12,7 @@ t.create('video view count') label: 'views', message: isMetric, color: 'red', - link: ['https://www.youtube.com/watch?v=abBdk8bSPKU'], + link: ['https://www.youtube.com/video/abBdk8bSPKU'], }) t.create('video not found')