Add 'IMDB/' from commit '7a4fab539f746f961a360f57012b771e3f70bafb'

git-subtree-dir: IMDB
git-subtree-mainline: 547e7ff9cb5c4e952cb06ef5fde4270795c79cc6
git-subtree-split: 7a4fab539f746f961a360f57012b771e3f70bafb
This commit is contained in:
oddluck 2019-03-09 01:40:31 -05:00
commit fbacaad20b
6 changed files with 214 additions and 0 deletions

1
IMDB/README.md Normal file
View File

@ -0,0 +1 @@
Queries OMDB database for information about IMDB titles

45
IMDB/__init__.py Normal file
View File

@ -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:

41
IMDB/config.py Normal file
View File

@ -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:

111
IMDB/plugin.py Normal file
View File

@ -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:

1
IMDB/requirements.txt Normal file
View File

@ -0,0 +1 @@
requests

15
IMDB/test.py Normal file
View File

@ -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: