aboutsummaryrefslogtreecommitdiffstats
path: root/bot/cogs/movie.py
blob: 4571400596e1c994b86bee0672449900cc52ddd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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))