From c298813b28418d867328a220db50b67a0e24e621 Mon Sep 17 00:00:00 2001 From: Scritches Date: Thu, 26 Apr 2018 16:58:15 -0400 Subject: [PATCH] Adds OMDB module to replace the RottenTomatoes module. ~movie command replaces ~rt command. API key from http://www.omdbapi.com/apikey.aspx --- modules/omdb/config.json | 6 +++ modules/omdb/omdb.js | 83 +++++++++++++++++++++++++++++++++++++++ modules/omdb/strings.json | 10 +++++ 3 files changed, 99 insertions(+) create mode 100644 modules/omdb/config.json create mode 100644 modules/omdb/omdb.js create mode 100644 modules/omdb/strings.json diff --git a/modules/omdb/config.json b/modules/omdb/config.json new file mode 100644 index 0000000..981489a --- /dev/null +++ b/modules/omdb/config.json @@ -0,0 +1,6 @@ +{ + "dependencies": [ ], + "ignorable": true, + "outputPrefix": "omdb", + "api_key": "insert api key here - get from http://www.omdbapi.com/apikey.aspx" +} diff --git a/modules/omdb/omdb.js b/modules/omdb/omdb.js new file mode 100644 index 0000000..028efa4 --- /dev/null +++ b/modules/omdb/omdb.js @@ -0,0 +1,83 @@ +/** + * Module Name: omdb + * Description: Interacts with the Open Movie Database to provide movie summary + * and review information. + */ + +var rp = require('request-promise-native'), + _ = require('underscore')._; + +var OMDB = function(dbot) { + this.apiRoot = 'http://www.omdbapi.com'; + this.imdbLinkPrefix = 'https://www.imdb.com/title/'; + + this.internalAPI = { + formatLink: r => { + var aRating = parseFloat(r.imdbRating) * 10; + var cRating = parseFloat(r.Metascore); + + if (isNaN(aRating)) { + aRating = " N/A"; + } else { + var aColour = (aRating <= 5) ? '\u00033 ' : '\u00034 '; + aRating = aColour + String(aRating) + '%\u000f'; + } + + if (isNaN(cRating)) { + cRating = " N/A"; + } else { + var cColour = (cRating <= 5) ? '\u00033 ' : '\u00034 '; + cRating = cColour + String(cRating) + '%\u000f'; + } + + var mString = dbot.t('omdb_film', { + 'title': r.Title, + 'year': r.Year, + 'aRating': aRating, + 'cRating': cRating + }); + + if (_.has(r, 'Director') && r.Director != "N/A") mString += ' [Director: ' + r.Director + ']'; + if (_.has(r, 'Genre') && r.Genre != "N/A") mString += ' [Genre: ' + r.Genre + ']'; + if (_.has(r, 'Plot') && r.Plot != "N/A") { + if (r.Plot.length > 140) r.Plot = r.Plot.substring(0, 140) + '...'; + mString += ' [Plot: ' + r.Plot + ']'; + } + + mString += ' - ' + this.imdbLinkPrefix + r.imdbID; + + return mString; + } + }; + + this.commands = { + '~movie': async event => { + try { + var r = await rp({ + url: this.apiRoot, + qs: { + apikey: this.config.api_key, + t: event.input[1], + plot: 'short', + r: 'json' + }, + json: true + }); + + if (r.Response === 'True') { + event.reply(this.internalAPI.formatLink(r)); + } else { + event.reply(dbot.t('omdb_noresults')); + } + } + catch (e) { + console.log(e); + } + } + }; + + this.commands['~movie'].regex = [/^movie (.+)$/, 2]; +} + + +exports.fetch = dbot => new OMDB(dbot); diff --git a/modules/omdb/strings.json b/modules/omdb/strings.json new file mode 100644 index 0000000..31d2dfb --- /dev/null +++ b/modules/omdb/strings.json @@ -0,0 +1,10 @@ +{ + "omdb_film": { + "en": "[{title} - Audience:{aRating} - Critic:{cRating} - {year}]", + "de": "[{title} - {aRating} - {year}]" + }, + "omdb_noresults": { + "en": "No films found.", + "de": "Kein Film gefunden." + } +}