Skip to content

Commit

Permalink
Merge branch 'main' into patch2
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroquinc committed May 25, 2024
2 parents f823a93 + ae90808 commit d7e5f05
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 51 deletions.
6 changes: 5 additions & 1 deletion api/trakt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ def _get(self, endpoint, params=None):

def user(self, username):
from .endpoints.user import User
return User(self, username)
return User(self, username)

def shows(self):
from .endpoints.shows import Shows
return Shows(self)
18 changes: 18 additions & 0 deletions api/trakt/endpoints/shows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from api.trakt.models.show import Show

class Shows:
def __init__(self, client):
self.client = client

def _get_show_list(self, endpoint):
params = {}
response = self.client._get(endpoint, params=params)
return [Show(show) for show in response]

def get_popular(self):
endpoint = 'shows/popular'
return [Show(show) for show in self.client._get(endpoint)]

def get_trending(self):
endpoint = 'shows/trending'
return self._get_show_list(endpoint)
15 changes: 15 additions & 0 deletions api/trakt/models/episode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from src.tmdb.client import TMDb

class Episode:
def __init__(self, data):
self.title = data['episode']['title']
self.show_title = data['show']['title']
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)
40 changes: 14 additions & 26 deletions api/trakt/models/favorite.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
from dateutil.parser import parse
from dateutil.tz import tzlocal

from api.trakt.models.movie import Movie
from api.trakt.models.show import Show
from api.trakt.models.season import Season
from api.trakt.models.episode import Episode

class Favorite:
def __init__(self, data):
self.type = data['type']
self.listed_at = parse(data['listed_at']).astimezone(tzlocal()) # Convert to local timezone
self.date = self.listed_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 @@ -29,29 +42,4 @@ def from_json(cls, data):
def create_media(data):
media_classes = {'movie': Movie, 'episode': Episode, 'season': Season, 'show': Show}
media_type = data['type']
return media_classes.get(media_type, lambda _: None)(data)

class Movie:
def __init__(self, data):
self.title = data['movie']['title']
self.year = data['movie']['year']

class Episode:
def __init__(self, data):
self.title = data['episode']['title']
self.show_title = data['show']['title']
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']

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']

class Show:
def __init__(self, data):
self.title = data['show']['title']
self.year = data['show']['year']
return media_classes.get(media_type, lambda _: None)(data)
11 changes: 11 additions & 0 deletions api/trakt/models/movie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from src.tmdb.client import TMDb

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)
5 changes: 4 additions & 1 deletion api/trakt/models/rating.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from dateutil.parser import parse
from dateutil.tz import tzlocal

from api.tmdb.client import TMDb
from api.trakt.models.movie import Movie
from api.trakt.models.show import Show
from api.trakt.models.season import Season
from api.trakt.models.episode import Episode

class Rating:
def __init__(self, data):
Expand Down
14 changes: 14 additions & 0 deletions api/trakt/models/season.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from src.tmdb.client import TMDb

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)
13 changes: 13 additions & 0 deletions api/trakt/models/show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from src.tmdb.client import TMDb

class Show:
def __init__(self, data):
show_data = data['show'] if 'show' in data else data
self.title = show_data['title']
self.year = show_data['year']
self.slug = show_data['ids']['slug']
self.trakt_id = show_data['ids']['trakt']
self.imdb_id = show_data['ids']['imdb']
self.tmdb_id = show_data['ids']['tmdb']
self.tvdb_id = show_data['ids']['tvdb']
self.poster = TMDb.show_poster_path(self.tvdb_id)
13 changes: 12 additions & 1 deletion src/discord/cogs/tasks.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
from discord.ext import tasks, commands

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

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

# Task to process recent Trakt ratings
@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}')

