aboutsummaryrefslogtreecommitdiff
path: root/jellystack_lib.py
blob: 194cfe2b59ef0b7bd5584fc643081c6ca908bb52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# File: jellystack_lib.py
# Location: /mnt/public/Support/Programs/jellyfin/scripts/
# Author: bgstack15
# Startdate: 2024-01-20-7 13:59
# SPDX-License-Identifier: GPL-3.0-only
# Title: jellyfin stackrpms library
# Project: jellystack
# Purpose: Useful functions that use python3 jellyfin apiclient 
# History:
#    2024-01 started for listing/rescanning libraries
# Usage:
#    in jellystack.py
#    client.auth.login("vm4:8096","admin","0EXAMPLE0f93EXAMPLEXAMPLE534e0f6")
# Reference:
#    from firefox devtools:
#    curl 'https://albion320.no-ip.biz:500/Items/14895ee3d991844aee62d94dd46dfb7a/Refresh?Recursive=true&ImageRefreshMode=Default&MetadataRefreshMode=Default&ReplaceAllImages=false&ReplaceAllMetadata=false' -X POST -H 'X-Emby-Authorization: MediaBrowser Client="Jellyfin Web", Device="Firefox", DeviceId="TW96aWxsYS81LjAgKFgxMTsgTGlEXAMPLEg2XzY0OyBydjo5MS4wKSBHZWNEXAMPLEEwMDEwMSBGaXJlZm94LzkxLjB8MTYzODk3NjExMzkwMA11", Version="10.8.10", Token="0f6f671c4eEXAMPLEc219aEXAMPLE7b3" -H 'Content-Length: 0'
# Improve:
# Dependencies:
#    req-devuan: jellyfin-apiclient-python

from jellyfin_apiclient_python import JellyfinClient
import json, os

def get_authenticated_client(url = "http://vm4:8096", username = "admin", password = "None"):
   client = JellyfinClient()
   # this one works basically, but somehow my previous attempt did not.
   client.config.app('cli','0.0.1','any-ssh-node','a56decad32c0aefd696a7d3565ac1d0')
   client.config.data["auth.ssl"] = False
   client.auth.connect_to_address(url)
   client.auth.login(url,username,password)
   credentials = client.auth.credentials.get_credentials()
   server = credentials["Servers"][0]
   server["username"] = 'username'
   json.dumps(server)
   #json.loads(credentials)
   #client.authenticate({"Servers": [credentials]}, discover=False)
   return client

def get_media_folders(client):
   results = client.jellyfin.get_media_folders()["Items"]
   libraries = [{"Name":i["Name"],"Id":i["Id"]} for i in results]
   return libraries

def libraries_to_csv(libraries):
   for i in libraries:
      print(f"\"{i['Name']}\" {i['Id']}")

def get_library_names_only(libraries):
   return [i["Name"] for i in libraries]

def refresh_library(library, libraries, client):
   # find id of given library
   _use_id = ""
   for i in libraries:
      if i["Id"] == library:
         _use_id = library
         break
      elif i["Name"] == library:
         _use_id = i["Id"]
         break
   # if not found, fail out
   if _use_id == "":
      raise f"Unable to find which library, given {library}"
   print(f"Will try to refresh library {_use_id}")
   client.http.request(
      {
         "type": "POST",
         "url": f"{client.config.data['auth.server']}/Items/{_use_id}/Refresh?Recursive=true&ImageRefreshMode=Default&MetadataRefreshMode=Default&ReplaceAllImages=false&ReplaceAllMetadata=false"
      }
   )
bgstack15