diff options
-rw-r--r-- | bot/cogs/movie.py | 65 | ||||
-rw-r--r-- | bot/cogs/rand_movie.py | 66 |
2 files changed, 65 insertions, 66 deletions
diff --git a/bot/cogs/movie.py b/bot/cogs/movie.py new file mode 100644 index 00000000..45714005 --- /dev/null +++ b/bot/cogs/movie.py @@ -0,0 +1,65 @@ +import requests +import random +from discord.ext import commands +from os import environ + +OMDB_API_KEY = environ.get('OMDB_API_KEY') + + +class Movie: + """ + Prints the details of a random scary movie to discord chat + """ + + def __init__(self, bot): + self.bot = bot + + @commands.command(name='movie', aliases=['scary_movie'], brief='Pick me 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) + + @staticmethod + async def select_movie(): + """ + Selects a random movie and returns a json of movie details from omdb + """ + + # 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' + } + 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) + + selection = random.choice(movies) + + omdb_params = { + 'apikey': OMDB_API_KEY, + 'i': selection + } + response = requests.get('http://www.omdbapi.com/', omdb_params) + + return response.json() + + @staticmethod + async def format_metadata(movie): + """ + Formats raw omdb data to be displayed 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 + + +def setup(bot): + bot.add_cog(Movie(bot)) diff --git a/bot/cogs/rand_movie.py b/bot/cogs/rand_movie.py deleted file mode 100644 index d531b32b..00000000 --- a/bot/cogs/rand_movie.py +++ /dev/null @@ -1,66 +0,0 @@ -import requests -import random -from pprint import pprint -from discord.ext import commands - -class Movies: - - """ - TODO. - """ - - def __init__(self, bot): - self.bot = bot - - @commands.command(name='movie', aliases=['rand_movie', 'movie'], brief='Pick a scary movie') - async def repository(self, ctx): - await ctx.send('https://github.com/discord-python/hacktoberbot') - - @staticmethod - def select_movie(api_key): - params = { - 'apikey': api_key, - 'type': 'movie', - 's': 'halloween' - } - - response = requests.get('http://www.omdbapi.com/', params) - - movies = [] - - for movie in response.json().get('Search'): - movie_id = movie.get('imdbID') - movies.append(movie_id) - - selection = random.choice(movies) - - params = { - 'apikey': api_key, - 'i': selection - } - - response = requests.get('http://www.omdbapi.com/', params) - - return response.json() - - - # @commands.group(name='git', invoke_without_command=True) - # async def github(self, ctx): - # """ - # A command group with the name git. You can now create sub-commands such as git commit. - # """ - # - # await ctx.send('Resources to learn **Git**: https://try.github.io/.') - # - # @github.command() - # async def commit(self, ctx): - # """ - # A command that belongs to the git command group. Invoked using git commit. - # """ - # - # await ctx.send('`git commit -m "First commit"` commits tracked changes.') - - -# Required in order to load the cog, use the class name in the add_cog function. -def setup(bot): - bot.add_cog(Template(bot)) |