forked from ghomasHudson/Jellyfin-Auto-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
73 lines (61 loc) · 2.32 KB
/
utils.py
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
72
73
'''Handy utility functions for other scripts'''
import requests
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
server_url = config["main"]["server_url"]
user_id = config["main"]["user_id"]
def get_all_collections(headers=None):
'''Find list of all collections'''
params = {
"enableTotalRecordCount": "false",
"enableImages": "false",
"Recursive": "true",
"includeItemTypes": "BoxSet"
}
print("Getting collections list...")
res = requests.get(f'{server_url}/Users/{user_id}/Items',headers=headers, params=params)
collections = {r["Name"]:r["Id"] for r in res.json()["Items"]}
return collections
def find_collection_with_name_or_create(list_name, collections, headers=None):
'''Find a collection with name `list_name` or create one if
it doesn't exist'''
# Try and find the collection with name `list_name`
collection_id = None
for collection in collections:
if list_name == collection:
print("found", list_name, collections[collection])
collection_id = collections[collection]
break
if collection_id is None:
# Collection doesn't exist -> Make a new one
print(f"Creating {list_name}...")
res2 = request_repeat_post(f'{server_url}/Collections',headers=headers, params={"name": list_name})
collection_id = res2.json()["Id"]
return collection_id
def get_library_id(library_name, headers=None):
'''Get the library named library_name'''
r = requests.get(f'{server_url}/Users/{user_id}/Views',headers=headers)
r.raise_for_status()
libraries = r.json().get('Items')
library_id = [ x.get('Id') for x in libraries
if x.get('Name') == library_name]
if library_id:
library_id = library_id[0]
return library_id
def request_repeat_get(url, headers=None, params=None):
'''Do a GET request, repeat if err'''
while True:
try:
res = requests.get(url, headers=headers, params=params)
return res
except:
pass
def request_repeat_post(url, headers=None, params=None):
'''Do a POST request, repeat if err'''
while True:
try:
res = requests.post(url, headers=headers, params=params)
return res
except:
pass