From 10ffbbaf29a7aa80df571675cd35b3b15ace4613 Mon Sep 17 00:00:00 2001 From: Rikiar Date: Mon, 18 Dec 2023 16:20:51 +0100 Subject: [PATCH 1/6] Fix container startup failure due to CMD path error --- Dockerfile | 2 +- Dockerfile.cuda | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) mode change 100644 => 100755 Dockerfile diff --git a/Dockerfile b/Dockerfile old mode 100644 new mode 100755 index 9896245..662c543 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,6 @@ ENV PYTHONUNBUFFERED 1 ADD https://raw.githubusercontent.com/McCloudS/subgen/main/subgen/subgen.py /subgen/subgen.py -CMD [ "python3", "-u", "./subgen.py" ] +CMD [ "python3", "-u", "subgen/subgen.py" ] EXPOSE 8090 diff --git a/Dockerfile.cuda b/Dockerfile.cuda index 3d3b26a..db7d4e1 100644 --- a/Dockerfile.cuda +++ b/Dockerfile.cuda @@ -8,6 +8,6 @@ ENV PYTHONUNBUFFERED 1 ADD https://raw.githubusercontent.com/McCloudS/subgen/main/subgen/subgen.py /subgen/subgen.py -CMD [ "python3", "-u", "./subgen.py" ] +CMD [ "python3", "-u", "subgen/subgen.py" ] EXPOSE 8090 From 113e0a203c41c2603626a63447b278cfbc492442 Mon Sep 17 00:00:00 2001 From: Rikiar Date: Mon, 18 Dec 2023 16:51:45 +0100 Subject: [PATCH 2/6] Enhance post-webhook metadata handling Added metadata refresh capability immediately after subtitle generation when handling Plex webhooks. If subtitle generation is successful, the Plex item's metadata is attempted to be refreshed to ensure that the updates are reflected. The change includes robust error handling to log failures in refreshing metadata and alerts the user if the webhook is misconfigured. This enhancement improves the overall user experience by ensuring the media library is up-to-date after any subtitle changes. --- subgen/subgen.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/subgen/subgen.py b/subgen/subgen.py index e1937b4..087d7d5 100644 --- a/subgen/subgen.py +++ b/subgen/subgen.py @@ -128,8 +128,13 @@ def receive_plex_webhook( logging.debug("Path of file: " + fullpath) gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True) - else: - print("This doesn't appear to be a properly configured Plex webhook, please review the instructions again!") + try: + refresh_plex_metadata(item_id, plexserver, plextoken) + logging.info(f"Metadata for item {item_id} refreshed successfully.") + except Exception as e: + logging.error(f"Failed to refresh metadata for item {item_id}: {e}") + else: + print("This doesn't appear to be a properly configured Plex webhook, please review the instructions again!") return "" @@ -355,6 +360,37 @@ def get_plex_file_name(itemid: str, server_ip: str, plex_token: str) -> str: else: raise Exception(f"Error: {response.status_code}") +def refresh_plex_metadata(itemid: str, server_ip: str, plex_token: str) -> None: + """ + Refreshes the metadata of a Plex library item. + + Args: + itemid: The ID of the item in the Plex library whose metadata needs to be refreshed. + server_ip: The IP address of the Plex server. + plex_token: The Plex token used for authentication. + + Raises: + Exception: If the server does not respond with a successful status code. + """ + + # Plex API endpoint to refresh metadata for a specific item + url = f"http://{server_ip}:32400/library/metadata/{itemid}/refresh" + + # Headers to include the Plex token for authentication + headers = { + "X-Plex-Token": plex_token, + } + + # Sending the PUT request to refresh metadata + response = requests.put(url, headers=headers) + + # Check if the request was successful + if response.status_code == 200: + print("Metadata refresh initiated successfully.") + else: + raise Exception(f"Error refreshing metadata: {response.status_code}") + + def get_jellyfin_file_name(item_id: str, jellyfin_url: str, jellyfin_token: str) -> str: """Gets the full path to a file from the Jellyfin server. From f41ef2d74017f596a5a312380f3082c9f0fcfc22 Mon Sep 17 00:00:00 2001 From: Rikiar Date: Mon, 18 Dec 2023 23:28:15 +0100 Subject: [PATCH 3/6] Refine Plex metadata key usage for refresh --- subgen/subgen.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/subgen/subgen.py b/subgen/subgen.py index 087d7d5..82d0dc0 100644 --- a/subgen/subgen.py +++ b/subgen/subgen.py @@ -129,10 +129,10 @@ def receive_plex_webhook( gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True) try: - refresh_plex_metadata(item_id, plexserver, plextoken) - logging.info(f"Metadata for item {item_id} refreshed successfully.") + refresh_plex_metadata(plex_json['Metadata']['ratingKey'], plexserver, plextoken) + logging.info(f"Metadata for item {plex_json['Metadata']['ratingKey']} refreshed successfully.") except Exception as e: - logging.error(f"Failed to refresh metadata for item {item_id}: {e}") + logging.error(f"Failed to refresh metadata for item {plex_json['Metadata']['ratingKey']}: {e}") else: print("This doesn't appear to be a properly configured Plex webhook, please review the instructions again!") From e514fc16be683f59d0fd328e6c350a04ae6632fe Mon Sep 17 00:00:00 2001 From: Rikiar Date: Tue, 19 Dec 2023 00:03:58 +0100 Subject: [PATCH 4/6] Reverting entrypoint path in Dockerfiles, was a me problem --- Dockerfile | 2 +- Dockerfile.cuda | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 662c543..9896245 100755 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,6 @@ ENV PYTHONUNBUFFERED 1 ADD https://raw.githubusercontent.com/McCloudS/subgen/main/subgen/subgen.py /subgen/subgen.py -CMD [ "python3", "-u", "subgen/subgen.py" ] +CMD [ "python3", "-u", "./subgen.py" ] EXPOSE 8090 diff --git a/Dockerfile.cuda b/Dockerfile.cuda index db7d4e1..3d3b26a 100644 --- a/Dockerfile.cuda +++ b/Dockerfile.cuda @@ -8,6 +8,6 @@ ENV PYTHONUNBUFFERED 1 ADD https://raw.githubusercontent.com/McCloudS/subgen/main/subgen/subgen.py /subgen/subgen.py -CMD [ "python3", "-u", "subgen/subgen.py" ] +CMD [ "python3", "-u", "./subgen.py" ] EXPOSE 8090 From 5c5fe8d11405c6b9d378dc78b6e3e1ffb8e1604d Mon Sep 17 00:00:00 2001 From: Rikiar Date: Tue, 19 Dec 2023 11:55:54 +0100 Subject: [PATCH 5/6] Improve webhook error handling and URL building Adjusted indentation in the webhook receiver to correctly fall back to a warning message when receiving a misconfigured webhook. Also, updated metadata refresh function to construct the request URL dynamically, allowing for more robust handling of server addresses, including those served over HTTPS. These changes ensure better user feedback and improve compatibility with secure Plex server configurations. --- .gitignore | 5 +++++ subgen/subgen.py | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f40bb1c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +subgen/tmp27tjle0a +subgen/.locks/* +subgen/__pycache__/subgen.cpython-310.pyc +subgen/models--Systran--faster-whisper-medium/* + diff --git a/subgen/subgen.py b/subgen/subgen.py index 82d0dc0..1e7469c 100644 --- a/subgen/subgen.py +++ b/subgen/subgen.py @@ -133,8 +133,8 @@ def receive_plex_webhook( logging.info(f"Metadata for item {plex_json['Metadata']['ratingKey']} refreshed successfully.") except Exception as e: logging.error(f"Failed to refresh metadata for item {plex_json['Metadata']['ratingKey']}: {e}") - else: - print("This doesn't appear to be a properly configured Plex webhook, please review the instructions again!") + else: + print("This doesn't appear to be a properly configured Plex webhook, please review the instructions again!") return "" @@ -374,7 +374,7 @@ def refresh_plex_metadata(itemid: str, server_ip: str, plex_token: str) -> None: """ # Plex API endpoint to refresh metadata for a specific item - url = f"http://{server_ip}:32400/library/metadata/{itemid}/refresh" + url = f"{server_ip}/library/metadata/{itemid}/refresh" # Headers to include the Plex token for authentication headers = { From 02c97f15358d190c66b952b41b99e12b2cb999ca Mon Sep 17 00:00:00 2001 From: Rikiar Date: Tue, 19 Dec 2023 12:26:46 +0100 Subject: [PATCH 6/6] Fix indentation for Plex webhook error message --- subgen/subgen.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/subgen/subgen.py b/subgen/subgen.py index 1e7469c..413b6f8 100644 --- a/subgen/subgen.py +++ b/subgen/subgen.py @@ -133,8 +133,8 @@ def receive_plex_webhook( logging.info(f"Metadata for item {plex_json['Metadata']['ratingKey']} refreshed successfully.") except Exception as e: logging.error(f"Failed to refresh metadata for item {plex_json['Metadata']['ratingKey']}: {e}") - else: - print("This doesn't appear to be a properly configured Plex webhook, please review the instructions again!") + else: + print("This doesn't appear to be a properly configured Plex webhook, please review the instructions again!") return ""