162 lines
4.5 KiB
Python
162 lines
4.5 KiB
Python
|
#! /usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
"""
|
||
|
This module exracts various information from imbd.
|
||
|
It also contains functionality for the local movie database.
|
||
|
"""
|
||
|
import os
|
||
|
import threading
|
||
|
import random
|
||
|
|
||
|
import requests
|
||
|
import bs4
|
||
|
|
||
|
import module
|
||
|
|
||
|
def setup(bot):
|
||
|
bot.memory['movie_lock'] = threading.Lock()
|
||
|
|
||
|
|
||
|
@module.commands('movie', 'tmdb')
|
||
|
@module.example('.movie ThisTitleDoesNotExist', '[MOVIE] Movie not found!')
|
||
|
@module.example('.movie Citizen Kane', '[MOVIE] Title: Citizen Kane | Year: \
|
||
|
1941 | Rating: 8.4 | Genre: Drama, Mystery | IMDB Link: \
|
||
|
http://imdb.com/title/tt0033467')
|
||
|
def movie(bot, trigger):
|
||
|
"""
|
||
|
Returns some information about a movie, like Title, Year, Rating,
|
||
|
Genre and IMDB Link.
|
||
|
"""
|
||
|
if not trigger.group(2):
|
||
|
return
|
||
|
word = trigger.group(2).strip().replace(" ", "+")
|
||
|
|
||
|
api_key = bot.config.movie.tmdb_api_key
|
||
|
uri = "https://api.themoviedb.org/3/search/movie?" + \
|
||
|
f"api_key={api_key}&query={word}"
|
||
|
data = requests.get(uri, timeout=30, verify=True).json()
|
||
|
try:
|
||
|
data = data['results'][0]
|
||
|
except IndexError:
|
||
|
return bot.reply("No results found.")
|
||
|
except KeyError:
|
||
|
print(data)
|
||
|
return bot.reply("An error. Please notify an adult.")
|
||
|
|
||
|
uri = "https://api.themoviedb.org/3/genre/movie/list?" + \
|
||
|
f"api_key={api_key}&language=en-US"
|
||
|
genres = requests.get(uri, timeout=30, verify=True).json()
|
||
|
try:
|
||
|
genres = genres['genres']
|
||
|
except KeyError:
|
||
|
return bot.reply("An error. Please notify an adult.")
|
||
|
|
||
|
movieGenres = []
|
||
|
for genre in genres:
|
||
|
if genre['id'] in data['genre_ids']:
|
||
|
movieGenres.append(genre['name'])
|
||
|
|
||
|
msg = "[\x0304MOVIE\x03] \x0310Title\x03: \x0312" + data['title'] + \
|
||
|
"\x03 | \x0310Year\x03: \x0308" + data['release_date'][:4] + \
|
||
|
"\x03 | \x0310Rating\x03: \x0312" + str(data['vote_average']) + \
|
||
|
"\x03 | \x0310Genre\x03: \x0312" + ", ".join(movieGenres) + \
|
||
|
"\x03 | \x0310TMDb Link\x03: \x0307" + \
|
||
|
"https://www.themoviedb.org/movie/" + str(data['id'])
|
||
|
|
||
|
msg += "\n\x0310Theater release date\x03: \x0308" + data['release_date'] + \
|
||
|
"\x03 | \x0310Physical release date\x03: \x0308" + \
|
||
|
phyiscalRelease(word, data['id'], api_key)
|
||
|
|
||
|
msg += "\n\x0310Overview\x03: " + data['overview']
|
||
|
|
||
|
bot.say(msg)
|
||
|
|
||
|
|
||
|
def phyiscalRelease(word, tmdb_id=None, api_key=None):
|
||
|
"""
|
||
|
Attempts to find a physical US release from TMDb. Failing that, it will
|
||
|
find a date on www.dvdreleasedates.com.
|
||
|
"""
|
||
|
if not tmdb_id:
|
||
|
return "Feature not yet implemented."
|
||
|
|
||
|
uri = f"https://api.themoviedb.org/3/movie/{tmdb_id}/release_dates?" + \
|
||
|
f"api_key={api_key}"
|
||
|
res = requests.get(uri, timeout=30, verify=True)
|
||
|
res.raise_for_status()
|
||
|
try:
|
||
|
releases = res.json()['results']
|
||
|
except KeyError:
|
||
|
return "No results found."
|
||
|
|
||
|
for release in releases:
|
||
|
if release["iso_3166_1"] != "US":
|
||
|
continue
|
||
|
for date in release["release_dates"]:
|
||
|
if date["type"] == 5:
|
||
|
return date["release_date"][:10]
|
||
|
#return "No physical US release found."
|
||
|
return dvdReleaseDates(word)
|
||
|
|
||
|
|
||
|
def dvdReleaseDates(word):
|
||
|
"""
|
||
|
Scrapes www.dvdsreleasedates.com for physical release dates.
|
||
|
"""
|
||
|
uri = f"http://www.dvdsreleasedates.com/search.php?searchStr={word}"
|
||
|
res = requests.get(uri, timeout=30, verify=True)
|
||
|
soup = bs4.BeautifulSoup(res.text, "html.parser")
|
||
|
rDate = soup.title.text[soup.title.text.rfind("Date")+4:]
|
||
|
if not rDate:
|
||
|
rDate = "Not announced."
|
||
|
elif rDate.startswith("rch results for"):
|
||
|
rDate = "Not found."
|
||
|
return rDate.strip()
|
||
|
|
||
|
|
||
|
@module.commands('pickmovie', 'getmovie')
|
||
|
@module.example('.pickmovie', 'Commandos')
|
||
|
def pickMovie(bot, trigger):
|
||
|
"""
|
||
|
Picks a random movie title out of the database.
|
||
|
"""
|
||
|
bot.memory['movie_lock'].acquire()
|
||
|
conn = bot.db.connect()
|
||
|
cur = conn.cursor()
|
||
|
cur.execute("SELECT * FROM movie WHERE times_watched < 1 AND shitpost = 0")
|
||
|
movieList = cur.fetchall()
|
||
|
conn.close()
|
||
|
|
||
|
roll = random.randint(0, len(movieList)-1)
|
||
|
bot.memory['movie_lock'].release()
|
||
|
bot.reply(movieList[roll][0])
|
||
|
|
||
|
|
||
|
@module.require_admin
|
||
|
@module.commands('addmovie')
|
||
|
@module.example('.addmovie Gay Niggers From Outer Space')
|
||
|
def addMovie(bot, trigger):
|
||
|
"""
|
||
|
Adds the specified movie to the movie database.
|
||
|
"""
|
||
|
bot.memory['movie_lock'].acquire()
|
||
|
movie = trigger.group(2)
|
||
|
conn = bot.db.connect()
|
||
|
cur = conn.cursor()
|
||
|
insert = (movie, trigger.nick)
|
||
|
try:
|
||
|
cur.execute("INSERT INTO movie (movie_title, added_by) VALUES(?,?)",
|
||
|
insert)
|
||
|
confirm = "Added movie: " + movie
|
||
|
except sqlite3.IntegrityError:
|
||
|
confirm = "Error: " + movie + " is already in the database."
|
||
|
conn.commit()
|
||
|
conn.close()
|
||
|
bot.memory['movie_lock'].release()
|
||
|
bot.say(confirm)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
from test_tools import run_example_tests
|
||
|
run_example_tests(__file__)
|