diff options
author | Parker <contact@pkrm.dev> | 2025-01-22 16:07:47 -0600 |
---|---|---|
committer | Parker <contact@pkrm.dev> | 2025-01-22 16:07:47 -0600 |
commit | 557a646d65326065b60ea729034d4dbf4069749d (patch) | |
tree | c0618c25fff81257cc7645b1b761833202787431 /code/cogs | |
parent | 7092bd81c71c92296724bfcc5a6a54dac07727c1 (diff) | |
parent | b48f2a1eff20996901e8db40d68c880a5d7f053f (diff) |
Merge branch 'dev'
Diffstat (limited to 'code/cogs')
-rw-r--r-- | code/cogs/newaccount.py | 35 | ||||
-rw-r--r-- | code/cogs/request.py | 14 | ||||
-rw-r--r-- | code/cogs/status.py | 87 |
3 files changed, 66 insertions, 70 deletions
diff --git a/code/cogs/newaccount.py b/code/cogs/newaccount.py index 8e17cda..1fca84c 100644 --- a/code/cogs/newaccount.py +++ b/code/cogs/newaccount.py @@ -1,9 +1,10 @@ import discord from discord import app_commands from discord.ext import commands -import sqlite3 +from utils.database import Session from utils.jellyfin_create import create_jellyfin_account +from utils.models import JellyfinAccounts from utils.config import ( JELLYFIN_PUBLIC_URL, JELLYFIN_ENABLED, @@ -19,15 +20,15 @@ class NewAccount(commands.Cog): @app_commands.check(lambda inter: JELLYFIN_ENABLED) async def newaccount(self, interaction: discord.Interaction) -> None: """Create a new temporary Jellyfin account""" + # Defer in case it takes too long + await interaction.response.defer(ephemeral=True) # Make sure the user doesn't already have an account - db = sqlite3.connect("data/cordarr.db") - cursor = db.cursor() - cursor.execute( - "SELECT * FROM jellyfin_accounts WHERE user_id = ?", - (interaction.user.id,), - ) - account = cursor.fetchone() - db.close() + with Session() as session: + account = ( + session.query(JellyfinAccounts) + .filter(JellyfinAccounts.user_id == interaction.user.id) + .first() + ) # Account already allocated if account: embed = discord.Embed( @@ -39,9 +40,7 @@ class NewAccount(commands.Cog): ), color=0xD01B86, ) - return await interaction.response.send_message( - embed=embed, ephemeral=True - ) + return await interaction.followup.send(embed=embed) # Create a new Jellyfin account for the user response = create_jellyfin_account(interaction.user.id) @@ -54,17 +53,15 @@ class NewAccount(commands.Cog): ), color=0xD01B86, ) - await interaction.response.send_message( - embed=embed, ephemeral=True - ) + await interaction.followup.send(embed=embed) # Send the user their account information embed = discord.Embed( title="Jellyfin Account Information", description=( # fmt: off - "Here is your temporary account information.\n\n", - f"**Server URL:** `[{JELLYFIN_PUBLIC_URL}]({JELLYFIN_PUBLIC_URL})`\n" + "Here is your temporary account information.\n\n" + f"**Server URL:** `{JELLYFIN_PUBLIC_URL}`\n" f"**Username:** `{response[0]}`\n" f"**Password:** `{response[1]}`\n\n" "Your account will be automatically deleted in" @@ -84,9 +81,7 @@ class NewAccount(commands.Cog): ), color=0xD01B86, ) - return await interaction.response.send_message( - embed=embed, ephemeral=True - ) + return await interaction.followup.send(embed=embed) async def setup(bot): diff --git a/code/cogs/request.py b/code/cogs/request.py index 2ad71ba..eeeb467 100644 --- a/code/cogs/request.py +++ b/code/cogs/request.py @@ -31,6 +31,8 @@ class Request(commands.Cog): name: str, ) -> None: """Request a movie or tv show to be added to the library""" + # Could take a sec. so defer the response + await interaction.response.defer(ephemeral=True) # Get matching content from relevant service if form == "Movie": content_data = get_content( @@ -54,9 +56,7 @@ class Request(commands.Cog): ), color=0xD01B86, ) - return await interaction.response.send_message( - embed=embed, ephemeral=True - ) + return await interaction.followup.send(embed=embed, ephemeral=True) if content_data == "ALREADY ADDED": embed = discord.Embed( @@ -70,9 +70,7 @@ class Request(commands.Cog): ), color=0xD01B86, ) - return await interaction.response.send_message( - embed=embed, ephemeral=True - ) + return await interaction.followup.send(embed=embed, ephemeral=True) embed = discord.Embed( title="Results Found", @@ -103,9 +101,7 @@ class Request(commands.Cog): SONARR_QUALITY_PROFILE_ID, ) - await interaction.response.send_message( - embed=embed, view=view, ephemeral=True - ) + await interaction.followup.send(embed=embed, view=view, ephemeral=True) async def setup(bot): diff --git a/code/cogs/status.py b/code/cogs/status.py index 90d0c8b..ef044cc 100644 --- a/code/cogs/status.py +++ b/code/cogs/status.py @@ -2,8 +2,9 @@ import discord from discord import app_commands from discord.ext import commands import requests -import sqlite3 +from utils.models import Requests +from utils.database import Session from utils.config import ( RADARR_HOST_URL, RADARR_HEADERS, @@ -20,17 +21,20 @@ class Status(commands.Cog): async def status(self, interaction: discord.Interaction) -> None: """Get the status of the movies you have requested""" # Defer the response - await interaction.response.defer() - - db = sqlite3.connect("data/cordarr.db") - cursor = db.cursor() - cursor.execute( - "SELECT title, release_year, local_id, tmdbid, tvdbid FROM" - " requests WHERE user_id = ?", - (interaction.user.id,), - ) - requested_content = cursor.fetchall() - db.close() + await interaction.response.defer(ephemeral=True) + + with Session() as session: + requested_content = ( + session.query( + Requests.title, + Requests.release_year, + Requests.local_id, + Requests.tmdbid, + Requests.tvdbid, + ) + .filter(Requests.user_id == interaction.user.id) + .all() + ) # No content requested if len(requested_content) == 0: @@ -42,9 +46,7 @@ class Status(commands.Cog): ), color=0xD01B86, ) - return await interaction.response.send_message( - embed=embed, ephemeral=True - ) + return await interaction.followup.send(embed=embed) # Create template embed embed = discord.Embed( @@ -76,8 +78,8 @@ class Status(commands.Cog): embed.description += radarr_desc + sonarr_desc + non_queue_desc - # Send the embed - await interaction.response.send_message(embed=embed, ephemeral=True) + # Send the follow-up message + await interaction.followup.send(embed=embed) def unpack_content(self, requested_content: list) -> tuple: """ @@ -94,18 +96,18 @@ class Status(commands.Cog): sonarr_content_info = {} for content in requested_content: - title, (release_year), local_id, tmdbid, tvdbid = content + title, release_year, local_id, tmdbid, tvdbid = content if tmdbid is not None: - radarr_content_info[int(local_id)] = { + radarr_content_info[local_id] = { "title": title, - "release_year": int(release_year), - "tmdbid": int(tmdbid), + "release_year": release_year, + "tmdbid": tmdbid, } else: - sonarr_content_info[int(local_id)] = { + sonarr_content_info[local_id] = { "title": title, - "release_year": int(release_year), - "tvdbid": int(tvdbid), + "release_year": release_year, + "tvdbid": tvdbid, } return radarr_content_info, sonarr_content_info @@ -171,7 +173,7 @@ class Status(commands.Cog): for content in requested_content: title, release_year, local_id, tmdbid, _ = content # If not in queue - if int(local_id) not in added_ids: + if local_id not in added_ids: # Pull the movie data from the service if tmdbid is not None: data = requests.get( @@ -187,23 +189,26 @@ class Status(commands.Cog): # If the movie has a file, then it has finished downloading if data.get("hasFile", True): # Remove from database - db = sqlite3.connect("data/cordarr.db") - cursor = db.cursor() - cursor.execute( - "DELETE FROM requests WHERE user_id = ? AND" - " local_id = ?", - (user_id, int(local_id)), - ) - db.commit() - db.close() + with Session() as session: + request = ( + session.query(Requests) + .filter(Requests.user_id == user_id) + .filter(Requests.local_id == local_id) + .first() + ) + session.delete(request) + session.commit() + # If series and only a portion of episodes have been downloaded - if data.get("statistics").get("percentOfEpisodes"): - description += ( - f"\n**{title} ({release_year})** - Status: `NOT" - " FOUND" - f" ({int(data['statistics']['percentOfEpisodes'])}%" - " of eps.)`" - ) + # If data["statistics"] exists and is not None + if "statistics" in data and data["statistics"] != None: + if "percentOfEpisodes" in data["statistics"]: + description += ( + f"\n**{title} ({release_year})** - Status: `NOT" + " FOUND" + f" ({int(data['statistics']['percentOfEpisodes'])}%" + " of eps.)`" + ) # All other scenarios, download not found else: description += ( |