|
| 1 | +import requests |
| 2 | +import urllib.parse |
| 3 | +from loguru import logger |
| 4 | + |
| 5 | +class JellyseerrClient: |
| 6 | + def __init__(self, server_url: str, api_key: str): |
| 7 | + # Fix common url issues |
| 8 | + if server_url.endswith("/"): |
| 9 | + server_url = server_url[:-1] # Remove trailing slash |
| 10 | + if not server_url.endswith("/api/v1"): |
| 11 | + server_url += "/api/v1" |
| 12 | + self.server_url = server_url |
| 13 | + self.api_key = api_key |
| 14 | + |
| 15 | + # Check if server is reachable |
| 16 | + try: |
| 17 | + r = requests.get(self.server_url + "/status") |
| 18 | + if r.status_code != 200: |
| 19 | + raise Exception("Jellyseerr Server is not reachable") |
| 20 | + except requests.exceptions.ConnectionError: |
| 21 | + raise Exception("Jellyseerr Server is not reachable") |
| 22 | + |
| 23 | + # Check if api key is valid |
| 24 | + r = requests.get(f"{self.server_url}/auth/me") |
| 25 | + |
| 26 | + |
| 27 | + def make_request(self, item): |
| 28 | + '''Request item from jellyseerr''' |
| 29 | + |
| 30 | + # Search for item |
| 31 | + r = requests.get(f"{self.server_url}/search", params={ |
| 32 | + "query": urllib.parse.quote_plus(item["title"]) |
| 33 | + }, headers={ |
| 34 | + "X-Api-Key": self.api_key |
| 35 | + }) |
| 36 | + |
| 37 | + # Find matching item |
| 38 | + mediaId = None |
| 39 | + for result in r.json()["results"]: |
| 40 | + # Try IMDB match first |
| 41 | + if "mediaInfo" in result and "ImdbId" in result["mediaInfo"]: |
| 42 | + imdb_id = result["mediaInfo"]["ImdbId"] |
| 43 | + if imdb_id == item["imdb_id"]: |
| 44 | + mediaId = result["id"] |
| 45 | + logger.debug(f"Found exact IMDB match for {item['title']}") |
| 46 | + break |
| 47 | + elif "releaseDate" in result: |
| 48 | + # Try year match |
| 49 | + release_year = result["releaseDate"].split("-")[0] |
| 50 | + if release_year == str(item["release_year"]): |
| 51 | + mediaId = result["id"] |
| 52 | + logger.debug(f"Found year match for {item['title']}") |
| 53 | + break |
| 54 | + |
| 55 | + # Request item if not found |
| 56 | + if mediaId is not None: |
| 57 | + if "mediaInfo" not in result or result["mediaInfo"]["jellyfinMediaId"] is None: |
| 58 | + # If it's not already in Jellyfin |
| 59 | + # Request item |
| 60 | + r = requests.post(f"{self.server_url}/request", json={ |
| 61 | + "mediaType": result["mediaType"], |
| 62 | + "mediaId": mediaId, |
| 63 | + }, headers={ |
| 64 | + "X-Api-Key": self.api_key |
| 65 | + }) |
| 66 | + logger.info(f"Requested {item['title']} from Jellyseerr") |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + from pyaml_env import parse_config |
| 72 | + config = parse_config("/home/thomas/Documents/Jellyfin-Auto-Collections/config.yaml", default_value=None) |
| 73 | + |
| 74 | + client = JellyseerrClient( |
| 75 | + server_url=config["jellyseerr"]["server_url"], |
| 76 | + api_key=config["jellyseerr"]["api_key"] |
| 77 | + ) |
| 78 | + client.make_request({ |
| 79 | + "title": "The Matrix", |
| 80 | + "imdb_id": "tt0133093", |
| 81 | + "release_year": 1999 |
| 82 | + }) |
0 commit comments