Skip to content

Commit

Permalink
Added jellyfin query plugin #52
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas committed Oct 6, 2024
1 parent 22eb009 commit 99bfc14
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
14 changes: 13 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,31 @@ def main(config):
loader = pluginlib.PluginLoader(modules=['plugins'])
plugins = loader.plugins['list_scraper']

# If Jellyfin_api plugin is enabled - pass the jellyfin creds to it
if "jellyfin_api" in config["plugins"] and config["plugins"]["jellyfin_api"].get("enabled", False):
config["plugins"]["jellyfin_api"]["server_url"] = config["jellyfin"]["server_url"]
config["plugins"]["jellyfin_api"]["user_id"] = config["jellyfin"]["user_id"]
config["plugins"]["jellyfin_api"]["api_key"] = config["jellyfin"]["api_key"]

# Update jellyfin with lists
for plugin_name in config['plugins']:
if config['plugins'][plugin_name]["enabled"] and plugin_name in plugins:
for list_id in config['plugins'][plugin_name]["list_ids"]:
logger.info(f"")
logger.info(f"")
logger.info("Getting list info for plugin: " + plugin_name + ", list id: " + list_id)
logger.info(f"Getting list info for plugin: {plugin_name}, list id: {list_id}")

# Match list items to jellyfin items
list_info = plugins[plugin_name].get_list(list_id, config['plugins'][plugin_name])

# Find jellyfin collection or create it
collection_id = jf_client.find_collection_with_name_or_create(list_info['name'], list_id, list_info.get("description", None), plugin_name)

if config["plugins"][plugin_name].get("clear_collection", False):
# Optionally clear everything from the collection first
jf_client.clear_collection(collection_id)

# Add items to the collection
for item in list_info['items']:
jf_client.add_item_to_collection(collection_id, item, year_filter=config["plugins"][plugin_name].get("year_filter", True))

Expand Down
38 changes: 38 additions & 0 deletions plugins/jellyfin_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import bs4
import requests
import json
from utils.base_plugin import ListScraper

class JellyfinAPI(ListScraper):
'''Generate collections based on Jellyfin API queries'''

_alias_ = 'jellyfin_api'

def get_list(list_id, config=None):
'''Call jellyfin API
list_id should be a dict to pass to https://api.jellyfin.org/#tag/Items/operation/GetItems
'''
params = {
"enableTotalRecordCount": "false",
"enableImages": "false",
"Recursive": "true",
"fields": ["ProviderIds", "ProductionYear"]
}
params = {**params, **list_id}

res = requests.get(f'{config["server_url"]}/Users/{config["user_id"]}/Items',headers={"X-Emby-Token": config["api_key"]}, params=params)

items = []
for item in res.json()["Items"]:
items.append({
"title": item["Name"],
"release_year": item.get("ProductionYear", None),
"media_type": item["Type"],
"imdb_id": item["ProviderIds"].get("Imdb", None)
})

return {
"name": f"{list_id}",
"description": "Movies which match the jellyfin API query: {list_id}",
"items": items
}
4 changes: 2 additions & 2 deletions utils/jellyfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def find_collection_with_name_or_create(self, list_name: str, list_id: str, desc

# Check if list name in tags
for collection in collections:
if list_id in collection["Tags"]:
if json.dumps(list_id) in collection["Tags"]:
collection_id = collection["Id"]
break

Expand All @@ -84,7 +84,7 @@ def find_collection_with_name_or_create(self, list_name: str, list_id: str, desc
collection = requests.get(f'{self.server_url}/Users/{self.user_id}/Items/{collection_id}', headers={"X-Emby-Token": self.api_key}).json()
if collection.get("Overview", "") == "" and description is not None:
collection["Overview"] = description
collection["Tags"] = list(set(collection.get("Tags", []) + ["Jellyfin-Auto-Collections", plugin_name, list_id]))
collection["Tags"] = list(set(collection.get("Tags", []) + ["Jellyfin-Auto-Collections", plugin_name, json.dumps(list_id)]))
r = requests.post(f'{self.server_url}/Items/{collection_id}',headers={"X-Emby-Token": self.api_key}, json=collection)

return collection_id
Expand Down

0 comments on commit 99bfc14

Please sign in to comment.