Skip to content

Commit

Permalink
Merge pull request #11 from BronyCon/config-move
Browse files Browse the repository at this point in the history
Config reworking!
  • Loading branch information
dispatchrabbi committed Dec 25, 2013
2 parents 0558be6 + 3227f35 commit 19bea56
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 90 deletions.
10 changes: 7 additions & 3 deletions config.js → config.js.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
realName: '',
debug: false,
stripColors: true,
sendErrors: 'dispatchrabbi',
sendErrors: '',
channels: [],
modules: [
{
Expand All @@ -23,7 +23,7 @@ module.exports = {
name: 'karma',
config: {
store: true,
limit: 3
explainReasons: 3
}
},
{
Expand All @@ -41,7 +41,11 @@ module.exports = {
{
name: 'twitter',
config: {
store: false
store: false,
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion modules/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('; '));
Expand Down
15 changes: 7 additions & 8 deletions modules/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
146 changes: 69 additions & 77 deletions modules/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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();
Expand All @@ -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;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 19bea56

Please sign in to comment.