From 9dea85730d2412075437f283989700a05f1b4009 Mon Sep 17 00:00:00 2001 From: reality Date: Mon, 15 Jul 2013 18:52:40 +0000 Subject: [PATCH] youtube link handle thing [#534] --- modules/youtube/config.json | 3 +++ modules/youtube/strings.json | 5 +++++ modules/youtube/youtube.js | 40 ++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 modules/youtube/config.json create mode 100644 modules/youtube/strings.json create mode 100644 modules/youtube/youtube.js diff --git a/modules/youtube/config.json b/modules/youtube/config.json new file mode 100644 index 0000000..e8e1bae --- /dev/null +++ b/modules/youtube/config.json @@ -0,0 +1,3 @@ +{ + "outputPrefix": "\u000305youtube\u000f" +} diff --git a/modules/youtube/strings.json b/modules/youtube/strings.json new file mode 100644 index 0000000..5afff8d --- /dev/null +++ b/modules/youtube/strings.json @@ -0,0 +1,5 @@ +{ + "yt_video": { + "en": "[{title} by {author} — \u000312▶\u000f{plays} (\u00039▲{likes}\u000f|\u000312{dislikes}▼\u000f)]" + } +} diff --git a/modules/youtube/youtube.js b/modules/youtube/youtube.js new file mode 100644 index 0000000..dbb59a6 --- /dev/null +++ b/modules/youtube/youtube.js @@ -0,0 +1,40 @@ +/** + * Module Name: youtube + * Description: Various Youtube functionality + */ +var _ = require('underscore')._, + request = require('request'); + +var youtube = function(dbot) { + this.ApiRoot = 'https://gdata.youtube.com/feeds/api'; + this.params = { + 'alt': 'json', + 'v': 2 + }; + + this.onLoad = function() { + dbot.api.link.addHandler(this.name, + /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/, + function(event, match, name) { + request.get(this.ApiRoot + '/videos/' + match[2], { + 'qs': this.params, + 'json': true + }, function(error, response, body) { + if(_.isObject(body) && _.has(body, 'entry')) { + var v = body.entry; + event.reply(dbot.t('yt_video', { + 'title': v.title['$t'], + 'plays': v['yt$statistics'].viewCount, + 'author': v.author[0].name['$t'], + 'likes': v['yt$rating'].numLikes, + 'dislikes': v['yt$rating'].numDislikes + })); + } + }); + }.bind(this)); + }.bind(this); +}; + +exports.fetch = function(dbot) { + return new youtube(dbot); +};