Skip to content

Commit

Permalink
Slim down YouTubeBase implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
PyvesB committed Apr 3, 2021
1 parent 9b45867 commit 55ecd94
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 100 deletions.
88 changes: 33 additions & 55 deletions services/youtube/youtube-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,25 @@ const documentation = `
<p>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:
<code>https://www.youtube.com/t/terms</code></p>`

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()
Expand All @@ -56,62 +47,49 @@ 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' },
},
}
)
)
}

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 }
14 changes: 5 additions & 9 deletions services/youtube/youtube-channel-views.service.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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
Expand All @@ -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 })
}
}
14 changes: 5 additions & 9 deletions services/youtube/youtube-comments.service.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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
Expand All @@ -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 })
}
}
2 changes: 1 addition & 1 deletion services/youtube/youtube-comments.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
16 changes: 7 additions & 9 deletions services/youtube/youtube-likes.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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',
Expand All @@ -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
Expand All @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions services/youtube/youtube-likes.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
10 changes: 5 additions & 5 deletions services/youtube/youtube-subscribers.service.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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
Expand All @@ -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,
})
}
}
14 changes: 5 additions & 9 deletions services/youtube/youtube-views.service.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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
Expand All @@ -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 })
}
}
2 changes: 1 addition & 1 deletion services/youtube/youtube-views.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 55ecd94

Please sign in to comment.