3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-18 08:32:42 +01:00
dbot/modules/imdb/imdb.js

74 lines
2.2 KiB
JavaScript
Raw Normal View History

/**
* Module Name: IMDB
* Description: Various IMDB functionality.
*/
var _ = require('underscore')._,
request = require('request');
var imdb = function(dbot) {
var ApiRoot = 'http://mymovieapi.com/';
this.internalAPI = {
'formatLink': function(m) {
var rating = m.rating;
var rColour = (rating <= 5) ? '\u00033 ' : '\u00034 ';
rating = rColour + String(rating) + '\u000f';
var mString = dbot.t('imdb_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, 'plot_simple')) mString += ' [Description: ' + m.plot_simple + ']';
mString += ' - ' + m.imdb_url;
return mString;
}
};
this.commands = {
2013-10-13 06:47:58 +02:00
'~imdb': function(event) {
2013-10-13 00:58:34 +02:00
request.get(ApiRoot, {
'qs': {
'q': event.input[1],
'page_limit': 1
},
'json': true
}, function(error, response, body) {
if(_.isObject(body) && !_.isUndefined(body[0])) {
event.reply(this.internalAPI.formatLink(body[0]));
} else {
event.reply(dbot.t('imdb_noresults'));
}
}.bind(this));
}
};
2013-10-13 06:51:15 +02:00
this.commands['~imdb'].regex = [/^~imdb (.+)$/, 2];
2013-10-13 00:58:34 +02:00
this.onLoad = function() {
dbot.api.link.addHandler('imdb', /https?:\/\/(www\.)?imdb\.com\/title\/([a-zA-Z0-9]+)/, function(matches, name, callback) {
var id = matches[2];
request.get(ApiRoot, {
'qs': {
'id': id,
'page_limit': 1
},
'json': true
}, function(error, response, body) {
if(_.isObject(body) && !_.has(body, 'error')) {
callback(this.internalAPI.formatLink(body));
}
}.bind(this));
}.bind(this));
}.bind(this)
};
exports.fetch = function(dbot) {
return new imdb(dbot);
};