Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: move clients to api folder and working sonarr/radarr webhooks #6

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
5 changes: 4 additions & 1 deletion src/radarr/client.py → api/radarr/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from utils.custom_logger import logger

from src.radarr.functions import process_webhook
class RadarrWebhookHandler:
def __init__(self, payload, discord_bot):
self.payload = payload
Expand All @@ -27,4 +28,6 @@ def initialize_global_variables(self):

async def handle_webhook(self):
logger.info(f"Processing Radarr webhook payload for event type: {self.event_type}")
logger.debug(f"Payload: {json.dumps(self.payload, indent=4)}")
logger.debug(f"Payload: {json.dumps(self.payload, indent=4)}")
channel = self.discord_bot.bot.get_channel(1052967176828616724)
await process_webhook(self, channel)
File renamed without changes.
6 changes: 5 additions & 1 deletion src/sonarr/client.py → api/sonarr/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
from utils.custom_logger import logger

from src.sonarr.functions import process_webhook

class SonarrWebhookHandler:
def __init__(self, payload, discord_bot):
self.payload = payload
Expand Down Expand Up @@ -47,4 +49,6 @@ def handle_episode_request(self):

async def handle_webhook(self):
logger.info(f"Processing Sonarr webhook payload for event type: {self.event_type}")
logger.debug(f"Payload: {json.dumps(self.payload, indent=4)}")
logger.debug(f"Payload: {json.dumps(self.payload, indent=4)}")
channel = self.discord_bot.bot.get_channel(1052967176828616724)
await process_webhook(self, channel)
File renamed without changes.
2 changes: 1 addition & 1 deletion api/trakt/models/rating.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dateutil.parser import parse
from dateutil.tz import tzlocal

from src.tmdb.client import TMDb
from api.tmdb.client import TMDb

class Rating:
def __init__(self, data):
Expand Down
6 changes: 5 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from src.discord.bot import DiscordBot

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

async def main():
discord_bot = DiscordBot(DISCORD_TOKEN)
Expand All @@ -17,4 +18,7 @@ async def main():
await webhook.cleanup()

if __name__ == "__main__":
asyncio.run(main())
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("Bot stopped by keyboard interrupt.")
33 changes: 33 additions & 0 deletions src/radarr/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from src.discord.embed import EmbedBuilder
from utils.custom_logger import logger

async def process_webhook(handler, channel):
def build_grab_embed():
embed = EmbedBuilder(description="A grab event has occurred.")
return embed

def build_download_embed():
embed = EmbedBuilder(description="A download event has occurred.")
return embed

def build_application_update_embed():
embed = EmbedBuilder(description="An application update event has occurred.")
return embed

def build_test_embed():
embed = EmbedBuilder(description="A test event has occurred.")
return embed

embed_builders = {
'Grab': build_grab_embed,
'Download': build_download_embed,
'ApplicationUpdate': build_application_update_embed,
'Test': build_test_embed,
}

if handler.event_type not in embed_builders:
logger.error(f"Unknown event type: {handler.event_type}")
return

embed_builder = embed_builders[handler.event_type]()
await embed_builder.send_embed(channel)
33 changes: 33 additions & 0 deletions src/sonarr/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from src.discord.embed import EmbedBuilder
from utils.custom_logger import logger

async def process_webhook(handler, channel):
def build_grab_embed():
embed = EmbedBuilder(description="A grab event has occurred.")
return embed

def build_download_embed():
embed = EmbedBuilder(description="A download event has occurred.")
return embed

def build_application_update_embed():
embed = EmbedBuilder(description="An application update event has occurred.")
return embed

def build_test_embed():
embed = EmbedBuilder(description="A test event has occurred.")
return embed

embed_builders = {
'Grab': build_grab_embed,
'Download': build_download_embed,
'ApplicationUpdate': build_application_update_embed,
'Test': build_test_embed,
}

if handler.event_type not in embed_builders:
logger.error(f"Unknown event type: {handler.event_type}")
return

embed_builder = embed_builders[handler.event_type]()
await embed_builder.send_embed(channel)
Empty file removed src/tmdb/__init__.py
Empty file.
5 changes: 2 additions & 3 deletions src/trakt/functions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from datetime import datetime, timedelta
from utils.datetime import get_times

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=120)
now, one_hour_ago = get_times()
ratings = user.get_ratings(start_time=one_hour_ago, end_time=now)

async def process_rating(rating):
Expand Down
4 changes: 2 additions & 2 deletions src/webhook/hook.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from aiohttp import web

from utils.custom_logger import logger
from src.sonarr.client import SonarrWebhookHandler
from src.radarr.client import RadarrWebhookHandler
from api.sonarr.client import SonarrWebhookHandler
from api.radarr.client import RadarrWebhookHandler

class HandleWebHook:
# Initialize the webhook receiver
Expand Down
2 changes: 1 addition & 1 deletion utils/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

def get_times():
now = datetime.now()
one_hour_ago = now - timedelta(hours=1200)
one_hour_ago = now - timedelta(hours=1)
return now, one_hour_ago
Loading