-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from zeroquinc:trakt-tasks
feature: support for trakt rating embeds in task form
- Loading branch information
Showing
7 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |