From 803de2d3ce8f5269979cc656caece7f1e9f27c05 Mon Sep 17 00:00:00 2001 From: sillyslux Date: Wed, 9 Aug 2017 05:06:57 +0200 Subject: [PATCH 1/2] load statistics from youtube when links are pasted to channel --- ecmabot.js | 3 +++ lib/youtube/index.js | 35 +++++++++++++++++++++++++++++++++++ shared.js | 13 +++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 lib/youtube/index.js diff --git a/ecmabot.js b/ecmabot.js index 1d3d7c6..e43a144 100644 --- a/ecmabot.js +++ b/ecmabot.js @@ -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+)/; Bot.call(this, profile); this.set_log_level(this.LOG_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, { diff --git a/lib/youtube/index.js b/lib/youtube/index.js new file mode 100644 index 0000000..44493bc --- /dev/null +++ b/lib/youtube/index.js @@ -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=&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); + } + }); + }); +}; diff --git a/shared.js b/shared.js index 5ebf7a8..70324ec 100644 --- a/shared.js +++ b/shared.js @@ -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) { @@ -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; From 01aab6833a1549acc73996a7bd97a1c1a26e1ba4 Mon Sep 17 00:00:00 2001 From: sillyslux Date: Wed, 9 Aug 2017 09:00:19 +0200 Subject: [PATCH 2/2] change youtube fetch behaviour to !yt command --- ecmabot.js | 34 +++++++++++++++++++++++++++++++--- shared.js | 13 ------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/ecmabot.js b/ecmabot.js index e43a144..a84e427 100644 --- a/ecmabot.js +++ b/ecmabot.js @@ -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"); @@ -18,7 +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=(\w+)/; + this.youtubeRegex = /https?:\/\/(?:www.youtube.com\/watch\?v=|youtu.be\/)([^&\s]+)/; Bot.call(this, profile); this.set_log_level(this.LOG_ALL); @@ -34,13 +35,17 @@ JSBot.prototype.init = function() { this.register_listener(this.executeRegex, Shared.execute_js); - this.register_listener(this.youtubeRegex, Shared.youtube); + 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"}); @@ -82,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) { @@ -272,4 +301,3 @@ if (process.env.ECMABOT_PROFILE) { } (new JSBot(profile)).init(); - diff --git a/shared.js b/shared.js index 70324ec..5ebf7a8 100644 --- a/shared.js +++ b/shared.js @@ -1,7 +1,6 @@ // 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) { @@ -76,18 +75,6 @@ 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;