Skip to content

Commit

Permalink
Update subgen.py
Browse files Browse the repository at this point in the history
Added metadata refresh for Jellyfin
  • Loading branch information
McCloudS authored Dec 19, 2023
1 parent b58376b commit 2ba2357
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions subgen/subgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ def receive_jellyfin_webhook(
logging.debug(f"Path of file: {fullpath}")

gen_subtitles(path_mapping(fullpath), transcribe_or_translate, True)
try:
refresh_jellyfin_metadata(ItemId, jellyfinserver, jellyfintoken)
logging.info(f"Metadata for item {ItemId} refreshed successfully.")
except Exception as e:
logging.error(f"Failed to refresh metadata for item {ItemId}: {e}")
else:
print("This doesn't appear to be a properly configured Jellyfin webhook, please review the instructions again!")

Expand Down Expand Up @@ -390,6 +395,42 @@ def refresh_plex_metadata(itemid: str, server_ip: str, plex_token: str) -> None:
else:
raise Exception(f"Error refreshing metadata: {response.status_code}")

def refresh_jellyfin_metadata(itemid: str, server_ip: str, jellyfin_token: str) -> None:
"""
Refreshes the metadata of a Jellyfin library item.
Args:
itemid: The ID of the item in the Jellyfin library whose metadata needs to be refreshed.
server_ip: The IP address of the Jellyfin server.
jellyfin_token: The Jellyfin token used for authentication.
Raises:
Exception: If the server does not respond with a successful status code.
"""

# Jellyfin API endpoint to refresh metadata for a specific item
url = f"{server_ip}/library/metadata/{itemid}/refresh"

# Headers to include the Jellyfin token for authentication
headers = {
"Authorization": f"MediaBrowser Token={jellyfin_token}",
}

# Cheap way to get the admin user id, and save it for later use.
users = json.loads(requests.get(f"{server_ip}/Users", headers=headers).content)
jellyfin_admin = get_jellyfin_admin(users)

response = requests.get(f"{server_ip}/Users/{jellyfin_admin}/Items/{item_id}/Refresh", headers=headers)

# Sending the PUT request to refresh metadata
response = requests.post(url, headers=headers)

# Check if the request was successful
if response.status_code == 204:
print("Metadata refresh queued 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.
Expand Down

0 comments on commit 2ba2357

Please sign in to comment.