151 lines
4.9 KiB
Python
151 lines
4.9 KiB
Python
###
|
||
# Copyright (c) 2019, Pedro de Oliveira
|
||
# All rights reserved.
|
||
#
|
||
# Redistribution and use in source and binary forms, with or without
|
||
# modification, are permitted provided that the following conditions are met:
|
||
#
|
||
# * Redistributions of source code must retain the above copyright notice,
|
||
# this list of conditions, and the following disclaimer.
|
||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||
# this list of conditions, and the following disclaimer in the
|
||
# documentation and/or other materials provided with the distribution.
|
||
# * Neither the name of the author of this software nor the name of
|
||
# contributors to this software may be used to endorse or promote products
|
||
# derived from this software without specific prior written consent.
|
||
#
|
||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||
# POSSIBILITY OF SUCH DAMAGE.
|
||
|
||
###
|
||
|
||
from supybot import utils, plugins, ircutils, callbacks
|
||
from supybot.commands import *
|
||
try:
|
||
from supybot.i18n import PluginInternationalization
|
||
_ = PluginInternationalization('OMDb')
|
||
except ImportError:
|
||
# Placeholder that allows to run the plugin on a bot
|
||
# without the i18n module
|
||
_ = lambda x: x
|
||
from urllib.parse import urlencode
|
||
import json
|
||
import requests
|
||
|
||
|
||
class OMDb(callbacks.Plugin):
|
||
"""OMDb Client"""
|
||
threaded = True
|
||
|
||
def __init__(self, irc):
|
||
self.__parent = super(OMDb, self)
|
||
self.__parent.__init__(irc)
|
||
self.prepend = "8,4OMDb"
|
||
|
||
def __check_property(self, name, items):
|
||
if name in items and items[name] != 'N/A':
|
||
return True
|
||
else:
|
||
return False
|
||
|
||
def __do_request(self, arguments):
|
||
baseurl = "http://www.omdbapi.com/"
|
||
arguments['apiKey'] = self.registryValue('apiKey')
|
||
response = requests.get(baseurl + "?" + urlencode(arguments))
|
||
return json.loads(response.content.decode('utf-8'))
|
||
|
||
def __format_result(self, result):
|
||
message = "{}".format(result['Title'])
|
||
|
||
if self.__check_property('Year', result):
|
||
message = "{} ({})".format(
|
||
message,
|
||
result['Year'],
|
||
)
|
||
|
||
if self.__check_property('Genre', result):
|
||
message = "{} 8{}".format(
|
||
message,
|
||
result['Genre'],
|
||
)
|
||
|
||
if self.__check_property('Runtime', result):
|
||
message = "{} [{}]".format(
|
||
message,
|
||
result['Runtime'],
|
||
)
|
||
|
||
if self.__check_property('imdbRating', result):
|
||
message = "{} 13{}/10".format(
|
||
message,
|
||
result['imdbRating'],
|
||
)
|
||
|
||
if self.__check_property('Plot', result):
|
||
message = "{} {}".format(
|
||
message,
|
||
result['Plot'],
|
||
)
|
||
|
||
if self.__check_property('imdbID', result):
|
||
message = "{} https://www.imdb.com/title/{}/".format(
|
||
message,
|
||
result['imdbID'],
|
||
)
|
||
|
||
return message
|
||
|
||
def find(self, irc, msg, args, text):
|
||
"""<text>
|
||
|
||
Returns the matched item with <text>.
|
||
"""
|
||
key = self.registryValue('apiKey')
|
||
if not key:
|
||
irc.error("{} API key not set".format(self.prepend))
|
||
return
|
||
result = self.__do_request({'s': text})
|
||
|
||
output = ""
|
||
for item in result['Search']:
|
||
output = "{}{} ({}) {}, ".format(
|
||
output,
|
||
item['Title'],
|
||
item['Year'],
|
||
item['imdbID']
|
||
)
|
||
|
||
irc.reply(output[:-2], prefixNick=False)
|
||
find = wrap(find, ['something'])
|
||
|
||
def info(self, irc, msg, args, text):
|
||
"""<text>
|
||
|
||
Returns information about of the item matched with <text>.
|
||
"""
|
||
key = self.registryValue('apiKey')
|
||
if not key:
|
||
irc.error("{} API key not set".format(self.prepend))
|
||
return
|
||
if text[:2] == 'tt':
|
||
result = self.__do_request({'i': text})
|
||
else:
|
||
result = self.__do_request({'t': text})
|
||
|
||
irc.reply(self.__format_result(result), prefixNick=False)
|
||
info = wrap(info, ['something'])
|
||
|
||
Class = OMDb
|
||
|
||
|
||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|