diff --git a/IMDB/README.md b/IMDB/README.md new file mode 100644 index 0000000..f9fe2d4 --- /dev/null +++ b/IMDB/README.md @@ -0,0 +1 @@ +Queries OMDB database for information about IMDB titles diff --git a/IMDB/__init__.py b/IMDB/__init__.py new file mode 100644 index 0000000..2f383d9 --- /dev/null +++ b/IMDB/__init__.py @@ -0,0 +1,45 @@ +### +# Copyright (c) 2015, butterscotchstallion +# All rights reserved. +# +# +### + +""" +IMDB: Queries OMDB database for information about IMDB titles +""" + +import supybot +import supybot.world as world + +# Use this for the version of this plugin. You may wish to put a CVS keyword +# in here if you're keeping the plugin in CVS or some similar system. +__version__ = "" + +# XXX Replace this with an appropriate author or supybot.Author instance. +__author__ = supybot.authors.unknown + +# This is a dictionary mapping supybot.Author instances to lists of +# contributions. +__contributors__ = {} + +# This is a url where the most recent plugin package can be downloaded. +__url__ = '' + +from . import config +from . import plugin +from imp import reload +# In case we're being reloaded. +reload(config) +reload(plugin) +# Add more reloads here if you add third-party modules and want them to be +# reloaded when this plugin is reloaded. Don't forget to import them as well! + +if world.testing: + from . import test + +Class = plugin.Class +configure = config.configure + + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/IMDB/config.py b/IMDB/config.py new file mode 100644 index 0000000..c783eea --- /dev/null +++ b/IMDB/config.py @@ -0,0 +1,41 @@ +### +# Copyright (c) 2015, butterscotchstallion +# All rights reserved. +# +# +### + +import supybot.conf as conf +import supybot.registry as registry +try: + from supybot.i18n import PluginInternationalization + _ = PluginInternationalization('IMDB') +except: + # Placeholder that allows to run the plugin on a bot + # without the i18n module + _ = lambda x: x + + +def configure(advanced): + # This will be called by supybot to configure this module. advanced is + # a bool that specifies whether the user identified themself as an advanced + # user or not. You should effect your configuration by manipulating the + # registry as appropriate. + from supybot.questions import expect, anything, something, yn + conf.registerPlugin('IMDB', True) + + +IMDB = conf.registerPlugin('IMDB') + +conf.registerGlobalValue(IMDB, 'template', + registry.String("$title ($year, $country, [$rated], $genre, $runtime) | IMDB: $imdbRating MC: $metascore% | $plot | Director: $director | Writer: $writer | Actors: $actors | http://imdb.com/title/$imdbID", _("""Template for the output of a search query."""))) + +# alternative template: +# $title ($year - $director) :: [i:$imdbRating r:$tomatoMeter m:$metascore] $plot :: http://imdb.com/title/$imdbID + +conf.registerGlobalValue(IMDB, 'noResultsMessage', + registry.String("No results for that query.", _("""This message is sent when there are no results"""))) +conf.registerGlobalValue(IMDB, 'omdbAPI', + registry.String('', _("""OMDB API Key"""))) + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/IMDB/plugin.py b/IMDB/plugin.py new file mode 100644 index 0000000..a79a56d --- /dev/null +++ b/IMDB/plugin.py @@ -0,0 +1,111 @@ +### +# Copyright (c) 2015, butterscotchstallion +# All rights reserved. +# +# +### + +import sys +import supybot.utils as utils +from supybot.commands import * +import supybot.plugins as plugins +import supybot.ircutils as ircutils +import supybot.ircmsgs as ircmsgs +import supybot.callbacks as callbacks +import requests +import json + +if sys.version_info[0] >= 3: + from urllib.parse import quote_plus +else: + from urllib import quote_plus + +try: + from supybot.i18n import PluginInternationalization + _ = PluginInternationalization('IMDB') +except ImportError: + # Placeholder that allows to run the plugin on a bot + # without the i18n module + _ = lambda x: x + +class IMDB(callbacks.Plugin): + """Queries OMDB database for information about IMDB titles""" + threaded = True + + def imdb(self, irc, msg, args, query): + """ + Queries OMDB api for query + """ + apikey = self.registryValue('omdbAPI') + if query[-4:].isdigit(): + encoded_query = quote_plus(query[0:-4]) + encoded_query_year = quote_plus(query[-4:]) + omdb_url = "http://www.omdbapi.com/?t=%s&y=%s&plot=short&r=json&tomatoes=true&apikey=%s" % (encoded_query, encoded_query_year, apikey) + self.log.info("IMDB: Check for %s year %s" % (query[0:-4], query[-4:])) + else: + encoded_query = quote_plus(query) + omdb_url = "http://www.omdbapi.com/?t=%s&y=&plot=short&r=json&tomatoes=true&apikey=%s" % (encoded_query, apikey) + + channel = msg.args[0] + result = None + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.60 Safari/537.36" + } + + self.log.info("IMDB: requesting %s" % omdb_url) + + try: + request = requests.get(omdb_url, timeout=10, headers=headers) + + if request.status_code == requests.codes.ok: + response = json.loads(request.text) + + not_found = "Error" in response + unknown_error = response["Response"] != "True" + + if not_found or unknown_error: + self.log.info("IMDB: OMDB error for %s" % (omdb_url)) + else: + imdb_template = self.registryValue("template") + if sys.version_info[0] < 3: + imdb_template = imdb_template.decode("utf-8") + imdb_template = imdb_template.replace("$title", response["Title"]) + imdb_template = imdb_template.replace("$year", response["Year"]) + imdb_template = imdb_template.replace("$country", response["Country"]) + imdb_template = imdb_template.replace("$director", response["Director"]) + imdb_template = imdb_template.replace("$plot", response["Plot"]) + imdb_template = imdb_template.replace("$imdbID", response["imdbID"]) + imdb_template = imdb_template.replace("$imdbRating", response["imdbRating"]) + imdb_template = imdb_template.replace("$tomatoMeter", response["tomatoMeter"]) + imdb_template = imdb_template.replace("$metascore", response["Metascore"]) + imdb_template = imdb_template.replace("$released",response["Released"]) + imdb_template = imdb_template.replace("$genre",response["Genre"]) + imdb_template = imdb_template.replace("$released",response["Released"]) + imdb_template = imdb_template.replace("$awards",response["Awards"]) + imdb_template = imdb_template.replace("$actors",response["Actors"]) + imdb_template = imdb_template.replace("$rated",response["Rated"]) + imdb_template = imdb_template.replace("$runtime",response["Runtime"]) + imdb_template = imdb_template.replace("$writer",response["Writer"]) + + result = imdb_template + else: + self.log.error("IMDB OMDB API %s - %s" % (request.status_code, request.text)) + + except requests.exceptions.Timeout as e: + self.log.error("IMDB Timeout: %s" % (str(e))) + except requests.exceptions.ConnectionError as e: + self.log.error("IMDB ConnectionError: %s" % (str(e))) + except requests.exceptions.HTTPError as e: + self.log.error("IMDB HTTPError: %s" % (str(e))) + finally: + if result is not None: + irc.reply(result) + else: + irc.error(self.registryValue("noResultsMessage")) + + imdb = wrap(imdb, ['text']) + +Class = IMDB + + +# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/IMDB/requirements.txt b/IMDB/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/IMDB/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/IMDB/test.py b/IMDB/test.py new file mode 100644 index 0000000..174ce5b --- /dev/null +++ b/IMDB/test.py @@ -0,0 +1,15 @@ +### +# Copyright (c) 2015, butterscotchstallion +# All rights reserved. +# +# +### + +from supybot.test import * + + +class IMDBTestCase(PluginTestCase): + plugins = ('IMDB',) + + +# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: