dbot/modules/rt/rt.js

62 lines
2.0 KiB
JavaScript
Raw Normal View History

2014-05-22 18:25:52 +02:00
/**
* Module Name: RT
* Description: Various RT functionality.
*/
var _ = require('underscore')._,
request = require('request');
var rt = function(dbot) {
var ApiRoot = 'http://api.rottentomatoes.com/api/public/v1.0/';
this.internalAPI = {
'formatLink': function(m) {
var rating = m.ratings.audience_score;
var rColour = (rating <= 5) ? '\u00033 ' : '\u00034 ';
rating = rColour + String(rating) + '%\u000f';
var mString = dbot.t('rt_film', {
'title': m.title,
'year': m.year,
'rating': rating
});
if(_.has(m, 'directors')) mString += ' [Director: ' + m.directors[0] + ']';
if(_.has(m, 'genres')) mString += ' [Genre: ' + m.genres[0] + ']';
if(_.has(m, 'synopsis') && m.synopsis != '') {
2014-06-02 04:13:04 +02:00
if(m.synopsis.length > 140) m.synopsis = m.synopsis.substring(0, 140) + '...';
2014-05-22 18:25:52 +02:00
mString += ' [Synopsis: ' + m.synopsis + ']';
} else if(_.has(m, 'critics_consensus')) {
mString += ' [Review: ' + m.critics_consensus + ']';
}
2016-02-21 00:06:28 +01:00
mString += ' - http:' + m.links.alternate;
2014-05-22 18:25:52 +02:00
return mString;
}
};
this.commands = {
'~rt': function(event) {
request.get(ApiRoot + 'movies.json', {
'qs': {
'q': event.input[1],
'page_limit': 1,
'apikey': this.config.api_key
},
'json': true
}, function(error, response, body) {
if(_.isObject(body) && _.has(body, 'movies') && !_.isUndefined(body.movies[0])) {
event.reply(this.internalAPI.formatLink(body.movies[0]));
} else {
event.reply(dbot.t('rt_noresults'));
}
}.bind(this));
}
};
this.commands['~rt'].regex = [/^rt (.+)$/, 2];
};
exports.fetch = function(dbot) {
return new rt(dbot);
};