From 42e5af85dd55b754d93374263ea7bcee85075f34 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Tue, 20 Nov 2018 15:14:32 +0100 Subject: Fixing flake8 errors, and making the gif post inside an embed. --- bot/cogs/hacktober/halloween_facts.py | 2 +- bot/cogs/hacktober/movie.py | 136 ---------------------------------- bot/cogs/hacktober/scarymovie.py | 136 ++++++++++++++++++++++++++++++++++ bot/cogs/hacktober/spookygif.py | 36 +++++++++ 4 files changed, 173 insertions(+), 137 deletions(-) delete mode 100644 bot/cogs/hacktober/movie.py create mode 100644 bot/cogs/hacktober/scarymovie.py create mode 100644 bot/cogs/hacktober/spookygif.py (limited to 'bot/cogs/hacktober') diff --git a/bot/cogs/hacktober/halloween_facts.py b/bot/cogs/hacktober/halloween_facts.py index bd164e30..7b5b866b 100644 --- a/bot/cogs/hacktober/halloween_facts.py +++ b/bot/cogs/hacktober/halloween_facts.py @@ -39,7 +39,7 @@ class HalloweenFacts: async def _fact_publisher_task(self): """ A background task that runs forever, sending Halloween facts at random to the Discord channel with id equal to - HACKTOBERFEST_CHANNEL_ID every INTERVAL seconds. + HACKTOBER_CHANNEL_ID every INTERVAL seconds. """ facts = list(enumerate(self.halloween_facts)) while True: diff --git a/bot/cogs/hacktober/movie.py b/bot/cogs/hacktober/movie.py deleted file mode 100644 index 925f813f..00000000 --- a/bot/cogs/hacktober/movie.py +++ /dev/null @@ -1,136 +0,0 @@ -import random -from os import environ - -import aiohttp -from discord import Embed -from discord.ext import commands - - -TMDB_API_KEY = environ.get('TMDB_API_KEY') -TMDB_TOKEN = environ.get('TMDB_TOKEN') - - -class Movie: - """ - Selects a random scary movie and embeds info into discord chat - """ - - def __init__(self, bot): - self.bot = bot - - @commands.command(name='movie', alias=['tmdb']) - async def random_movie(self, ctx): - """ - Randomly select a scary movie and display information about it. - """ - selection = await self.select_movie() - movie_details = await self.format_metadata(selection) - - await ctx.send(embed=movie_details) - - @staticmethod - async def select_movie(): - """ - Selects a random movie and returns a json of movie details from TMDb - """ - - 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' - } - - # Get total page count of horror movies - async with aiohttp.ClientSession() as session: - response = await session.get(url=url, params=params, headers=headers) - total_pages = await response.json() - total_pages = total_pages.get('total_pages') - - # Get movie details from one random result on a random page - params['page'] = random.randint(1, total_pages) - response = await session.get(url=url, params=params, headers=headers) - response = await response.json() - selection_id = random.choice(response.get('results')).get('id') - - # Get full details and credits - selection = await session.get( - url='https://api.themoviedb.org/3/movie/' + str(selection_id), - params={'api_key': TMDB_API_KEY, 'append_to_response': 'credits'} - ) - - return await selection.json() - - @staticmethod - async def format_metadata(movie): - """ - Formats raw TMDb data to be embedded in discord chat - """ - - # Build the relevant URLs. - movie_id = movie.get("id") - poster_path = movie.get("poster_path") - tmdb_url = f'https://www.themoviedb.org/movie/{movie_id}' if movie_id else None - poster = f'https://image.tmdb.org/t/p/original{poster_path}' if poster_path else None - - # Get cast names - cast = [] - for actor in movie.get('credits', {}).get('cast', [])[:3]: - cast.append(actor.get('name')) - - # Get director name - director = movie.get('credits', {}).get('crew', []) - if director: - director = director[0].get('name') - - # Determine the spookiness rating - rating = '' - rating_count = movie.get('vote_average', 0) - - if rating_count: - rating_count /= 2 - - for _ in range(int(rating_count)): - rating += ':skull:' - if (rating_count % 1) >= .5: - rating += ':bat:' - - # Try to get year of release and runtime - year = movie.get('release_date', [])[:4] - runtime = movie.get('runtime') - runtime = f"{runtime} minutes" if runtime else None - - # Not all these attributes will always be present - movie_attributes = { - "Directed by": director, - "Starring": ', '.join(cast), - "Running time": runtime, - "Release year": year, - "Spookiness rating": rating, - } - - embed = Embed( - colour=0x01d277, - title='**' + movie.get('title') + '**', - url=tmdb_url, - description=movie.get('overview') - ) - - if poster: - embed.set_image(url=poster) - - # Add the attributes that we actually have data for, but not the others. - for name, value in movie_attributes.items(): - if value: - embed.add_field(name=name, value=value) - - embed.set_footer(text='powered by themoviedb.org') - - return embed - - -def setup(bot): - bot.add_cog(Movie(bot)) diff --git a/bot/cogs/hacktober/scarymovie.py b/bot/cogs/hacktober/scarymovie.py new file mode 100644 index 00000000..97e9f424 --- /dev/null +++ b/bot/cogs/hacktober/scarymovie.py @@ -0,0 +1,136 @@ +import random +from os import environ + +import aiohttp +from discord import Embed +from discord.ext import commands + + +TMDB_API_KEY = environ.get('TMDB_API_KEY') +TMDB_TOKEN = environ.get('TMDB_TOKEN') + + +class ScaryMovie: + """ + Selects a random scary movie and embeds info into discord chat + """ + + def __init__(self, bot): + self.bot = bot + + @commands.command(name='movie', alias=['tmdb']) + async def random_movie(self, ctx): + """ + Randomly select a scary movie and display information about it. + """ + selection = await self.select_movie() + movie_details = await self.format_metadata(selection) + + await ctx.send(embed=movie_details) + + @staticmethod + async def select_movie(): + """ + Selects a random movie and returns a json of movie details from TMDb + """ + + 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' + } + + # Get total page count of horror movies + async with aiohttp.ClientSession() as session: + response = await session.get(url=url, params=params, headers=headers) + total_pages = await response.json() + total_pages = total_pages.get('total_pages') + + # Get movie details from one random result on a random page + params['page'] = random.randint(1, total_pages) + response = await session.get(url=url, params=params, headers=headers) + response = await response.json() + selection_id = random.choice(response.get('results')).get('id') + + # Get full details and credits + selection = await session.get( + url='https://api.themoviedb.org/3/movie/' + str(selection_id), + params={'api_key': TMDB_API_KEY, 'append_to_response': 'credits'} + ) + + return await selection.json() + + @staticmethod + async def format_metadata(movie): + """ + Formats raw TMDb data to be embedded in discord chat + """ + + # Build the relevant URLs. + movie_id = movie.get("id") + poster_path = movie.get("poster_path") + tmdb_url = f'https://www.themoviedb.org/movie/{movie_id}' if movie_id else None + poster = f'https://image.tmdb.org/t/p/original{poster_path}' if poster_path else None + + # Get cast names + cast = [] + for actor in movie.get('credits', {}).get('cast', [])[:3]: + cast.append(actor.get('name')) + + # Get director name + director = movie.get('credits', {}).get('crew', []) + if director: + director = director[0].get('name') + + # Determine the spookiness rating + rating = '' + rating_count = movie.get('vote_average', 0) + + if rating_count: + rating_count /= 2 + + for _ in range(int(rating_count)): + rating += ':skull:' + if (rating_count % 1) >= .5: + rating += ':bat:' + + # Try to get year of release and runtime + year = movie.get('release_date', [])[:4] + runtime = movie.get('runtime') + runtime = f"{runtime} minutes" if runtime else None + + # Not all these attributes will always be present + movie_attributes = { + "Directed by": director, + "Starring": ', '.join(cast), + "Running time": runtime, + "Release year": year, + "Spookiness rating": rating, + } + + embed = Embed( + colour=0x01d277, + title='**' + movie.get('title') + '**', + url=tmdb_url, + description=movie.get('overview') + ) + + if poster: + embed.set_image(url=poster) + + # Add the attributes that we actually have data for, but not the others. + for name, value in movie_attributes.items(): + if value: + embed.add_field(name=name, value=value) + + embed.set_footer(text='powered by themoviedb.org') + + return embed + + +def setup(bot): + bot.add_cog(ScaryMovie(bot)) diff --git a/bot/cogs/hacktober/spookygif.py b/bot/cogs/hacktober/spookygif.py new file mode 100644 index 00000000..1249905d --- /dev/null +++ b/bot/cogs/hacktober/spookygif.py @@ -0,0 +1,36 @@ +import aiohttp +import discord +from discord.ext import commands + +from bot.constants import GIPHY_TOKEN + + +class SpookyGif: + """ + A cog to fetch a random spooky gif from the web! + """ + + def __init__(self, bot): + self.bot = bot + + @commands.command(name="spookygif", aliases=["sgif", "scarygif"]) + async def spookygif(self, ctx): + """ + Fetches a random gif from the GIPHY API and responds with it. + """ + + async with aiohttp.ClientSession() as session: + params = {'api_key': GIPHY_TOKEN, 'tag': 'halloween', 'rating': 'g'} + # Make a GET request to the Giphy API to get a random halloween gif. + async with session.get('http://api.giphy.com/v1/gifs/random', params=params) as resp: + data = await resp.json() + url = data['data']['image_url'] + + embed = discord.Embed(colour=0x9b59b6) + embed.title = "A spooooky gif!" + embed.set_image(url=url) + await ctx.send(embed=embed) + + +def setup(bot): + bot.add_cog(SpookyGif(bot)) -- cgit v1.2.3