Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load statistics from youtube when links are pasted to channel #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ecmabot.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var JSBot = function(profile) {
this.factoids = new FactoidServer(path.join(__dirname, "ecmabot-factoids.json"));
this.caniuse_server = new CanIUseServer;
this.executeRegex = /^((?:sm?|v8|js?|b|n|>>?|>>>>|\|)>)([^>].*)+/;
this.youtubeRegex = /https?:\/\/www.youtube.com\/watch\?v=(\w+)/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about youtu.be links

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the best use for an "explicit trigger" would then be a command like !yt to make it go fetch and paste data after somebody else has already pasted a link. Then it would only occur if at least one user is interested.

youtu.be links won't be a problem, but, maybe the googleapi auth key will?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mainly meant the regex :-) you can extract the video ID from both links if needed.


Bot.call(this, profile);
this.set_log_level(this.LOG_ALL);
Expand All @@ -33,6 +34,8 @@ JSBot.prototype.init = function() {

this.register_listener(this.executeRegex, Shared.execute_js);

this.register_listener(this.youtubeRegex, Shared.youtube);

//this.register_listener(/^(\S+)(\+\+|--);?$/, this.do_beers);

this.register_command("g", Shared.google, {
Expand Down
35 changes: 35 additions & 0 deletions lib/youtube/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var HTTPS = require("https");

var youtube = module.exports = function(query, callback) {
var self = this;

var request = HTTPS.get({
'host': 'www.googleapis.com',
'path': "/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&key=<pasteSomeGoogleAPIKeyHere>&id=" + query,
'Referer': 'http://www.v8bot.com',
'User-Agent': 'NodeJS HTTP client',
'Accept': '*/*'
})

request.addListener('response', function(response) {
response.setEncoding('utf8');
var body = "";
response.addListener('data', function(chunk) { body += chunk; });
response.addListener('end', function() {
var videoData = JSON.parse(body);
var video = videoData.items && videoData.items.length && videoData.items[0] || null
if (video) {
callback.call(self, {
title: video.snippet.title,
user: video.snippet.channelTitle,
duration: video.contentDetails.duration,
views: video.statistics.viewCount,
likes: video.statistics.likeCount,
dislikes: video.statistics.dislikeCount,
});
} else {
callback.call(self, null);
}
});
});
};
13 changes: 13 additions & 0 deletions shared.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This is for common functions defined in many bots at once
var Sandbox = require("./lib/sandbox");
var FeelingLucky = require("./lib/feelinglucky");
var YoutubeRequest = require("./lib/youtube");
var Gist = require("./lib/paste/gist");

function parse_regex_literal (text) {
Expand Down Expand Up @@ -75,6 +76,18 @@ var Shared = module.exports = {
});
},

youtube: function(context, text, vidId) {
YoutubeRequest(vidId, function(data) {
context.channel.send(
'^ Youtube :: ' + data.title +
' :: Channel: ' + data.user +
' :: Duration: ' + data.duration.replace(/PT(\d{1,4})M(\d{1,2})S/,'$1:$2') +
' :: Views: ' + Number(data.views).toLocaleString('en') +
' :: likes: ' + Number(data.likes).toLocaleString('en') +
' :: dislikes: ' + Number(data.dislikes).toLocaleString('en')
);
});
},

execute_js: function(context, text, command, code) {
var engine;
Expand Down