From 3227f35ca41d185b6791063a6af5fb3699c68130 Mon Sep 17 00:00:00 2001 From: Eli Morris-Heft Date: Wed, 25 Dec 2013 08:44:15 -0600 Subject: [PATCH] Moved some config stuff around. Also reworked the youtube module and added main to package.json. --- config.js => config.js.example | 10 ++- modules/karma.js | 2 +- modules/twitter.js | 15 ++-- modules/youtube.js | 146 ++++++++++++++++----------------- package.json | 3 +- 5 files changed, 86 insertions(+), 90 deletions(-) rename config.js => config.js.example (78%) diff --git a/config.js b/config.js.example similarity index 78% rename from config.js rename to config.js.example index 7cbaab6..b512870 100644 --- a/config.js +++ b/config.js.example @@ -6,7 +6,7 @@ module.exports = { realName: '', debug: false, stripColors: true, - sendErrors: 'dispatchrabbi', + sendErrors: '', channels: [], modules: [ { @@ -23,7 +23,7 @@ module.exports = { name: 'karma', config: { store: true, - limit: 3 + explainReasons: 3 } }, { @@ -41,7 +41,11 @@ module.exports = { { name: 'twitter', config: { - store: false + store: false, + consumer_key: '', + consumer_secret: '', + access_token_key: '', + access_token_secret: '' } }, { diff --git a/modules/karma.js b/modules/karma.js index 9827292..46ee5ab 100644 --- a/modules/karma.js +++ b/modules/karma.js @@ -129,7 +129,7 @@ var karmaAdjustRegexp = /^(\([^)]+\)|[^ ]+)([+]{2}|[-]{2})(.*)?$/, var subject = _getSubject(text.substr('!explain '.length)); if(_karma[subject]) { - var limit = this.config.limit || 3; + var limit = this.config.explainReasons || 3; this.bot.reply(nick, to, subject + ' has ' + _karma[subject].value + ' karma. The highest it\'s ever been was ' + _karma[subject].highest + ' and the lowest it\'s ever been was ' + _karma[subject].lowest + '.'); _karma[subject].positive.length && this.bot.reply(nick, to, 'Positive: ' + _selectReasons(_karma[subject].positive, limit).join('; ')); diff --git a/modules/twitter.js b/modules/twitter.js index df848c2..5ea0f27 100644 --- a/modules/twitter.js +++ b/modules/twitter.js @@ -3,13 +3,7 @@ var Module = require('./module.js'), util = require('util'), moment = require('moment'); -var twitterConfig = { - consumer_key: '', - consumer_secret: '', - access_token_key: '', - access_token_secret: '' - }, - tweetRegex = /(http(s)?:\/\/)?(www.)?twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)/, +var tweetRegex = /(http(s)?:\/\/)?(www.)?twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)/, messageListener = function(to, nick, text, raw) { var tweetID = tweetRegex.exec(text), bot = this.bot; @@ -36,7 +30,12 @@ twitterModule.load = function(name, config, bot) { var self = this; this.__proto__.load.apply(this, arguments); - this.twitter = new TwitterAPI(twitterConfig); + this.twitter = new TwitterAPI({ + consumer_key: this.config.consumer_key, + consumer_secret: this.config.consumer_secret, + access_token_key: this.config.access_token_key, + access_token_secret: this.config.access_token_secret + }); this.twitter.verifyCredentials(function (error, data) { if(error) { diff --git a/modules/youtube.js b/modules/youtube.js index a5d25a1..65622d5 100644 --- a/modules/youtube.js +++ b/modules/youtube.js @@ -6,49 +6,49 @@ var Module = require('./module.js'), // This fix for displaying a duration from here: https://github.com/moment/moment/issues/463#issuecomment-16698903 moment.duration.fn.format = function (input) { - var output = input; - var milliseconds = this.asMilliseconds(); - var totalMilliseconds = 0; - var replaceRegexps = { - years: /Y(?!Y)/g, - months: /M(?!M)/g, - weeks: /W(?!W)/g, - days: /D(?!D)/g, - hours: /H(?!H)/g, - minutes: /m(?!m)/g, - seconds: /s(?!s)/g, - milliseconds: /S(?!S)/g - } - var matchRegexps = { - years: /Y/g, - months: /M/g, - weeks: /W/g, - days: /D/g, - hours: /H/g, - minutes: /m/g, - seconds: /s/g, - milliseconds: /S/g - } - for (var r in replaceRegexps) { - if (replaceRegexps[r].test(output)) { - var as = 'as'+r.charAt(0).toUpperCase() + r.slice(1); - var value = new String(Math.floor(moment.duration(milliseconds - totalMilliseconds)[as]())); - var replacements = output.match(matchRegexps[r]).length - value.length; - output = output.replace(replaceRegexps[r], value); + var output = input; + var milliseconds = this.asMilliseconds(); + var totalMilliseconds = 0; + var replaceRegexps = { + years: /Y(?!Y)/g, + months: /M(?!M)/g, + weeks: /W(?!W)/g, + days: /D(?!D)/g, + hours: /H(?!H)/g, + minutes: /m(?!m)/g, + seconds: /s(?!s)/g, + milliseconds: /S(?!S)/g + }; + var matchRegexps = { + years: /Y/g, + months: /M/g, + weeks: /W/g, + days: /D/g, + hours: /H/g, + minutes: /m/g, + seconds: /s/g, + milliseconds: /S/g + }; + for (var r in replaceRegexps) { + if (replaceRegexps[r].test(output)) { + var as = 'as'+r.charAt(0).toUpperCase() + r.slice(1); + var value = Math.floor(moment.duration(milliseconds - totalMilliseconds)[as]()).toString(); + var replacements = output.match(matchRegexps[r]).length - value.length; + output = output.replace(replaceRegexps[r], value); - while (replacements > 0 && replaceRegexps[r].test(output)) { - output = output.replace(replaceRegexps[r], '0'); - replacements--; - } - output = output.replace(matchRegexps[r], ''); + while (replacements > 0 && replaceRegexps[r].test(output)) { + output = output.replace(replaceRegexps[r], '0'); + replacements--; + } + output = output.replace(matchRegexps[r], ''); - var temp = {}; - temp[r] = value; - totalMilliseconds += moment.duration(temp).asMilliseconds(); - } - } - return output; -} + var temp = {}; + temp[r] = value; + totalMilliseconds += moment.duration(temp).asMilliseconds(); + } + } + return output; +}; function pick() { for (var i = 0; i < arguments.length; i++) { @@ -60,10 +60,10 @@ function pick() { return null; } -var videoRegex = /(?:http(?:s)?:\/\/)?(?:www.)?(?:youtube\.com\/(?:watch\?(?:.*&)?v=([a-zA-Z0-9\-_]+)|v\/([a-zA-Z0-9\-_]+))|youtu.be\/([a-zA-Z0-9\-_]+))/i, - messageListener = function(to, nick, text, raw) { - var videoID = videoRegex.exec(text), - bot = this.bot; +var videoRegex = /(?:http(?:s)?:\/\/)?(?:www.)?(?:youtube\.com\/(?:watch\?(?:.*&)?v=([a-zA-Z0-9\-_]+)|v\/([a-zA-Z0-9\-_]+))|youtu.be\/([a-zA-Z0-9\-_]+))/i; +var messageListener = function(to, nick, text, raw) { + var videoID = videoRegex.exec(text), + bot = this.bot; if (videoID && videoID.length) { videoID.shift(); @@ -73,45 +73,37 @@ var videoRegex = /(?:http(?:s)?:\/\/)?(?:www.)?(?:youtube\.com\/(?:watch\?(?:.*& videoID = 0; } - if(videoID) { - request('https://gdata.youtube.com/feeds/api/videos/' + videoID + '?v=2&alt=json', function(error, response, body) { - if (!error && response.statusCode == 200) { - var videoInfo = JSON.parse(body).entry; - - var response = '%s - %s | Posted by %s on %s | %s views, %s likes, %s dislikes', - videoTitle = videoInfo.title['$t'], - videoDuration = moment.duration(parseInt(videoInfo['media$group']['yt$duration'].seconds, 10), 'seconds').format('mm:ss'), - videoAuthor = videoInfo.author[0].name['$t'], - videoPublishDate = moment(videoInfo.published['$t']).format('dddd, MMMM Do YYYY, h:mm:ss a'), - videoViewCount = 0, - videoLikeCount = 0, - videoDislikeCount = 0; - - if (videoInfo['yt$statistics']) { - videoViewCount = videoInfo['yt$statistics'].viewCount; - } + if(videoID) { + request('https://gdata.youtube.com/feeds/api/videos/' + videoID + '?v=2&alt=json', function(error, response, body) { + if (!error && response.statusCode == 200) { + var videoInfo = JSON.parse(body).entry, + duration = moment.duration(parseInt(videoInfo.media$group.yt$duration.seconds, 10), 'seconds').format('mm:ss'), + publishDate = moment(videoInfo.published.$t).format('dddd, MMMM Do YYYY, h:mm:ss a'), + views = (videoInfo.yt$statistics && videoInfo.yt$statistics.viewCount) || 0, + likes = (videoInfo.yt$rating && videoInfo.yt$rating.numLikes) || 0, + dislikes = (videoInfo.yt$rating && videoInfo.yt$rating.numDislikes) || 0; - if (videoInfo['yt$rating']) { - videoLikeCount = videoInfo['yt$rating'].numLikes; - videoDislikeCount = videoInfo['yt$rating'].numDislikes; - } - - videoViewCount = numeral(videoViewCount).format(','); - videoLikeCount = numeral(videoLikeCount).format(','); - videoDislikeCount = numeral(videoDislikeCount).format(','); - - bot.reply(to, nick, util.format(response, videoTitle, videoDuration, videoAuthor, videoPublishDate, videoViewCount, videoLikeCount, videoDislikeCount)); - } - }); - } + + bot.reply(to, nick, util.format('%s - %s | Posted by %s on %s | %s views, %s likes, %s dislikes', + videoInfo.title.$t, // video title + duration, + videoInfo.author[0].name.$t, // video artist name + publishDate, + numeral(views).format(','), + numeral(likes).format(','), + numeral(dislikes).format(',') + )); + } + }); + } }; var youtubeModule = new Module({ - 'message#': [messageListener] + 'message#': [messageListener] }); youtubeModule.help = function() { return ['This module looks for youtube URLs in posts to channels and echoes information about the video.']; -} +}; module.exports = youtubeModule; \ No newline at end of file diff --git a/package.json b/package.json index 22f75e8..db59c6b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,8 @@ { "name": "spikebot", - "version": "2.1.0", + "version": "2.2.0", "description": "Your number one assistant-bot", + "main": "spikebot.js", "dependencies": { "irc": "0.3.x", "json-store": "0.0.x",