replace jellyfin utils with class method
This commit is contained in:
@@ -7,3 +7,4 @@
|
|||||||
- Removed caching of last fetched items to ensure the most up-to-date information is always displayed in Discord Rich Presence
|
- Removed caching of last fetched items to ensure the most up-to-date information is always displayed in Discord Rich Presence
|
||||||
- Handle paused state correctly by checking the `PlayState` property from Jellyfin and updating Discord Rich Presence accordingly
|
- Handle paused state correctly by checking the `PlayState` property from Jellyfin and updating Discord Rich Presence accordingly
|
||||||
- Set the `auth.ssl` configuration option in Jellyfin Client dynamically based on the `jellyfin_server_url`
|
- Set the `auth.ssl` configuration option in Jellyfin Client dynamically based on the `jellyfin_server_url`
|
||||||
|
- Removed `to_rpc_payload` function in favor of a class method within the `JellyfinMediaItem` class for better encapsulation and organization of code
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ class JellyfinApiClient:
|
|||||||
self.logger.info("Connecting to Jellyfin server...")
|
self.logger.info("Connecting to Jellyfin server...")
|
||||||
self.client = JellyfinClient()
|
self.client = JellyfinClient()
|
||||||
self.client.config.app('jellydisc', '0.1.1', machine_name, unique_id)
|
self.client.config.app('jellydisc', '0.1.1', machine_name, unique_id)
|
||||||
self.client.config.data['auth.ssl'] = settings.jellyfin_server_url.startswith('https://')
|
self.client.config.data['auth.ssl'] = settings.jellyfin_server_url.startswith(
|
||||||
|
'https://')
|
||||||
|
|
||||||
self.last_auth_time = None
|
self.last_auth_time = None
|
||||||
self.authenticate()
|
self.authenticate()
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
|
from discord.models import DiscordRPCUpdatePayload
|
||||||
|
from pypresence.types import ActivityType
|
||||||
|
|
||||||
|
|
||||||
class JellyfinMediaType(str, Enum):
|
class JellyfinMediaType(str, Enum):
|
||||||
@@ -32,3 +34,45 @@ class JellyfinMediaItem(BaseModel):
|
|||||||
metadata: Union[JellyfinMusicMediaMetadata,
|
metadata: Union[JellyfinMusicMediaMetadata,
|
||||||
JellyfinMovieMediaMetadata,
|
JellyfinMovieMediaMetadata,
|
||||||
JellyfinEpisodeMediaMetadata]
|
JellyfinEpisodeMediaMetadata]
|
||||||
|
|
||||||
|
def to_rpc_payload(self) -> DiscordRPCUpdatePayload:
|
||||||
|
"""
|
||||||
|
Converts a JellyfinMediaItem to a DiscordRPCUpdatePayload.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
DiscordRPCUpdatePayload: The converted Discord RPC update payload.
|
||||||
|
"""
|
||||||
|
if self.type == JellyfinMediaType.AUDIO:
|
||||||
|
return DiscordRPCUpdatePayload(
|
||||||
|
id=self.id,
|
||||||
|
title=f"Listening to {
|
||||||
|
self.name}",
|
||||||
|
subtitle=f"by {
|
||||||
|
self.metadata.artist}",
|
||||||
|
image_url=self.image_url,
|
||||||
|
details=self.metadata.album,
|
||||||
|
activity_type=ActivityType.LISTENING,
|
||||||
|
start=self.start,
|
||||||
|
end=self.end)
|
||||||
|
elif self.type == JellyfinMediaType.MOVIE:
|
||||||
|
return DiscordRPCUpdatePayload(
|
||||||
|
id=self.id,
|
||||||
|
title=f"Watching {self.name}",
|
||||||
|
subtitle=self.metadata.date,
|
||||||
|
image_url=self.image_url,
|
||||||
|
details=self.name,
|
||||||
|
activity_type=ActivityType.WATCHING,
|
||||||
|
start=self.start,
|
||||||
|
end=self.end
|
||||||
|
)
|
||||||
|
elif self.type == JellyfinMediaType.EPISODE:
|
||||||
|
return DiscordRPCUpdatePayload(
|
||||||
|
id=self.id,
|
||||||
|
title=f"Watching {self.name}",
|
||||||
|
subtitle=self.metadata.subtitle,
|
||||||
|
image_url=self.image_url,
|
||||||
|
details=self.name,
|
||||||
|
activity_type=ActivityType.WATCHING,
|
||||||
|
start=self.start,
|
||||||
|
end=self.end
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
from discord.models import DiscordRPCUpdatePayload
|
|
||||||
from jellyfin.models import JellyfinMediaItem, JellyfinMediaType
|
|
||||||
from pypresence.types import ActivityType
|
|
||||||
|
|
||||||
|
|
||||||
def to_rpc_payload(media_item: JellyfinMediaItem) -> DiscordRPCUpdatePayload:
|
|
||||||
"""
|
|
||||||
Converts a JellyfinMediaItem to a DiscordRPCUpdatePayload.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
media_item (JellyfinMediaItem): The Jellyfin media item to convert.
|
|
||||||
Returns:
|
|
||||||
DiscordRPCUpdatePayload: The converted Discord RPC update payload.
|
|
||||||
"""
|
|
||||||
if media_item.type == JellyfinMediaType.AUDIO:
|
|
||||||
return DiscordRPCUpdatePayload(
|
|
||||||
id=media_item.id,
|
|
||||||
title=f"Listening to {
|
|
||||||
media_item.name}",
|
|
||||||
subtitle=f"by {
|
|
||||||
media_item.metadata.artist}",
|
|
||||||
image_url=media_item.image_url,
|
|
||||||
details=media_item.metadata.album,
|
|
||||||
activity_type=ActivityType.LISTENING,
|
|
||||||
start=media_item.start,
|
|
||||||
end=media_item.end)
|
|
||||||
elif media_item.type == JellyfinMediaType.MOVIE:
|
|
||||||
return DiscordRPCUpdatePayload(
|
|
||||||
id=media_item.id,
|
|
||||||
title=f"Watching {media_item.name}",
|
|
||||||
subtitle=media_item.metadata.date,
|
|
||||||
image_url=media_item.image_url,
|
|
||||||
details=media_item.name,
|
|
||||||
activity_type=ActivityType.WATCHING,
|
|
||||||
start=media_item.start,
|
|
||||||
end=media_item.end
|
|
||||||
)
|
|
||||||
elif media_item.type == JellyfinMediaType.EPISODE:
|
|
||||||
return DiscordRPCUpdatePayload(
|
|
||||||
id=media_item.id,
|
|
||||||
title=f"Watching {media_item.name}",
|
|
||||||
subtitle=media_item.metadata.subtitle,
|
|
||||||
image_url=media_item.image_url,
|
|
||||||
details=media_item.name,
|
|
||||||
activity_type=ActivityType.WATCHING,
|
|
||||||
start=media_item.start,
|
|
||||||
end=media_item.end
|
|
||||||
)
|
|
||||||
3
main.py
3
main.py
@@ -1,6 +1,5 @@
|
|||||||
from discord.rpc import DiscordRPC
|
from discord.rpc import DiscordRPC
|
||||||
from jellyfin.api_client import JellyfinApiClient
|
from jellyfin.api_client import JellyfinApiClient
|
||||||
from jellyfin.utils import to_rpc_payload
|
|
||||||
from settings import settings
|
from settings import settings
|
||||||
import coloredlogs
|
import coloredlogs
|
||||||
import logging
|
import logging
|
||||||
@@ -23,7 +22,7 @@ def main():
|
|||||||
time.sleep(settings.poll_interval)
|
time.sleep(settings.poll_interval)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
discordRPC.update(to_rpc_payload(media_item))
|
discordRPC.update(media_item.to_rpc_payload())
|
||||||
time.sleep(settings.poll_interval)
|
time.sleep(settings.poll_interval)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logging.info("Shutting down...")
|
logging.info("Shutting down...")
|
||||||
|
|||||||
Reference in New Issue
Block a user