From aa6088ad23243bf178a6a28498c4f01755b6ea5b Mon Sep 17 00:00:00 2001 From: Phin <59180111+phin05@users.noreply.github.com> Date: Sat, 10 Feb 2024 12:42:58 +0530 Subject: [PATCH] Downscale poster images before upload --- .github/release-notes/v2.5.0.md | 8 ++++++++ README.md | 1 + config/constants.py | 2 +- core/config.py | 1 + core/imgur.py | 4 +++- core/plex.py | 2 +- models/config.py | 1 + 7 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 .github/release-notes/v2.5.0.md diff --git a/.github/release-notes/v2.5.0.md b/.github/release-notes/v2.5.0.md new file mode 100644 index 0000000..9c769ba --- /dev/null +++ b/.github/release-notes/v2.5.0.md @@ -0,0 +1,8 @@ +### Release Notes + +* Poster images will now be downscaled to a maximum size of 256x256 before being uploaded (#78) + +### Installation Instructions + +* [Regular](https://github.com/phin05/discord-rich-presence-plex/blob/v2.5.0/README.md#installation) +* [Docker](https://github.com/phin05/discord-rich-presence-plex/blob/v2.5.0/README.md#run-with-docker) diff --git a/README.md b/README.md index 1e7e993..bd666fb 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ The config file is stored in a directory named `data`. * `posters` * `enabled` (boolean, default: `false`) - Displays media posters if enabled. Requires `imgurClientID`. * `imgurClientID` (string, default: `""`) - [Obtention Instructions](#obtaining-an-imgur-client-id) + * `maxSize` (int, default: `256`) * `buttons` (list) - [Information](#buttons) * `label` (string) - The label to be displayed on the button. * `url` (string) - A web address or a [dynamic URL placeholder](#dynamic-button-urls). diff --git a/config/constants.py b/config/constants.py index ed61571..8fe061a 100644 --- a/config/constants.py +++ b/config/constants.py @@ -2,7 +2,7 @@ import os import sys name = "Discord Rich Presence for Plex" -version = "2.4.5" +version = "2.5.0" plexClientID = "discord-rich-presence-plex" discordClientID = "413407336082833418" diff --git a/core/config.py b/core/config.py index edc13dc..c83e486 100644 --- a/core/config.py +++ b/core/config.py @@ -18,6 +18,7 @@ config: models.config.Config = { "posters": { "enabled": False, "imgurClientID": "", + "maxSize": 256, }, "buttons": [], }, diff --git a/core/imgur.py b/core/imgur.py index 07eece3..e281541 100644 --- a/core/imgur.py +++ b/core/imgur.py @@ -6,12 +6,14 @@ import io import models.imgur import requests -def uploadToImgur(url: str) -> Optional[str]: +def uploadToImgur(url: str, maxSize: int = 0) -> Optional[str]: try: originalImageBytesIO = io.BytesIO(requests.get(url).content) originalImage = Image.open(originalImageBytesIO) newImage = Image.new("RGB", originalImage.size) newImage.putdata(originalImage.getdata()) # pyright: ignore[reportUnknownMemberType,reportUnknownArgumentType] + if maxSize: + newImage.thumbnail((maxSize, maxSize)) newImageBytesIO = io.BytesIO() newImage.save(newImageBytesIO, subsampling = 0, quality = 90, format = "JPEG") data: models.imgur.UploadResponse = requests.post( diff --git a/core/plex.py b/core/plex.py index 72dee90..9316bb8 100644 --- a/core/plex.py +++ b/core/plex.py @@ -229,7 +229,7 @@ class PlexAlertListener(threading.Thread): thumbUrl = getCacheKey(thumb) if not thumbUrl: self.logger.debug("Uploading image to Imgur") - thumbUrl = uploadToImgur(self.server.url(thumb, True)) + thumbUrl = uploadToImgur(self.server.url(thumb, True), config["display"]["posters"]["maxSize"]) setCacheKey(thumb, thumbUrl) activity: models.discord.Activity = { "details": title[:128], diff --git a/models/config.py b/models/config.py index 3a86cc9..2a4ab30 100644 --- a/models/config.py +++ b/models/config.py @@ -7,6 +7,7 @@ class Logging(TypedDict): class Posters(TypedDict): enabled: bool imgurClientID: str + maxSize: int class Button(TypedDict): label: str