aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/valentines/movie_generator.py
blob: 4df9e0d5f7ff2685ff4e768f825d81353f500548 (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
import logging
import random
from os import environ
from urllib import parse

import discord
from discord.ext import commands

TMDB_API_KEY = environ.get("TMDB_API_KEY")

log = logging.getLogger(__name__)


class RomanceMovieFinder(commands.Cog):
    """A Cog that returns a random romance movie suggestion to a user."""

    def __init__(self, bot: commands.Bot):
        self.bot = bot

    @commands.command(name="romancemovie")
    async def romance_movie(self, ctx: commands.Context) -> None:
        """Randomly selects a romance movie and displays information about it."""
        # Selecting a random int to parse it to the page parameter
        random_page = random.randint(0, 20)
        # TMDB api params
        params = {
            "api_key": TMDB_API_KEY,
            "language": "en-US",
            "sort_by": "popularity.desc",
            "include_adult": "false",
            "include_video": "false",
            "page": random_page,
            "with_genres": "10749"
        }
        # The api request url
        request_url = "https://api.themoviedb.org/3/discover/movie?" + parse.urlencode(params)
        async with self.bot.http_session.get(request_url) as resp:
            # Trying to load the json file returned from the api
            try:
                data = await resp.json()
                # Selecting random result from results object in the json file
                selected_movie = random.choice(data["results"])

                embed = discord.Embed(
                    title=f":sparkling_heart: {selected_movie['title']} :sparkling_heart:",
                    description=selected_movie["overview"],
                )
                embed.set_image(url=f"http://image.tmdb.org/t/p/w200/{selected_movie['poster_path']}")
                embed.add_field(name="Release date :clock1:", value=selected_movie["release_date"])
                embed.add_field(name="Rating :star2:", value=selected_movie["vote_average"])
                embed.set_footer(text="This product uses the TMDb API but is not endorsed or certified by TMDb.")
                embed.set_thumbnail(url="https://i.imgur.com/LtFtC8H.png")
                await ctx.send(embed=embed)
            except KeyError:
                warning_message = "A KeyError was raised while fetching information on the movie. The API service" \
                                  " could be unavailable or the API key could be set incorrectly."
                embed = discord.Embed(title=warning_message)
                log.warning(warning_message)
                await ctx.send(embed=embed)


def setup(bot: commands.Bot) -> None:
    """Romance movie Cog load."""
    bot.add_cog(RomanceMovieFinder(bot))