@@ -10,18 +10,26 @@ const documentation = `
1010<code>https://www.youtube.com/t/terms</code></p>`
1111
1212const schema = Joi . object ( {
13- items : Joi . array ( )
14- . items (
15- Joi . object ( {
16- statistics : Joi . object ( {
13+ pageInfo : Joi . object ( {
14+ totalResults : nonNegativeInteger ,
15+ resultsPerPage : nonNegativeInteger ,
16+ } ) . required ( ) ,
17+ items : Joi . array ( ) . items (
18+ Joi . object ( {
19+ statistics : Joi . alternatives (
20+ Joi . object ( {
1721 viewCount : nonNegativeInteger ,
1822 likeCount : nonNegativeInteger ,
1923 dislikeCount : nonNegativeInteger ,
2024 commentCount : nonNegativeInteger ,
21- } ) . required ( ) ,
22- } )
23- )
24- . required ( ) ,
25+ } ) ,
26+ Joi . object ( {
27+ viewCount : nonNegativeInteger ,
28+ subscriberCount : nonNegativeInteger ,
29+ } )
30+ ) ,
31+ } )
32+ ) ,
2533} ) . required ( )
2634
2735class YouTubeBase extends BaseJsonService {
@@ -39,38 +47,49 @@ class YouTubeBase extends BaseJsonService {
3947 namedLogo : 'youtube' ,
4048 }
4149
42- static renderSingleStat ( { statistics, statisticName, videoId } ) {
50+ static renderSingleStat ( { statistics, statisticName, id } ) {
4351 return {
4452 label : `${ statisticName } s` ,
4553 message : metric ( statistics [ `${ statisticName } Count` ] ) ,
4654 style : 'social' ,
47- link : `https://www.youtube.com/watch?v= ${ encodeURIComponent ( videoId ) } ` ,
55+ link : `https://www.youtube.com/${ this . type } / ${ encodeURIComponent ( id ) } ` ,
4856 }
4957 }
5058
51- async fetch ( { videoId } ) {
59+ async fetch ( { id } ) {
5260 return this . _requestJson (
5361 this . authHelper . withQueryStringAuth (
5462 { passKey : 'key' } ,
5563 {
5664 schema,
57- url : ' https://www.googleapis.com/youtube/v3/videos' ,
65+ url : ` https://www.googleapis.com/youtube/v3/${ this . constructor . type } s` ,
5866 options : {
59- qs : { id : videoId , part : 'statistics' } ,
67+ qs : { id, part : 'statistics' } ,
6068 } ,
6169 }
6270 )
6371 )
6472 }
6573
66- async handle ( { videoId } , queryParams ) {
67- const json = await this . fetch ( { videoId } )
68- if ( json . items . length === 0 ) {
69- throw new NotFound ( { prettyMessage : 'video not found' } )
74+ async handle ( { channelId, videoId } , queryParams ) {
75+ const id = channelId || videoId
76+ const json = await this . fetch ( { id } )
77+ if ( json . pageInfo . totalResults === 0 ) {
78+ throw new NotFound ( {
79+ prettyMessage : `${ this . constructor . type } not found` ,
80+ } )
7081 }
7182 const statistics = json . items [ 0 ] . statistics
72- return this . constructor . render ( { statistics, videoId } , queryParams )
83+ return this . constructor . render ( { statistics, id } , queryParams )
7384 }
7485}
7586
76- module . exports = { documentation, YouTubeBase }
87+ class YouTubeVideoBase extends YouTubeBase {
88+ static type = 'video'
89+ }
90+
91+ class YouTubeChannelBase extends YouTubeBase {
92+ static type = 'channel'
93+ }
94+
95+ module . exports = { documentation, YouTubeVideoBase, YouTubeChannelBase }
0 commit comments