change to new youtube api

This commit is contained in:
reality 2015-04-20 20:10:55 +00:00
parent 25b026cf26
commit f6f6de508b
2 changed files with 42 additions and 37 deletions

View File

@ -1,3 +1,4 @@
{ {
"outputPrefix": "\u00031,0You\u000315,5Tube\u000f" "outputPrefix": "\u00031,0You\u000315,5Tube\u000f",
"api_key": "see google"
} }

View File

@ -6,21 +6,19 @@ var _ = require('underscore')._,
request = require('request'); request = require('request');
var youtube = function(dbot) { var youtube = function(dbot) {
this.ApiRoot = 'https://gdata.youtube.com/feeds/api'; this.ApiRoot = 'https://www.googleapis.com/youtube/v3/';
this.params = {
'alt': 'json',
'v': 2
};
this.LinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; this.LinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
this.api = { this.api = {
'search': function(term, callback) { 'search': function(term, callback) {
var qs = _.clone(this.params); var qs = _.clone(this.params);
request.get(this.ApiRoot + '/videos', { request.get(this.ApiRoot + 'search', {
'qs': _.extend(qs, { 'qs': {
'key': this.config.api_key,
'q': term, 'q': term,
'max-results': 1 'maxResults': 1,
}), 'part': "snippet"
},
'json': true 'json': true
}, function(error, response, body) { }, function(error, response, body) {
callback(body); callback(body);
@ -30,33 +28,22 @@ var youtube = function(dbot) {
this.internalAPI = { this.internalAPI = {
'formatLink': function(v) { 'formatLink': function(v) {
var seconds = v['media$group']['yt$duration'].seconds, var time = v.contentDetails.duration.match(/^PT(\d+)M(\d+)S$/),
minutes = Math.floor(seconds / 60), seconds = time[2],
seconds = ((seconds%60 < 10) ? "0"+seconds%60 : seconds%60); minutes = time[1];
if(!_.has(v, 'yt$rating')) {
v['yt$rating'] = {
'numLikes': 0,
'numDislikes': 0
};
}
if(!_.has(v, 'yt$statistics')) {
v['yt$statistics'] = { 'viewCount': 0 };
}
var res = dbot.t('yt_video', { var res = dbot.t('yt_video', {
'title': v.title['$t'], 'title': v.snippet.title,
'plays': v['yt$statistics'].viewCount.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"), 'plays': v.statistics.viewCount.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"),
'author': v.author[0].name['$t'], 'author': v.snippet.channelTitle,
'likes': v['yt$rating'].numLikes, 'likes': v.statistics.likeCount,
'dislikes': v['yt$rating'].numDislikes, 'dislikes': v.statistics.dislikeCount,
'minutes': minutes, 'minutes': minutes,
'seconds': seconds 'seconds': seconds
}); });
var link = v.link[0].href.match(this.LinkRegex); if(v.id) {
if(link) { res += ' - https://youtu.be/' + v.id;
res += ' - https://youtu.be/' + link[2];
} }
return res; return res;
@ -66,8 +53,20 @@ var youtube = function(dbot) {
this.commands = { this.commands = {
'~yt': function(event) { '~yt': function(event) {
this.api.search(event.input[1], function(body) { this.api.search(event.input[1], function(body) {
if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) { if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
event.reply(this.internalAPI.formatLink(body.feed.entry[0])); request.get(this.ApiRoot + 'videos', {
'qs': {
'key': this.config.api_key,
'id': body.items[0].id.videoId,
'maxResults': 1,
'part': "snippet,contentDetails,statistics,status"
},
'json': true
}, function(error, response, body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
event.reply(this.internalAPI.formatLink(body.items[0]));
}
}.bind(this));
} else { } else {
event.reply(dbot.t('yt_noresults')); event.reply(dbot.t('yt_noresults'));
} }
@ -79,12 +78,17 @@ var youtube = function(dbot) {
this.onLoad = function() { this.onLoad = function() {
dbot.api.link.addHandler(this.name, this.LinkRegex, dbot.api.link.addHandler(this.name, this.LinkRegex,
function(match, name, callback) { function(match, name, callback) {
request.get(this.ApiRoot + '/videos/' + match[2], { request.get(this.ApiRoot + 'videos', {
'qs': this.params, 'qs': {
'key': this.config.api_key,
'id': match[2],
'maxResults': 1,
'part': "snippet,contentDetails,statistics,status"
},
'json': true 'json': true
}, function(error, response, body) { }, function(error, response, body) {
if(_.isObject(body) && _.has(body, 'entry')) { if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
callback(this.internalAPI.formatLink(body.entry)); callback(this.internalAPI.formatLink(body.items[0]));
} }
}.bind(this)); }.bind(this));
}.bind(this)); }.bind(this));