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,
|
2018-12-03 22:55:25 +00:00
|
|
|
"Title": soup.body.find('h1').contents[0].strip(),
|
2019-02-01 16:05:05 +00:00
|
|
|
"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("a")[1:]]),
|
|
|
|
"Duration": soup.body.find(attrs={"class": "title_wrapper"}).find("time").text.strip() if soup.body.find(attrs={"class": "title_wrapper"}).find("time") else None,
|
2018-08-30 05:20:00 +00:00
|
|
|
"imdbRating": soup.body.find(attrs={"class": "ratingValue"}).find("strong").text.strip(),
|
|
|
|
"imdbVotes": soup.body.find(attrs={"class": "imdbRating"}).find("a").text.strip(),
|
|
|
|
"Plot": re.sub(r"\s+", " ", soup.body.find(attrs={"class": "summary_text"}).text).strip(),
|
|
|
|
|
|
|
|
"Type": "TV Series" if soup.find(id="title-episode-widget") else "Movie",
|
2018-12-03 22:55:25 +00:00
|
|
|
"Genre": ", ".join([x.text.strip() for x in soup.body.find(id="titleStoryLine").find_all("a") if x.get("href") is not None and x.get("href")[:21] == "/search/title?genres="]),
|
2018-08-30 05:20:00 +00:00
|
|
|
"Country": ", ".join([x.text.strip() for x in soup.body.find(id="titleDetails").find_all("a") if x.get("href") is not None and x.get("href")[:32] == "/search/title?country_of_origin="]),
|
|
|
|
"Credits": " ; ".join([x.find("h4").text.strip() + " " + (", ".join([y.text.strip() for y in x.find_all("a") if y.get("href") is not None and y.get("href")[:6] == "/name/"])) for x in soup.body.find_all(attrs={"class": "credit_summary_item"})]),
|
2017-08-25 22:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-06-20 05:46:46 +00:00
|
|
|
if "d" not in data:
|
|
|
|
return None
|
|
|
|
elif year is None:
|
2017-08-25 22:14:14 +00:00
|
|
|
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']))
|
2018-08-30 05:20:00 +00:00
|
|
|
res.append_message("%s \x02from\x0F %s; %s"
|
|
|
|
% (data['Type'], data['Country'], data['Credits']))
|
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)
|