This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/imdb.py

74 lines
2.2 KiB
Python
Raw Normal View History

2013-06-27 12:34:06 +02:00
# IMDb lookup plugin by Ghetto Wizard (2011) and blha303 (2013)
2011-11-20 10:23:31 +01:00
from util import hook, http
import re
2013-06-27 12:34:06 +02:00
import json
id_re = re.compile("tt\d+")
2011-11-20 10:23:31 +01:00
2013-06-27 12:34:06 +02:00
def truncate(msg):
nmsg = msg.split(" ")
out = None
x = 0
for i in nmsg:
if x <= 7:
if out:
out = out + " " + nmsg[x]
else:
out = nmsg[x]
x = x + 1
if x <= 7:
return out
else:
return out + "..."
2011-11-20 10:23:31 +01:00
@hook.command
def imdb(inp):
2012-05-16 05:07:27 +02:00
"imdb <movie> -- Gets information about <movie> from IMDb."
2011-11-20 10:23:31 +01:00
strip = inp.strip()
if id_re.match(strip):
content = http.get_json("http://www.omdbapi.com/", i=strip)
else:
content = http.get_json("http://www.omdbapi.com/", t=strip)
2011-11-20 10:23:31 +01:00
2012-10-18 13:06:34 +02:00
if content.get('Error', None) == 'Movie not found!':
return 'Movie not found!'
2011-11-20 10:23:31 +01:00
elif content['Response'] == 'True':
2012-05-12 16:10:05 +02:00
content['URL'] = 'http://www.imdb.com/title/%(imdbID)s' % content
2011-11-20 10:23:31 +01:00
out = '\x02%(Title)s\x02 (%(Year)s) (%(Genre)s): %(Plot)s'
if content['Runtime'] != 'N/A':
out += ' \x02%(Runtime)s\x02.'
2012-05-12 16:10:05 +02:00
if content['imdbRating'] != 'N/A' and content['imdbVotes'] != 'N/A':
out += ' \x02%(imdbRating)s/10\x02 with \x02%(imdbVotes)s\x02' \
' votes.'
2011-11-20 10:23:31 +01:00
out += ' %(URL)s'
return out % content
else:
return 'Unknown error.'
2013-06-27 12:34:06 +02:00
imdb_re = (r'(.*:)//(imdb.com|www.imdb.com)(:[0-9]+)?(.*)', re.I)
@hook.regex(*imdb_re)
def imdb_url(match):
id = match.group(4).split('/')[-1]
if id == "":
id = match.group(4).split('/')[-2]
content = http.get_json("http://www.omdbapi.com/", i=id)
if content.get('Error', None) == 'Movie not found!':
return 'Movie not found!'
elif content['Response'] == 'True':
content['URL'] = 'http://www.imdb.com/title/%(imdbID)s' % content
content['Plot'] = truncate(content['Plot'])
out = '\x02%(Title)s\x02 (%(Year)s) (%(Genre)s): %(Plot)s'
if content['Runtime'] != 'N/A':
out += ' \x02%(Runtime)s\x02.'
if content['imdbRating'] != 'N/A' and content['imdbVotes'] != 'N/A':
out += ' \x02%(imdbRating)s/10\x02 with \x02%(imdbVotes)s\x02' \
' votes.'
return out % content
else:
return 'Unknown error.'