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 all commits
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
33 changes: 32 additions & 1 deletion ecmabot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var Sandbox = require("./lib/sandbox");
var FactoidServer = require("./lib/factoidserv");
var FeelingLucky = require("./lib/feelinglucky");
var CanIUseServer = require("./lib/caniuse");
var YoutubeRequest = require("./lib/youtube");

var Bot = require("./lib/irc");
var Shared = require("./shared");
Expand All @@ -18,6 +19,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=|youtu.be\/)([^&\s]+)/;

Bot.call(this, profile);
this.set_log_level(this.LOG_ALL);
Expand All @@ -33,11 +35,17 @@ JSBot.prototype.init = function() {

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

this.register_listener(this.youtubeRegex, this.ytRememberId);

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

this.register_command("g", Shared.google, {
help: "Run this command with a search query to return the first Google result. Usage: !g kitten images"});

this.register_command("yt", this.ytRequest, {
help: "You have to paste a link to youtube first."
});

this.register_command("google", this.google, {
help: "Returns a link to a Google search page of the search term. Usage: !google opencourseware computational complexity"});

Expand Down Expand Up @@ -79,6 +87,30 @@ JSBot.prototype.init = function() {

};

JSBot.prototype.ytRememberId = function(context, text, vidId) {
this._lastYtId = vidId
};

JSBot.prototype.ytRequest = function(context, text) {
if (this._lastYtId) {
YoutubeRequest(this._lastYtId, function(data) {
context.channel.send(
'^^ Youtube: ' + data.title +
' (by ' + data.user +
') [' + data.duration
.replace(/PT((\d{1,2})H)?((\d{1,2})M)?((\d{1,2})S)?/,'$2:$4:$6')
.replace(/:(\d)$/,':0$1')
.replace(/^:|:$/g,'') +
'] views:' + Number(data.views).toLocaleString('en') +
' likes:' + Number(data.likes).toLocaleString('en') +
' dislikes:' + Number(data.dislikes).toLocaleString('en')
);
});
this._lastYtId = null
} else {
context.channel.send_reply (context.sender, this.get_command_help("yt"));
}
};

JSBot.prototype.google = function(context, text) {

Expand Down Expand Up @@ -269,4 +301,3 @@ if (process.env.ECMABOT_PROFILE) {
}

(new JSBot(profile)).init();

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);
}
});
});
};