IMDB module: reworking, handle year precision
This commit is contained in:
parent
cd7843e16e
commit
eae0adbb43
@ -13,27 +13,77 @@ nemubotversion = 3.4
|
|||||||
def help_full():
|
def help_full():
|
||||||
return "Search a movie title with: !imdbs <approximative title> ; View movie details with !imdb <title>"
|
return "Search a movie title with: !imdbs <approximative title> ; View movie details with !imdb <title>"
|
||||||
|
|
||||||
@hook("cmd_hook", "imdb", help="View movie details with !imdb <title>")
|
def get_movie(title=None, year=None, imdbid=None, fullplot=True, tomatoes=False):
|
||||||
|
"""Returns the information about the matching movie"""
|
||||||
|
|
||||||
|
# Built URL
|
||||||
|
url = "http://www.omdbapi.com/?"
|
||||||
|
if title is not None:
|
||||||
|
url += "t=%s&" % urllib.parse.quote(title)
|
||||||
|
if year is not None:
|
||||||
|
url += "y=%s&" % urllib.parse.quote(year)
|
||||||
|
if imdbid is not None:
|
||||||
|
url += "i=%s&" % urllib.parse.quote(imdbid)
|
||||||
|
if fullplot:
|
||||||
|
url += "plot=full&"
|
||||||
|
if tomatoes:
|
||||||
|
url += "tomatoes=true&"
|
||||||
|
|
||||||
|
print_debug(url)
|
||||||
|
|
||||||
|
# Make the request
|
||||||
|
response = urllib.request.urlopen(url)
|
||||||
|
data = json.loads(response.read().decode())
|
||||||
|
|
||||||
|
# Return data
|
||||||
|
if "Error" in data:
|
||||||
|
raise IRCException(data["Error"])
|
||||||
|
|
||||||
|
elif "Response" in data and data["Response"] == "True":
|
||||||
|
return data
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise IRCException("An error occurs during movie search")
|
||||||
|
|
||||||
|
def find_movies(title):
|
||||||
|
"""Find existing movies matching a approximate title"""
|
||||||
|
|
||||||
|
# Built URL
|
||||||
|
url = "http://www.omdbapi.com/?s=%s" % urllib.parse.quote(title)
|
||||||
|
print_debug(url)
|
||||||
|
|
||||||
|
# Make the request
|
||||||
|
raw = urllib.request.urlopen(url)
|
||||||
|
data = json.loads(raw.read().decode())
|
||||||
|
|
||||||
|
# Return data
|
||||||
|
if "Error" in data:
|
||||||
|
raise IRCException(data["Error"])
|
||||||
|
|
||||||
|
elif "Search" in data:
|
||||||
|
return data
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise IRCException("An error occurs during movie search")
|
||||||
|
|
||||||
|
|
||||||
|
@hook("cmd_hook", "imdb")
|
||||||
def cmd_imdb(msg):
|
def cmd_imdb(msg):
|
||||||
|
"""View movie details with !imdb <title>"""
|
||||||
if len(msg.cmds) < 2:
|
if len(msg.cmds) < 2:
|
||||||
raise IRCException("precise a movie/serie title!")
|
raise IRCException("precise a movie/serie title!")
|
||||||
|
|
||||||
title = ' '.join(msg.cmds[1:])
|
title = ' '.join(msg.cmds[1:])
|
||||||
|
|
||||||
if re.match("^tt[0-9]{7}$", title) is not None:
|
if re.match("^tt[0-9]{7}$", title) is not None:
|
||||||
url = "http://www.omdbapi.com/?i=%s" % urllib.parse.quote(title)
|
data = get_movie(imdbid=title)
|
||||||
else:
|
else:
|
||||||
url = "http://www.omdbapi.com/?t=%s" % urllib.parse.quote(title)
|
rm = re.match(r"^(.+)\s\(([0-9]{4})\)$", title)
|
||||||
|
if rm is not None:
|
||||||
|
data = get_movie(title=rm.group(1), year=rm.group(2))
|
||||||
|
else:
|
||||||
|
data = get_movie(title=title)
|
||||||
|
|
||||||
print_debug(url)
|
|
||||||
|
|
||||||
response = urllib.request.urlopen(url)
|
|
||||||
data = json.loads(response.read().decode())
|
|
||||||
|
|
||||||
if "Error" in data:
|
|
||||||
raise IRCException(data["Error"])
|
|
||||||
|
|
||||||
elif "Response" in data and data["Response"] == "True":
|
|
||||||
res = Response(msg.sender, channel=msg.channel,
|
res = Response(msg.sender, channel=msg.channel,
|
||||||
title="%s (%s)" % (data['Title'], data['Year']),
|
title="%s (%s)" % (data['Title'], data['Year']),
|
||||||
nomore="No more information, more at http://www.imdb.com/title/%s" % data['imdbID'])
|
nomore="No more information, more at http://www.imdb.com/title/%s" % data['imdbID'])
|
||||||
@ -45,28 +95,14 @@ def cmd_imdb(msg):
|
|||||||
% (data['Type'], data['Country'], data['Released'], data['Genre'], data['Director'], data['Writer'], data['Actors']))
|
% (data['Type'], data['Country'], data['Released'], data['Genre'], data['Director'], data['Writer'], data['Actors']))
|
||||||
return res
|
return res
|
||||||
|
|
||||||
else:
|
@hook("cmd_hook", "imdbs")
|
||||||
raise IRCException("An error occurs during movie search")
|
|
||||||
|
|
||||||
|
|
||||||
@hook("cmd_hook", "imdbs", help="!imdbs <approximative title> to search a movie title")
|
|
||||||
def cmd_search(msg):
|
def cmd_search(msg):
|
||||||
url = "http://www.omdbapi.com/?s=%s" % urllib.parse.quote(' '.join(msg.cmds[1:]))
|
"""!imdbs <approximative title> to search a movie title"""
|
||||||
print_debug(url)
|
|
||||||
|
|
||||||
raw = urllib.request.urlopen(url)
|
data = find_movies(' '.join(msg.cmds[1:]))
|
||||||
data = json.loads(raw.read().decode())
|
|
||||||
|
|
||||||
if "Error" in data:
|
|
||||||
raise IRCException(data["Error"])
|
|
||||||
|
|
||||||
elif "Search" in data:
|
|
||||||
movies = list()
|
movies = list()
|
||||||
|
|
||||||
for m in data['Search']:
|
for m in data['Search']:
|
||||||
movies.append("\x02%s\x0F (%s of %s)" % (m['Title'], m['Type'], m['Year']))
|
movies.append("\x02%s\x0F (%s of %s)" % (m['Title'], m['Type'], m['Year']))
|
||||||
|
|
||||||
return Response(msg.sender, movies, title="Titles found", channel=msg.channel)
|
return Response(msg.sender, movies, title="Titles found", channel=msg.channel)
|
||||||
|
|
||||||
else:
|
|
||||||
raise IRCException("An error occurs during movie search")
|
|
||||||
|
Loading…
Reference in New Issue
Block a user