# Task to process recent Trakt favorites
@tasks.loop(hours=24)
async def trakt_favorites(self):
favorites_channel = self.bot.get_channel(1052967176828616724)
try:
await process_favorites(favorites_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')
Expand Down
33 changes: 29 additions & 4 deletions src/trakt/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from src.discord.embed import EmbedBuilder
from api.trakt.client import TraktClient
from utils.datetime import get_times

# Function to process recent ratings and send them to a specified Discord channel as embeds
async def process_ratings(ratings_channel, username):
client = TraktClient()
user = client.user(username)
Expand All @@ -11,10 +13,10 @@ async def process_ratings(ratings_channel, username):

async def process_rating(rating):
description_formats = {
'show': f"**{rating.show_title}**\n{rating.title}\n\n {username} rated this {rating.type} with {rating.rated} :star:",
'season': f"**{rating.show_title}**\nSeason {rating.season_id}\n\n {username} rated this {rating.type} with {rating.rated} :star:",
'episode': f"**{rating.show_title} (S{rating.season_id}E{rating.episode_id})**\n{rating.title}\n\n {username} rated this {rating.type} with {rating.rated} :star:",
'movie': f"**{rating.title} ({rating.year})**\n\n {username} rated this {rating.type} with {rating.rated} :star:"
'show': f"**{rating.title}**\n\n{username} rated this {rating.type}\n{rating.rated} :star:",
'season': f"**{rating.show_title}**\nSeason {rating.season_id}\n\n{username} rated this {rating.type}\n{rating.rated} :star:",
'episode': f"**{rating.show_title} (S{rating.season_id}E{rating.episode_id})**\n{rating.title}\n\n{username} rated this {rating.type}\n{rating.rated} :star:",
'movie': f"**{rating.title} ({rating.year})**\n\n{username} rated this {rating.type}\n{rating.rated} :star:"
}
description = description_formats[rating.type]
embed_builder = EmbedBuilder(description=description, color=0xFF0000)
Expand All @@ -24,3 +26,26 @@ async def process_rating(rating):

for rating in ratings:
await process_rating(rating)

# Function to process recent favorites and send them to a specified Discord channel as embeds
async def process_favorites(favorites_channel, username):
client = TraktClient()
user = client.user(username)
now, one_hour_ago = get_times()
favorites = sorted(user.get_favorites(start_time=one_hour_ago, end_time=now), key=lambda favorite: favorite.date)

async def process_favorite(favorite):
description_formats = {
'show': f"**{favorite.title}**\n\n{username} favorited this {favorite.type}",
'season': f"**{favorite.show_title}**\nSeason {favorite.season_id}\n\n{username} favorited this {favorite.type}",
'episode': f"**{favorite.show_title} (S{favorite.season_id}E{favorite.episode_id})**\n{favorite.title}\n\n{username} favorited this {favorite.type}",
'movie': f"**{favorite.title} ({favorite.year})**\n\n{username} favorited this {favorite.type}"
}
description = description_formats[favorite.type]
embed_builder = EmbedBuilder(description=description, color=0xFF0000)
embed_builder.set_footer(text=favorite.date)
embed_builder.set_thumbnail(url=favorite.poster)
await embed_builder.send_embed(favorites_channel)

for favorite in favorites:
await process_favorite(favorite)
24 changes: 6 additions & 18 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
from datetime import datetime, timedelta
from api.trakt.client import TraktClient

client = TraktClient()
user = client.user('desiler')
shows_instance = client.shows()

# Get the current time and the time one hour ago
now = datetime.now()
one_hour_ago = now - timedelta(hours=1200)

#ratings = user.get_ratings(start_time=one_hour_ago, end_time=now)
#ratings = user.get_ratings()
#for rating in ratings:
favorites = user.get_favorites(start_time=one_hour_ago, end_time=now)
for favorite in favorites:
print(f"Title: {favorite.title}")
print(f"Media type: {favorite.type}")
print(f"Season: {favorite.season_id}")
print(f"Episode: {favorite.episode_id}")
print(f"Listed at: {favorite.listed_at}")
print(f"Show title: {favorite.show_title}")
print()
for show in shows_instance.get_trending():
print(show.title)

for show in shows_instance.get_popular():
print(show.title)

0 comments on commit d7e5f05

Please sign in to comment.