Created
April 4, 2026 14:11
-
-
Save stpettersens/36c04218d6a42459d28b72c72e21bdbb to your computer and use it in GitHub Desktop.
Script to refresh a Jellyfin Server media library given a server domain and an API key.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # refresh-jellyfin media script | |
| import os | |
| import sys | |
| import requests | |
| from datetime import datetime | |
| from typing import TypeAlias | |
| from dataclasses import dataclass | |
| Response: TypeAlias = requests.models.Response | |
| # Config file should be at /etc/jellyfin.conf and contain: | |
| # <SERVER_DOMAIN> | |
| # <API_KEY> | |
| @dataclass | |
| class JellyfinConfig: | |
| server_domain: str | |
| api_key: str | |
| def load_config(config_file: str) -> JellyfinConfig|None: | |
| if (os.path.exists(config_file)): | |
| with open(config_file) as f: | |
| lines = f.readlines() | |
| return JellyfinConfig(lines[0].strip(), lines[1].strip()) | |
| return None | |
| def refresh_jellyfin_media(config_file: str) -> int: | |
| config: JellyfinConfig|None = load_config(config_file) | |
| if config is None: | |
| print("ERROR: Please provide configuration at:") | |
| print(config_file) | |
| return -1 | |
| url: str = f"https://{config.server_domain}/library/refresh" | |
| headers: dict[str, str] = { | |
| "User-Agent": "refresh-jellyfin/0.1", | |
| "X-MediaBrowser-Token": config.api_key, | |
| } | |
| response: Response = requests.post(url, headers=headers) | |
| status: int = response.status_code | |
| print(datetime.now().isoformat()) | |
| print("Invoked jellyfin media refresh for:") | |
| print(config.server_domain) | |
| print(f"HTTP {status}") | |
| if status != 200: | |
| return -1 | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(refresh_jellyfin_media("/etc/jellyfin.conf")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment