-
Notifications
You must be signed in to change notification settings - Fork 8
/
plex_backend.py
executable file
·77 lines (68 loc) · 2.7 KB
/
plex_backend.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
from collections import defaultdict
from json import load, dump
from plexapi.server import PlexServer
class PlexBackend():
def __init__(self, plexurl, token, libname, data_path):
self.token = token
self.plexurl = plexurl
self.lib_name = libname
self.data_path = data_path
self.plex = PlexServer(self.plexurl, self.token)
self.music = self.plex.library.section(self.lib_name)
def down_plex_lib(self):
songs = {}
try:
playlists = self.plex.playlists()
songs["playlist"] = {}
for p in playlists:
p_name = p.title
songs["playlist"][p_name] = []
for track in p.items():
title = track.title
album = track.album().title
artist = track.artist().title
file_key = self.get_file(track)
file = self.get_tokenized_uri( file_key )
songs["playlist"][p_name].append([artist, album, title, file])
root = self.music.all()
artists = defaultdict(list)
albums = defaultdict(list)
titles = defaultdict(list)
count = 0
for artist in root:
artist_title = artist.title
songs[artist_title] = {}
for album in artist.albums():
album_title = album.title
songs[artist_title][album_title] = []
for track in album.tracks():
title = track.title
file_key = self.get_file(track)
file = self.get_tokenized_uri( file_key )
try:
print("""%d
%s -- %s
%s
%s
""" % (count, artist_title, album_title, title,file_key))
songs[artist_title][album_title].append([title, file])
count += 1
except Exception as ex:
print(ex)
self.json_save(songs, self.data_path)
print("done loading library")
except Exception as e:
print(e)
return None
def json_save(self, data, fname):
with open(fname, 'w') as fp:
dump(data, fp)
def json_load(self, fname):
with open(fname, 'r') as fp:
return load(fp)
def get_tokenized_uri(self, uri):
return self.plexurl+uri+"?X-Plex-Token="+self.token
def get_file(self,track):
for media in track.media:
for p in media.parts:
return p.key