Skip to content

Commit

Permalink
Merge pull request #4 from zeroquinc:trakt-tasks
Browse files Browse the repository at this point in the history
feature: support for trakt rating embeds in task form
  • Loading branch information
zeroquinc authored May 24, 2024
2 parents a607f61 + 05a7021 commit 1c3b198
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 5 deletions.
35 changes: 34 additions & 1 deletion api/trakt/models/rating.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
from dateutil.parser import parse
from dateutil.tz import tzlocal

from src.tmdb.client import TMDb

class Rating:
def __init__(self, data):
self.type = data['type']
self.rated = data['rating']
self.rated_at = parse(data['rated_at']).astimezone(tzlocal()) # Convert to local timezone
self.date = self.rated_at.strftime("%d/%m/%Y %H:%M:%S") # Convert to human readable format
self.show_title = None
self.season_id = None
self.episode_id = None
self.trakt_id = None
self.imdb_id = None
self.tmdb_id = None
self.tvdb_id = None
self.poster = None
self.slug = None
media = create_media(data)
self.set_media_attributes(media)

def set_media_attributes(self, media):
self.title = media.title
self.year = media.year
self.poster = media.poster

if isinstance(media, (Episode, Season)):
self.show_title = media.show_title
Expand All @@ -36,6 +46,11 @@ class Movie:
def __init__(self, data):
self.title = data['movie']['title']
self.year = data['movie']['year']
self.slug = data['movie']['ids']['slug']
self.trakt_id = data['movie']['ids']['trakt']
self.imdb_id = data['movie']['ids']['imdb']
self.tmdb_id = data['movie']['ids']['tmdb']
self.poster = TMDb.movie_poster_path(self.tmdb_id)

class Episode:
def __init__(self, data):
Expand All @@ -44,15 +59,33 @@ def __init__(self, data):
self.season_id = "{:02}".format(data['episode']['season']) # Format as two-digit number
self.episode_id = "{:02}".format(data['episode']['number']) # Format as two-digit number
self.year = data['show']['year']
self.slug = data['show']['ids']['slug']
self.trakt_id = data['show']['ids']['trakt']
self.imdb_id = data['show']['ids']['imdb']
self.tmdb_id = data['show']['ids']['tmdb']
self.tvdb_id = data['show']['ids']['tvdb']
self.poster = TMDb.show_poster_path(self.tvdb_id)

class Season:
def __init__(self, data):
self.title = data['show']['title']
self.show_title = data['show']['title']
self.season_id = "{:02}".format(data['season']['number']) # Format as two-digit number
self.year = data['show']['year']
self.slug = data['show']['ids']['slug']
self.trakt_id = data['show']['ids']['trakt']
self.imdb_id = data['show']['ids']['imdb']
self.tmdb_id = data['show']['ids']['tmdb']
self.tvdb_id = data['show']['ids']['tvdb']
self.poster = TMDb.show_poster_path(self.tvdb_id)

class Show:
def __init__(self, data):
self.title = data['show']['title']
self.year = data['show']['year']
self.year = data['show']['year']
self.slug = data['show']['ids']['slug']
self.trakt_id = data['show']['ids']['trakt']
self.imdb_id = data['show']['ids']['imdb']
self.tmdb_id = data['show']['ids']['tmdb']
self.tvdb_id = data['show']['ids']['tvdb']
self.poster = TMDb.show_poster_path(self.tvdb_id)
11 changes: 7 additions & 4 deletions src/discord/bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import discord
from discord.ext import commands, tasks

from config.config import DELAY_START
from discord.ext import commands

from utils.custom_logger import logger
from utils.datetime import get_times
Expand Down Expand Up @@ -38,7 +36,12 @@ async def on_ready(self):
logger.info(
f'Logged in as {self.bot.user.name} ({self.bot.user.id}) and is ready!'
)


# Load the tasks cog
logger.info('Loading tasks cog')
await self.bot.load_extension('src.discord.cogs.tasks')

# Load commands
@self.bot.command()
async def favorite(ctx, username: str = None):
if username is None:
Expand Down
Empty file added src/discord/cogs/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions src/discord/cogs/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from discord.ext import tasks, commands

from utils.custom_logger import logger
from src.trakt.functions import process_ratings

class TasksCog(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
self.trakt_ratings.start()

@tasks.loop(minutes=60)
async def trakt_ratings(self):
ratings_channel = self.bot.get_channel(1052967176828616724)
try:
await process_ratings(ratings_channel, 'desiler')
except Exception as e:
logger.error(f'Error processing recent Trakt ratings: {e}')

async def setup(bot):
logger.info('Cogs have been loaded')
await bot.add_cog(TasksCog(bot))
4 changes: 4 additions & 0 deletions src/discord/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def add_field(self, name, value, inline=True):
def set_footer(self, text, icon_url=None):
self.embed.set_footer(text=text, icon_url=icon_url)
return self

def set_thumbnail(self, url):
self.embed.set_thumbnail(url=url)
return self

def build(self):
return self.embed
Expand Down
42 changes: 42 additions & 0 deletions src/tmdb/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import requests

from config.globals import TMDB_API_KEY
from utils.custom_logger import logger

class TMDb:
BASE_URL = "https://api.themoviedb.org/3"
API_KEY = TMDB_API_KEY

# Fetches the show poster path from TMDb
@classmethod
def show_poster_path(cls, tvdb_id):
try:
response = requests.get(
f"{cls.BASE_URL}/find/{tvdb_id}",
params={
"api_key": cls.API_KEY,
"external_source": "tvdb_id"
}
)
response.raise_for_status()
data = response.json()
if (tv_results := data.get('tv_results', [])):
return f"https://image.tmdb.org/t/p/original{tv_results[0].get('poster_path')}"
except Exception as e:
logger.error(f"Failed to fetch show poster path from TMDb: {e}")
return None

# Fetches the movie poster path from TMDb
@classmethod
def movie_poster_path(cls, tmdb_id):
try:
response = requests.get(
f"{cls.BASE_URL}/movie/{tmdb_id}",
params={"api_key": cls.API_KEY}
)
response.raise_for_status()
data = response.json()
return f"https://image.tmdb.org/t/p/original{data.get('poster_path')}"
except Exception as e:
logger.error(f"Failed to fetch movie poster path from TMDb: {e}")
return None
18 changes: 18 additions & 0 deletions src/trakt/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from datetime import datetime, timedelta

from src.discord.embed import EmbedBuilder
from api.trakt.client import TraktClient

async def process_ratings(ratings_channel, username):
client = TraktClient()
user = client.user(username)
now = datetime.now()
one_hour_ago = now - timedelta(hours=30)
ratings = user.get_ratings(start_time=one_hour_ago, end_time=now)

for rating in ratings:
description = f"**{rating.show_title} (S{rating.season_id}E{rating.episode_id})**\n{rating.title}\n\n {username} rated {rating.rated} :star:"
embed_builder = EmbedBuilder(description=description, color=0xFF0000)
embed_builder.set_footer(text=rating.date)
embed_builder.set_thumbnail(url=rating.poster)
await embed_builder.send_embed(ratings_channel)

0 comments on commit 1c3b198

Please sign in to comment.