-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkodi_api.py
More file actions
448 lines (374 loc) · 15.4 KB
/
kodi_api.py
File metadata and controls
448 lines (374 loc) · 15.4 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import requests
import json
import time
# Kodi connection details
KODI_IP = "xxx.xxx.xxx.xxx" # Replace with your Kodi IP address
KODI_PORT = 8080
KODI_USERNAME = "xxxx" # Replace with your Kodi username
KODI_PASSWORD = "xxxx" # Replace with your Kodi password
KODI_URL = f"http://{KODI_IP}:{KODI_PORT}/jsonrpc"
HEADERS = {'Content-Type': 'application/json'}
def send_kodi_request(payload):
"""Send a JSON-RPC request to Kodi."""
response = requests.post(KODI_URL, headers=HEADERS, data=json.dumps(payload), auth=(KODI_USERNAME, KODI_PASSWORD))
response.raise_for_status()
return response.json()
# Library update and cleaning
def update_library():
"""Update the Kodi library."""
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.Scan",
"id": 1
}
response = send_kodi_request(payload)
return response.get("result", "Failed to update library")
def clean_library():
"""Clean the Kodi library."""
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.Clean",
"id": 1
}
response = send_kodi_request(payload)
return response.get("result", "Failed to clean library")
# Movie, TV show, and music video retrieval with filtering
def get_movies(search=""):
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.GetMovies",
"params": {"properties": ["title"]},
"id": 1
}
movies = send_kodi_request(payload).get("result", {}).get("movies", [])
return [movie for movie in movies if search.lower() in movie['title'].lower()]
def get_tvshows(search=""):
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.GetTVShows",
"params": {"properties": ["title"]},
"id": 1
}
tvshows = send_kodi_request(payload).get("result", {}).get("tvshows", [])
return [tvshow for tvshow in tvshows if search.lower() in tvshow['title'].lower()]
def get_music_videos(search=""):
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.GetMusicVideos",
"params": {"properties": ["title"]},
"id": 1
}
music_videos = send_kodi_request(payload).get("result", {}).get("musicvideos", [])
return [mv for mv in music_videos if search.lower() in mv['title'].lower()]
# Refresh functions
def refresh_movie(movie_id):
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.RefreshMovie",
"params": {"movieid": movie_id},
"id": 1
}
return send_kodi_request(payload).get("result", "Failed")
def refresh_all_movies():
"""Refresh all movies in the Kodi library."""
# Get the list of all movies
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.GetMovies",
"params": {"properties": ["title"]},
"id": 1
}
try:
# Send request to Kodi
response = send_kodi_request(payload)
movies = response.get("result", {}).get("movies", [])
if not movies:
print("No movies found in the library. Response received:", response)
return
# Loop through each movie and refresh it
for movie in movies:
movie_id = movie.get("movieid")
movie_title = movie.get("title", "Untitled Movie")
# Refresh the movie metadata
result = refresh_movie(movie_id)
print(f"Refreshing movie '{movie_title}': {result}")
time.sleep(0.25)
except Exception as e:
print("An error occurred while trying to refresh all movies:", str(e))
def refresh_tvshow(tvshow_id):
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.RefreshTVShow",
"params": {"tvshowid": tvshow_id},
"id": 1
}
return send_kodi_request(payload).get("result", "Failed")
def refresh_tvshow_with_episodes(tvshow_id, tvshow_label=""):
"""Refresh a single TV show and all its episodes."""
# Refresh the TV show metadata
result = refresh_tvshow(tvshow_id)
print(f"Refreshing TV show '{tvshow_label}': {result}")
# Retrieve all episodes for the specified TV show
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.GetEpisodes",
"params": {"tvshowid": tvshow_id},
"id": 1
}
episodes_response = send_kodi_request(payload)
episodes = episodes_response.get("result", {}).get("episodes", [])
if not episodes:
print(f"No episodes found for TV show '{tvshow_label}'. Response received:", episodes_response)
return
# Refresh each episode individually
for episode in episodes:
episode_id = episode.get("episodeid")
episode_label = episode.get("label", "Untitled Episode")
# Refresh episode metadata
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.RefreshEpisode",
"params": {"episodeid": episode_id},
"id": 1
}
episode_result = send_kodi_request(payload).get("result", "Failed")
print(f" - Refreshing episode '{episode_label}': {episode_result}")
time.sleep(0.25)
def refresh_all_tvshows():
"""Refresh all TV shows and each episode within them."""
# Get the list of all TV shows without specifying properties
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.GetTVShows",
"params": {},
"id": 1
}
try:
# Send request to Kodi
response = send_kodi_request(payload)
tvshows = response.get("result", {}).get("tvshows", [])
if not tvshows:
print("No TV shows found in the library. Response received:", response)
return
# Loop through each TV show and refresh it along with all its episodes
for tvshow in tvshows:
tvshow_id = tvshow.get("tvshowid")
tvshow_title = tvshow.get("label", "Untitled TV Show") # Use 'label' instead of 'title'
# Refresh the TV show metadata
result = refresh_tvshow(tvshow_id)
print(f"Refreshing TV show '{tvshow_title}': {result}")
# Get episodes for the current TV show
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.GetEpisodes",
"params": {"tvshowid": tvshow_id},
"id": 1
}
episodes_response = send_kodi_request(payload)
episodes = episodes_response.get("result", {}).get("episodes", [])
if not episodes:
print(f"No episodes found for TV show '{tvshow_title}'. Response received:", episodes_response)
continue
# Refresh each episode in the current TV show
for episode in episodes:
episode_id = episode.get("episodeid")
episode_title = episode.get("label", "Untitled Episode") # Use 'label' instead of 'title'
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.RefreshEpisode",
"params": {"episodeid": episode_id},
"id": 1
}
episode_result = send_kodi_request(payload).get("result", "Failed")
print(f" - Refreshing episode '{episode_title}': {episode_result}")
time.sleep(0.25)
except Exception as e:
print("An error occurred while trying to refresh all TV shows:", str(e))
def refresh_music_video(music_video_id):
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.RefreshMusicVideo",
"params": {"musicvideoid": music_video_id},
"id": 1
}
return send_kodi_request(payload).get("result", "Failed")
def refresh_all_music_videos():
"""Refresh all music videos in the Kodi library."""
# Get the list of all music videos
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.GetMusicVideos",
"params": {"properties": ["title"]},
"id": 1
}
try:
# Send request to Kodi
response = send_kodi_request(payload)
music_videos = response.get("result", {}).get("musicvideos", [])
if not music_videos:
print("No music videos found in the library. Response received:", response)
return
# Loop through each music video and refresh it
for mv in music_videos:
music_video_id = mv.get("musicvideoid")
music_video_title = mv.get("title", "Untitled Music Video")
# Refresh the music video metadata
result = refresh_music_video(music_video_id)
print(f"Refreshing music video '{music_video_title}': {result}")
time.sleep(0.25)
except Exception as e:
print("An error occurred while trying to refresh all music videos:", str(e))
# Delete functions
def delete_movie(movie_id):
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.RemoveMovie",
"params": {"movieid": movie_id},
"id": 1
}
return send_kodi_request(payload).get("result", "Failed")
def delete_tvshow(tvshow_id):
payload = {
"jsonrpc": "2.0",
"method": "VideoLibrary.RemoveTVShow",
"params": {"tvshowid": tvshow_id},
"id": 1
}
return send_kodi_request(payload).get("result", "Failed")
# Main menu options
def main_menu():
while True:
print("\nKodi Library Management:")
print("1. Update Kodi library")
print("2. Clean Kodi library")
print("3. Refresh Kodi library")
print("4. Delete items from Kodi library")
print("5. Exit")
choice = input("Choose an option (1-5): ")
if choice == "1":
result = update_library()
print("Library update:", result)
elif choice == "2":
result = clean_library()
print("Library clean:", result)
elif choice == "3":
refresh_menu()
elif choice == "4":
delete_menu()
elif choice == "5":
print("Exiting...")
break
else:
print("Invalid option. Please try again.")
def refresh_menu():
while True:
print("\nRefresh Options:")
print("1. Refresh a specific movie by search")
print("2. Refresh a specific TV show and episodes by search")
print("3. Refresh a specific music video by search")
print("4. Refresh all movies")
print("5. Refresh all TV shows and episodes")
print("6. Refresh all music videos")
print("7. Exit to main menu")
choice = input("Choose an option (1-7): ")
if choice == "1":
search = input("Enter movie search term: ")
movies = get_movies(search)
if not movies:
print("No movies found.")
continue
for idx, movie in enumerate(movies):
print(f"{idx + 1}. {movie['title']}")
print(f"{len(movies) + 1}. Exit")
choice = int(input("Enter the number of the movie to refresh: "))
if choice == len(movies) + 1:
continue
movie_id = movies[choice - 1]["movieid"]
result = refresh_movie(movie_id)
print(f"Movie refresh result: {result}")
elif choice == "2":
search = input("Enter TV show search term: ")
tvshows = get_tvshows(search)
if not tvshows:
print("No TV shows found.")
continue
for idx, tvshow in enumerate(tvshows):
print(f"{idx + 1}. {tvshow['label']}")
choice = int(input("Enter the number of the TV show to refresh (or 0 to exit): ")) - 1
if choice >= 0:
tvshow_id = tvshows[choice]["tvshowid"]
tvshow_label = tvshows[choice].get("label", "Untitled TV Show")
refresh_tvshow_with_episodes(tvshow_id, tvshow_label)
elif choice == "3":
search = input("Enter music video search term: ")
music_videos = get_music_videos(search)
if not music_videos:
print("No music videos found.")
continue
for idx, mv in enumerate(music_videos):
print(f"{idx + 1}. {mv['title']}")
print(f"{len(music_videos) + 1}. Exit")
choice = int(input("Enter the number of the music video to refresh: "))
if choice == len(music_videos) + 1:
continue
music_video_id = music_videos[choice - 1]["musicvideoid"]
result = refresh_music_video(music_video_id)
print(f"Music video refresh result: {result}")
elif choice == "4":
print("Refreshing all movies...")
refresh_all_movies()
elif choice == "5":
print("Refreshing all TV shows and their episodes...")
refresh_all_tvshows()
elif choice == "6":
print("Refreshing all music videos...")
refresh_all_music_videos()
elif choice == "7":
print("Returning to main menu...")
break
else:
print("Invalid option. Please try again.")
def delete_menu():
while True:
print("\nDelete Options:")
print("1. Delete a specific movie by search")
print("2. Delete a specific TV show and its episodes by search")
print("3. Exit to main menu")
choice = input("Choose an option (1-3): ")
if choice == "1":
search = input("Enter movie search term: ")
movies = get_movies(search)
if not movies:
print("No movies found.")
continue
for idx, movie in enumerate(movies):
print(f"{idx + 1}. {movie['title']}")
print(f"{len(movies) + 1}. Exit")
choice = int(input("Enter the number of the movie to delete: "))
if choice == len(movies) + 1:
continue
movie_id = movies[choice - 1]["movieid"]
result = delete_movie(movie_id)
print(f"Movie delete result: {result}")
elif choice == "2":
search = input("Enter TV show search term: ")
tvshows = get_tvshows(search)
if not tvshows:
print("No TV shows found.")
continue
for idx, tvshow in enumerate(tvshows):
print(f"{idx + 1}. {tvshow['title']}")
print(f"{len(tvshows) + 1}. Exit")
choice = int(input("Enter the number of the TV show to delete: "))
if choice == len(tvshows) + 1:
continue
tvshow_id = tvshows[choice - 1]["tvshowid"]
result = delete_tvshow(tvshow_id)
print(f"TV Show delete result: {result}")
elif choice == "3":
print("Returning to main menu...")
break
else:
print("Invalid option. Please try again.")
if __name__ == "__main__":
main_menu()