aboutsummaryrefslogtreecommitdiffstats
path: root/bot/cogs/movie.py
diff options
context:
space:
mode:
authorGravatar Ryan Gables <[email protected]>2018-10-09 01:16:38 -0700
committerGravatar Ryan Gables <[email protected]>2018-10-09 01:16:38 -0700
commiteee33d3d294686a99e6410ad9d43ff155ee763dd (patch)
treeb63d7a232b585dabbe10c48a7de3f9aa3a27b9b2 /bot/cogs/movie.py
parentmade apikey env var + formatted omdb response (diff)
Embeds movie data from TMDb
Diffstat (limited to 'bot/cogs/movie.py')
-rw-r--r--bot/cogs/movie.py88
1 files changed, 61 insertions, 27 deletions
diff --git a/bot/cogs/movie.py b/bot/cogs/movie.py
index 45714005..65c96075 100644
--- a/bot/cogs/movie.py
+++ b/bot/cogs/movie.py
@@ -1,64 +1,98 @@
import requests
import random
-from discord.ext import commands
from os import environ
+from discord.ext import commands
+from discord import Embed
-OMDB_API_KEY = environ.get('OMDB_API_KEY')
+TMDB_API_KEY = environ.get('TMDB_API_KEY')
+TMDB_TOKEN = environ.get('TMDB_TOKEN')
class Movie:
"""
- Prints the details of a random scary movie to discord chat
+ Selects a random scary movie and embeds info into discord chat
"""
def __init__(self, bot):
self.bot = bot
- @commands.command(name='movie', aliases=['scary_movie'], brief='Pick me a scary movie')
+ @commands.command(name='movie', alias=['tmdb'], brief='Pick a scary movie')
async def random_movie(self, ctx):
selection = await self.select_movie()
movie_details = await self.format_metadata(selection)
- await ctx.send(movie_details)
+ await ctx.send(embed=movie_details)
@staticmethod
async def select_movie():
"""
- Selects a random movie and returns a json of movie details from omdb
+ Selects a random movie and returns a json of movie details from TMDb
"""
- # TODO: Come up w/ a scary movie list to select from. Currently returns random Halloween movie
- omdb_params = {
- 'apikey': OMDB_API_KEY,
- 'type': 'movie',
- 's': 'halloween'
+ url = 'https://api.themoviedb.org/4/discover/movie'
+ params = {
+ 'with_genres': '27',
+ 'vote_count.gte': '5'
+ }
+ headers = {
+ 'Authorization': 'Bearer ' + TMDB_TOKEN,
+ 'Content-Type': 'application/json;charset=utf-8'
}
- response = requests.get('http://www.omdbapi.com/', omdb_params)
- movies = []
- for movie in response.json().get('Search'):
- movie_id = movie.get('imdbID')
- movies.append(movie_id)
+ # Get total page count of horror movies
+ response = requests.get(url=url, params=params, headers=headers)
+ total_pages = response.json().get('total_pages')
- selection = random.choice(movies)
+ # Get movie details from one random result on a random page
+ params['page'] = random.randint(1, total_pages)
+ response = requests.get(url=url, params=params, headers=headers)
+ selection_id = random.choice(response.json().get('results')).get('id')
- omdb_params = {
- 'apikey': OMDB_API_KEY,
- 'i': selection
- }
- response = requests.get('http://www.omdbapi.com/', omdb_params)
+ # Get full details and credits
+ selection = requests.get(url='https://api.themoviedb.org/3/movie/' + str(selection_id),
+ params={'api_key': TMDB_API_KEY, 'append_to_response': 'credits'})
- return response.json()
+ return selection.json()
@staticmethod
async def format_metadata(movie):
"""
- Formats raw omdb data to be displayed in discord chat
+ Formats raw TMDb data to be embedded in discord chat
"""
- display_text = f"You should watch {movie.get('Title')} ({movie.get('Year')})\n" \
- f"https://www.imdb.com/title/{movie.get('imdbID')}"
- return display_text
+ tmdb_url = 'https://www.themoviedb.org/movie/' + str(movie.get('id'))
+ poster = 'https://image.tmdb.org/t/p/original' + movie.get('poster_path')
+
+ cast = []
+ for actor in movie.get('credits').get('cast')[:3]:
+ cast.append(actor.get('name'))
+
+ director = movie.get('credits').get('crew')[0].get('name')
+
+ rating_count = movie.get('vote_average') / 2
+ rating = ''
+
+ for i in range(int(rating_count)):
+ rating += ':skull:'
+
+ if (rating_count % 1) >= .5:
+ rating += ':bat:'
+
+ embed = Embed(
+ colour=0x01d277,
+ title='**' + movie.get('title') + '**',
+ url=tmdb_url,
+ description=movie.get('overview')
+ )
+ embed.set_image(url=poster)
+ embed.add_field(name='Staring', value=', '.join(cast))
+ embed.add_field(name='Directed by', value=director)
+ embed.add_field(name='Year', value=movie.get('release_date')[:4])
+ embed.add_field(name='Runtime', value=str(movie.get('runtime')) + 'm')
+ embed.add_field(name='Spooky Rating', value=rating)
+ embed.set_footer(text='powered by themoviedb.org')
+
+ return embed
def setup(bot):