Skip to content

Commit

Permalink
Downscale poster images before upload
Browse files Browse the repository at this point in the history
  • Loading branch information
phin05 committed Feb 10, 2024
1 parent 69eef9e commit aa6088a
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .github/release-notes/v2.5.0.md
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys

name = "Discord Rich Presence for Plex"
version = "2.4.5"
version = "2.5.0"

plexClientID = "discord-rich-presence-plex"
discordClientID = "413407336082833418"
Expand Down
1 change: 1 addition & 0 deletions core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"posters": {
"enabled": False,
"imgurClientID": "",
"maxSize": 256,
},
"buttons": [],
},
Expand Down
4 changes: 3 additions & 1 deletion core/imgur.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
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(
Expand Down
2 changes: 1 addition & 1 deletion core/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def handleAlert(self, alert: models.plex.Alert) -> None:
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],
Expand Down
1 change: 1 addition & 0 deletions models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Logging(TypedDict):
class Posters(TypedDict):
enabled: bool
imgurClientID: str
maxSize: int

class Button(TypedDict):
label: str
Expand Down

0 comments on commit aa6088a

Please sign in to comment.