From d6ac5e5e9036c4637c0a04471acd977c28e0dc46 Mon Sep 17 00:00:00 2001 From: Phin <59180111+phin05@users.noreply.github.com> Date: Sun, 5 Nov 2023 11:43:52 +0530 Subject: [PATCH] Convert poster images to JPG before uploading them --- NOTICE | 36 ++++++++++++++++++++++++++++++++++++ core/imgur.py | 10 +++++++++- main.py | 7 +++++-- requirements.txt | 1 + 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/NOTICE b/NOTICE index 538a244..a001b51 100644 --- a/NOTICE +++ b/NOTICE @@ -463,3 +463,39 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +---------------------------------------------------------------------- +Pillow +https://github.com/python-pillow/Pillow +---------------------------------------------------------------------- + +The Python Imaging Library (PIL) is + + Copyright © 1997-2011 by Secret Labs AB + Copyright © 1995-2011 by Fredrik Lundh + +Pillow is the friendly PIL fork. It is + + Copyright © 2010-2023 by Jeffrey A. Clark (Alex) and contributors. + +Like PIL, Pillow is licensed under the open source HPND License: + +By obtaining, using, and/or copying this software and/or its associated +documentation, you agree that you have read, understood, and will comply +with the following terms and conditions: + +Permission to use, copy, modify and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appears in all copies, and that +both that copyright notice and this permission notice appear in supporting +documentation, and that the name of Secret Labs AB or the author not be +used in advertising or publicity pertaining to distribution of the software +without specific, written prior permission. + +SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS +SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. +IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR ANY SPECIAL, +INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. diff --git a/core/imgur.py b/core/imgur.py index 0df80dc..07eece3 100644 --- a/core/imgur.py +++ b/core/imgur.py @@ -1,15 +1,23 @@ from .config import config +from PIL import Image from typing import Optional from utils.logging import logger +import io import models.imgur import requests def uploadToImgur(url: str) -> 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] + newImageBytesIO = io.BytesIO() + newImage.save(newImageBytesIO, subsampling = 0, quality = 90, format = "JPEG") data: models.imgur.UploadResponse = requests.post( "https://api.imgur.com/3/image", headers = { "Authorization": f"Client-ID {config['display']['posters']['imgurClientID']}" }, - files = { "image": requests.get(url).content } + files = { "image": newImageBytesIO.getvalue() } ).json() if not data["success"]: raise Exception(data["data"]["error"]) diff --git a/main.py b/main.py index 2e51d8f..1cb9105 100644 --- a/main.py +++ b/main.py @@ -110,7 +110,10 @@ def testIpc() -> None: discordIpcService.disconnect() if __name__ == "__main__": - if len(sys.argv) > 1 and sys.argv[1] == "test-ipc": + mode = sys.argv[1] if len(sys.argv) > 1 else "" + if not mode: + main() + elif mode == "test-ipc": testIpc() else: - main() + print(f"Invalid mode: {mode}") diff --git a/requirements.txt b/requirements.txt index cc7d864..6e4d8ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ PlexAPI==4.10.1 requests==2.31.0 websocket-client==1.3.2 PyYAML==6.0.1 +Pillow==10.1.0