2014-08-27 23:39:31 +00:00
|
|
|
"""Show many information about a movie or serie"""
|
|
|
|
|
2015-12-02 00:18:15 +00:00
|
|
|
# PYTHON STUFFS #######################################################
|
|
|
|
|
2014-08-26 14:38:30 +00:00
|
|
|
import re
|
2014-12-17 06:32:34 +00:00
|
|
|
import urllib.parse
|
2014-07-23 12:54:00 +00:00
|
|
|
|
2017-08-25 22:14:14 +00:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2015-10-30 20:57:45 +00:00
|
|
|
from nemubot.exception import IMException
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.hooks import hook
|
|
|
|
from nemubot.tools import web
|
2014-08-12 18:10:19 +00:00
|
|
|
|
2017-08-27 16:22:53 +00:00
|
|
|
from nemubot.module.more import Response
|
2014-09-26 16:00:22 +00:00
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2015-12-02 00:18:15 +00:00
|
|
|
# MODULE CORE #########################################################
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2017-08-25 22:14:14 +00:00
|
|
|
def get_movie_by_id(imdbid):
|
2014-08-28 00:04:33 +00:00
|
|
|
"""Returns the information about the matching movie"""
|
|
|
|
|
2017-08-25 22:14:14 +00:00
|
|
|
url = "http://www.imdb.com/title/" + urllib.parse.quote(imdbid)
|
|
|
|
soup = BeautifulSoup(web.getURLContent(url))
|
|
|
|
|
|
|
|
return {
|
|
|
|
"imdbID": imdbid,
|
|
|
|
"Title": soup.body.find(attrs={"itemprop": "name"}).next_element.strip(),
|
|
|
|
"Year": soup.body.find(id="titleYear").find("a").text.strip() if soup.body.find(id="titleYear") else ", ".join([y.text.strip() for y in soup.body.find(attrs={"class": "seasons-and-year-nav"}).find_all("div")[3].find_all("a")[:-1]]),
|
|
|
|
"Duration": soup.body.find_all(attrs={"itemprop": "duration"})[-1].text.strip(),
|
|
|
|
"imdbRating": soup.body.find(attrs={"itemprop": "ratingValue"}).text.strip(),
|
|
|
|
"imdbVotes": soup.body.find(attrs={"itemprop": "ratingCount"}).text.strip(),
|
|
|
|
"Plot": re.sub(r"\s+", " ", soup.body.find(id="titleStoryLine").find(attrs={"itemprop": "description"}).text).strip(),
|
|
|
|
|
|
|
|
"Type": "TV Series" if soup.find(attrs={"class": "np_episode_guide"}) else "Movie",
|
|
|
|
"Country": ", ".join([c.find("a").text.strip() for c in soup.body.find(id="titleDetails").find_all(attrs={"class": "txt-block"}) if c.text.find("Country") != -1]),
|
|
|
|
"Released": soup.body.find(attrs={"itemprop": "datePublished"}).attrs["content"] if "content" in soup.body.find(attrs={"itemprop": "datePublished"}).attrs else "N\A",
|
|
|
|
"Genre": ", ".join([g.text.strip() for g in soup.body.find_all(attrs={"itemprop": "genre"})[:-1]]),
|
|
|
|
"Director": ", ".join([d.find(attrs={"itemprop": "name"}).text.strip() for d in soup.body.find_all(attrs={"itemprop": "director"})]),
|
|
|
|
"Writer": ", ".join([d.find(attrs={"itemprop": "name"}).text.strip() for d in soup.body.find_all(attrs={"itemprop": "creator"})]),
|
|
|
|
"Actors": ", ".join([d.find(attrs={"itemprop": "name"}).text.strip() for d in soup.body.find_all(attrs={"itemprop": "actors"})]),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def find_movies(title, year=None):
|
2014-08-28 00:04:33 +00:00
|
|
|
"""Find existing movies matching a approximate title"""
|
2014-07-25 10:53:07 +00:00
|
|
|
|
2017-08-25 22:14:14 +00:00
|
|
|
title = title.lower()
|
|
|
|
|
2014-08-28 00:04:33 +00:00
|
|
|
# Built URL
|
2017-08-25 22:14:14 +00:00
|
|
|
url = "https://v2.sg.media-imdb.com/suggests/%s/%s.json" % (urllib.parse.quote(title[0]), urllib.parse.quote(title.replace(" ", "_")))
|
2014-07-23 12:54:00 +00:00
|
|
|
|
2014-08-28 00:04:33 +00:00
|
|
|
# Make the request
|
2017-08-25 22:14:14 +00:00
|
|
|
data = web.getJSON(url, remove_callback=True)
|
2014-08-28 00:04:33 +00:00
|
|
|
|
2017-08-25 22:14:14 +00:00
|
|
|
if year is None:
|
|
|
|
return data["d"]
|
2014-08-28 00:04:33 +00:00
|
|
|
else:
|
2017-08-25 22:14:14 +00:00
|
|
|
return [d for d in data["d"] if "y" in d and str(d["y"]) == year]
|
2014-07-23 12:54:00 +00:00
|
|
|
|
|
|
|
|
2015-12-02 00:18:15 +00:00
|
|
|
# MODULE INTERFACE ####################################################
|
|
|
|
|
|
|
|
@hook.command("imdb",
|
|
|
|
help="View movie/serie details, using OMDB",
|
|
|
|
help_usage={
|
|
|
|
"TITLE": "Look for a movie titled TITLE",
|
|
|
|
"IMDB_ID": "Look for the movie with the given IMDB_ID",
|
|
|
|
})
|
2014-08-28 00:04:33 +00:00
|
|
|
def cmd_imdb(msg):
|
2015-07-10 21:09:54 +00:00
|
|
|
if not len(msg.args):
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("precise a movie/serie title!")
|
2014-08-28 00:04:33 +00:00
|
|
|
|
2015-07-10 21:09:54 +00:00
|
|
|
title = ' '.join(msg.args)
|
2014-07-23 12:54:00 +00:00
|
|
|
|
2014-08-28 00:04:33 +00:00
|
|
|
if re.match("^tt[0-9]{7}$", title) is not None:
|
2017-08-25 22:14:14 +00:00
|
|
|
data = get_movie_by_id(imdbid=title)
|
2014-07-25 10:53:07 +00:00
|
|
|
else:
|
2014-08-28 00:04:33 +00:00
|
|
|
rm = re.match(r"^(.+)\s\(([0-9]{4})\)$", title)
|
|
|
|
if rm is not None:
|
2017-08-25 22:14:14 +00:00
|
|
|
data = find_movies(rm.group(1), year=rm.group(2))
|
2014-08-28 00:04:33 +00:00
|
|
|
else:
|
2017-08-25 22:14:14 +00:00
|
|
|
data = find_movies(title)
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
raise IMException("Movie/series not found")
|
|
|
|
|
|
|
|
data = get_movie_by_id(data[0]["id"])
|
2014-08-28 00:04:33 +00:00
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
res = Response(channel=msg.channel,
|
|
|
|
title="%s (%s)" % (data['Title'], data['Year']),
|
|
|
|
nomore="No more information, more at http://www.imdb.com/title/%s" % data['imdbID'])
|
2014-08-28 00:04:33 +00:00
|
|
|
|
2017-08-25 22:14:14 +00:00
|
|
|
res.append_message("%s \x02genre:\x0F %s; \x02rating\x0F: %s (%s votes); \x02plot\x0F: %s" %
|
|
|
|
(data['Type'], data['Genre'], data['imdbRating'], data['imdbVotes'], data['Plot']))
|
2014-08-28 00:04:33 +00:00
|
|
|
|
2017-08-25 22:14:14 +00:00
|
|
|
res.append_message("%s \x02from\x0F %s \x02released on\x0F %s; \x02directed by:\x0F %s; \x02written by:\x0F %s; \x02main actors:\x0F %s"
|
|
|
|
% (data['Type'], data['Country'], data['Released'], data['Director'], data['Writer'], data['Actors']))
|
2014-08-28 00:04:33 +00:00
|
|
|
return res
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2015-12-02 00:18:15 +00:00
|
|
|
@hook.command("imdbs",
|
|
|
|
help="Search a movie/serie by title",
|
|
|
|
help_usage={
|
|
|
|
"TITLE": "Search a movie/serie by TITLE",
|
|
|
|
})
|
2014-08-28 00:04:33 +00:00
|
|
|
def cmd_search(msg):
|
2015-07-10 21:09:54 +00:00
|
|
|
if not len(msg.args):
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("precise a movie/serie title!")
|
2014-08-28 00:04:33 +00:00
|
|
|
|
2015-07-10 21:09:54 +00:00
|
|
|
data = find_movies(' '.join(msg.args))
|
2014-08-28 00:04:33 +00:00
|
|
|
|
|
|
|
movies = list()
|
2017-08-25 22:14:14 +00:00
|
|
|
for m in data:
|
|
|
|
movies.append("\x02%s\x0F%s with %s" % (m['l'], (" (" + str(m['y']) + ")") if "y" in m else "", m['s']))
|
2014-08-28 00:04:33 +00:00
|
|
|
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response(movies, title="Titles found", channel=msg.channel)
|