-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_funcs.py
305 lines (258 loc) · 9.24 KB
/
helper_funcs.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import urllib.parse
import os
import requests
import time
import xml.etree.ElementTree as ET
STATIC_FILES = {
'SERVER_SETTINGS':'plex_server_settings.xml',
'LIBRARIES':'plex_library_list.xml',
'LIB_CONTENT':'plex_lib_content.xml',
'COLLECTIONS':'plex_collections.xml',
'COLLECTION':'plex_coll_content.xml'
}
def get_epochtime():
return int(time.time())
#################
#################
## ##
## | | |\ | ##
## | | |/ | ##
## |__| |\ |__ ##
## ##
#################
#################
def url_encode(str_elem):
return urllib.parse.quote(str_elem)
def create_url(str_host,str_http_type,int_port):
if create_url.__code__.co_argcount != 3:
print("This function requires 3 arguments.")
exit(0)
try:
str(int_port)
except:
exit(0)
print("The port should be a number.")
str_url = str_http_type+"://"+str_host+":"+str(int_port)+"/"
return str_url
def create_url_args(lst_url_args):
if not lst_url_args:
return ""
url_args=""
for arg in lst_url_args:
if len(url_args) == 0:
url_args+="?"+arg[0]+"="+url_encode(arg[1])
else:
url_args+="&"+arg[0]+"="+url_encode(arg[1])
return url_args
def create_base_url_parts(lst_base_url_parts,end_slash):
if not lst_base_url_parts:
return ""
url_parts = ""
for url_part in lst_base_url_parts:
if len(url_parts) == 0:
url_parts += url_part
else:
url_parts += "/" + url_part
if end_slash:
return url_parts+"/"
return url_parts
# !!!!!!!!!!!!!!!!!!!!!! BASE URL ALWAYS ENDS WITH A FWD SLASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# !!!!!!!!!!!!!!!!!!!!!! FIRST LIST ELEM IS ALWAYS PARTS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# !!!!!!!!!!!!!!!!!!!!!! SECOND LIST ELEM IS ALWAYS ARGS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
def build_request_url_elems(lst_args,end_slash=None):
# lst_base_url_parts, lst_url_args
str_url_parts = create_base_url_parts(lst_args[0],end_slash)
str_url_args = create_url_args(lst_args[1])
url = str_url_parts + str_url_args
return url
######################
######################
## ##
## \ / +- |> ##
## \ /\ / +- |\ ##
## \/ \/ +- |/ ##
## ##
######################
######################
def get_request(str_req_url):
print(str_req_url)
req_resp = requests.get(str_req_url, verify=False)
return req_resp.content
def put_request(str_req_url):
print(str_req_url)
req_resp = requests.put(str_req_url,verify=False)
return req_resp.content
############################
############################
## ##
## \ / |\ | -+- +- ##
## \ /\ / |/ | | +- ##
## \/ \/ |\ | | +- ##
## ##
############################
############################
def get_write_dir():
write_dir = os.path.expanduser("~")
return write_dir
def write_xml(content_blob,filename):
write_dir = get_write_dir()
f_2_w = write_dir+"\\"+filename
with open(f_2_w, 'wb') as f:
f.write(content_blob)
####################
####################
## ##
## \ / |\ /| | ##
## x | \/ | | ##
## / \ | | |__ ##
## ##
####################
####################
# retruns tuple list of lib name and key
# expects xml file with libs
# TODO: maybe merge colls/lib key funcs
def parse_xml_get_root(fname):
f_content = get_write_dir()+"\\"+fname
plex_content = ET.parse(f_content)
xml_root = plex_content.getroot()
return xml_root
def getLibsFromXmL(f_plex_libs):
xml_root = parse_xml_get_root(f_plex_libs)
# key, title
lst_res = []
for item in xml_root.findall('./Directory'):
lst_res.append((item.attrib['key'], item.attrib['title']))
return lst_res
def getCollectionsFromXmL(f_plex_colls):
xml_root = parse_xml_get_root(f_plex_colls)
# key, title
lst_res = []
for item in xml_root.findall('./Directory'):
lst_res.append((item.attrib['ratingKey'],item.attrib['title']))
return lst_res
# TODO: account for absence of properties
def getCollectionContentFromXmL(f_plex_coll_content):
xml_root = parse_xml_get_root(f_plex_coll_content)
lst_res = []
for item in xml_root.findall('./Video'):
try:
lst_res.append((item.attrib['ratingKey'],item.attrib['title'],item.attrib['originallyAvailableAt']))
except KeyError:
print("woops, didn't find some items...")
pass
return lst_res
def getFilmSearchContentFromXml(f_mov_search_content):
xml_root = parse_xml_get_root(f_mov_search_content)
lst_res = []
for item in xml_root.findall("./Hub[@hubIdentifier='movie']/Video"):
try:
lst_res.append((item.attrib['ratingKey'],item.attrib['title'],item.attrib['originallyAvailableAt']))
except KeyError:
print("woops, didn't find some items...")
pass
return lst_res
# TODO: change elem pos here
# TODO: present user with potential unparsed film title((s) count)
# film_data = [(ratingkey,title,originallyavailableat),...]
def parse_titles(film_data):
# ratingkey-filmTitle
lst_res = []
for blob in film_data:
print(blob[1])
if "RARBG" in blob[1]:
lst_blob = blob[1].split('.')
for elem in lst_blob:
if elem.isdigit():
lst_title = lst_blob[:lst_blob.index(elem)]
title = ' '.join(lst_title)
lst_res.append([blob[0],title])
if "nickarad" in blob[1]:
title = blob[1].split(" (")[0]
lst_res.append([blob[0],title])
if "[H264-mp4]" in blob[1]:
film_title_prep = blob[1].split(" -")[0].split(' ')
if film_title_prep[0].isdigit():
film_title_prep.pop(0)
title = ' '.join(film_title_prep)
lst_res.append([blob[0],title])
return lst_res
def check_xml_existence(f_name):
f_path = get_write_dir()+"\\"+f_name
print(f_path)
if os.path.isfile(f_path):
return True
else:
False
def lib_key_exists(lib_key):
if check_xml_existence(STATIC_FILES['LIBRARIES']):
return lib_key in list(zip(*getLibsFromXmL(STATIC_FILES['LIBRARIES'])))[0]
def coll_key_exists(coll_key):
if check_xml_existence(STATIC_FILES['COLLECTIONS']):
return coll_key in list(zip(*getCollectionsFromXmL(STATIC_FILES['COLLECTIONS'])))[0]
##########################
##########################
## ##
## / | | /\ \ / ##
## \ +-+ | | \ /\ / ##
## / | | \/ \/ \/ ##
## ##
##########################
##########################
def show_help():
help_string ="""
PLEX SCRIPTOR: python script.py [-new] args\n
\n XML files are stored in user home.
\n -h\t\t\t\t get this help menu. If no args are supplied, also show this help menu.
\n -s\t\t\t\t get server settings, stored in xml file
\n -l\t\t\t\t get libraries, stored in xml file, and show them.
\n -l lib_key\t\t\t get content of a library, identified by key. Key must be a valid library key. Requires -l to have been\n\t\t\t\t executed at least once.
\n -m -term "<terms>"\t\t search for films given your term(s). Terms must be in "". returns matching film data in xml, and\n\t\t\t\t shows the film info.
\n -m -key film_key\t\t get film properties by film_key, stored in xml file. Returns NO if film is not found.
\n -c lib_key\t\t\t get all collections of a library, stored in xml file, and show them. Key: see -l.
\n -c lib_key coll_key\t\t get content of a collection identified by coll_key, located in library identified by lib_key.
\n -c lib_key coll_key -update\t normalize media titles in collection.
\n -------- COMMON ARGUMENTS --------
\n -new\t\t\t\t Tells the script to not look for existing files in user home. Without it, it will use existing data if\n\t\t\t\t present.
"""
print(help_string)
exit(0)
def prepTups2(lst_tups):
tup_for_tup = ['Key: %s --> Name: %s\n' % tup for tup in lst_tups]
str_tup_for_tup = ', '.join(tup_for_tup).replace(',','')
return str_tup_for_tup
def prepTups3(lst_tups):
tup_for_tup = ['Key: %s --> Name: %s ---> Released: %s\n' % tup for tup in lst_tups]
str_tup_for_tup = ', '.join(tup_for_tup).replace(',','')
return str_tup_for_tup
def show_libraries(fname):
lst_libs = getLibsFromXmL(fname)
str_tup_for_tup = prepTups2(lst_libs)
print(
"""
AVAILABLE LIBRARIES:\n\n {}
""".format(str_tup_for_tup)
)
def show_collections(fname):
lst_collections = getCollectionsFromXmL(fname)
str_tup_for_tup = prepTups2(lst_collections)
print(
"""
COLLECTIONS in this LIBRARY:\n\n {}
""".format(str_tup_for_tup)
)
def show_collection_content(fname):
lst_coll_content = getCollectionContentFromXmL(fname)
str_tup_for_tup = prepTups3(lst_coll_content)
print(
"""
FILMS in this COLLECTION:\n\n {}
""".format(str_tup_for_tup)
)
def show_film_search_content(fname):
lst_film_search_content = getFilmSearchContentFromXml(fname)
str_tup_for_tup = prepTups3(lst_film_search_content)
print(
"""
FILMS in this SEARCH:\n\n {}
""".format(str_tup_for_tup)
)