-
Notifications
You must be signed in to change notification settings - Fork 0
/
FehPlexSlideShow.py
232 lines (165 loc) · 6.96 KB
/
FehPlexSlideShow.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
#!/usr/bin/env python3
import sys
import time
import os
from plexapi.myplex import MyPlexAccount
from plexapi.server import PlexServer
import subprocess
##############################################
def read_config(cfgfile='FehPlexSlideShow.config'):
fpssConfig = {}
numtokens = 0
with open(cfgfile, mode='r') as f:
configlines = f.read().splitlines()
f.close()
for i in range(0,len(configlines)):
cline = configlines[i].split("=")
if cline[0] == 'PlexUsername':
print("Plex Username:", cline[1])
fpssConfig['plexusername'] = cline[1]
numtokens += 1
if cline[0] == 'PlexPassword':
print("Plex Password:", cline[1])
fpssConfig['plexpassword'] = cline[1]
numtokens += 1
if cline[0] == 'PlexServer':
print("Plex Server:", cline[1])
fpssConfig['plexserver'] = cline[1]
numtokens += 1
if cline[0] == 'PlexAuthToken':
print("Plex AuthToken:", cline[1])
fpssConfig['plexauthtoken'] = cline[1]
numtokens += 1
if cline[0] == 'FehPlaylistFile':
print("Feh Playlist:", cline[1])
fpssConfig['fehplaylistfile'] = cline[1]
numtokens += 1
if cline[0] == 'PlexMethod':
print("Plex Method:" , cline[1])
fpssConfig['plexmethod'] = cline[1]
numtokens += 1
if numtokens < 3:
print("Missing FehPlexSlideShow configuration information")
exit(-2)
return fpssConfig
####### end read_config
##############################################
##############################################
def get_plex_photos():
plexPhotos = {}
numphotos = 0
#loading()
# Loop through all libraries looking for photos
for section in plex.library.sections():
if section.type == 'photo':
#print("Name:", section.title, "Type:", section.type)
for section_item in section.all():
#print("iName:", section_item.title, "Type:", section_item.type)
for photo in section_item.photos():
# Query for photo key (returns Media Container)
qobj = plex.query(photo.key)
# Need media part of media container
mpart = qobj[0][0][0].get('key')
# Various Media information
photo_height = qobj[0][0].get('height')
photo_width = qobj[0][0].get('width')
photo_aspect_ratio = qobj[0][0].get('aspectRatio')
#if img_width > img_height:
# print(img_height, img_width, img_aspect_ratio)
# print(photo.title, photo.media, photo.key, photo.ratingKey, imgurl)
# Direct URL to media part (only works when local but useful for debuggin)
photo_direct_url = plex.url(mpart, includeToken=True)
# plex.url works when local to PMS, but not public network
#photo_url2 = plex.url(mpart)
#photo_url2 = photo_url2 + '?X-Plex-Token=' + fpssConfig['plexauthtoken']
#print("PhotoURL:",photo_direct_url, photo_url2)
#plexPhotos[photo.key] = dict([('title', photo.title), ('url', photo_direct_url)])
# Using transcode call since it works on local and pub networks
photo_url = plex.transcodeImage(mpart, photo_height, photo_width)
plexPhotos[photo.key] = dict([('title', photo.title), ('url', photo_url)])
print("Adding", photo.key, photo_direct_url)
return plexPhotos
####### end get_plex_photos
##############################################
##############################################
def feh_write_playlist(photos, playlist_file='FehPlexSlideShow.playlist'):
if len(photos) == 0:
print("Empty photos")
return False
with open(playlist_file, mode='w') as f:
for key in photos.keys():
f.write(photos[key]['url'] + '\n')
f.close()
return True
####### end feh_write_playlist
##############################################
##############################################
def feh_slideshow(playlist_file = 'FehPlexSlideShow.playlist', debugonly = False):
# feh cmd
feh_cmd = 'feh -Z -F -Y -D 7 --cycle-once -Y --auto-rotate -f ' + playlist_file
if debugonly:
print("Running command:", feh_cmd)
else:
#os.system(feh_cmd)
myproc = subprocess.Popen(["feh", "-Z", "-F", "-Y", "-D 7", "--cycle-once", "--auto-rotate", "-f", playlist_file])
return myproc
# end feh_slideshow
##############################################
##############################################
def loading():
lproc = subprocess.Popen(["feh", "-Z", "-F", "-Y", "FehPlexSlideShow-loading.jpg"])
#time.sleep(5)
#proc.terminate()
return lproc
# end loading
##############################################
def get_plex_server():
PlexMethod = MyPlexAccount
if fpssConfig['plexmethod'] == 'MyPlexAccount':
# Use the My Plex Account method of connecting
#account = MyPlexAccount(fpssConfig['plexusername'], fpssConfig['plexpassword'], token=fpssConfig['plexauthtoken'], timeout=5)
account = MyPlexAccount(fpssConfig['plexusername'], fpssConfig['plexpassword'], timeout=5)
cons = account.resource(fpssConfig['plexserver']).connections
print("Connections available:", len(cons), "Access Token:", account.resource(fpssConfig['plexserver']).accessToken)
for r in range(0, len(cons)):
conr = cons[r]
#print(dir(conr))
print(conr.address, conr.httpuri, conr.protocol, conr.key, conr.uri, conr.local)
#print(cons[r])
#exit()
try:
pms = account.resource(fpssConfig['plexserver']).connect()
except plexapi.exceptions.NotFound:
print("Could not connect to plex")
exit(-1)
else:
baseurl = fpssConfig['plexmethod']
pms = PlexServer(baseurl, fpssConfig['plexauthtoken'])
return pms
# end get plex server
##############################################
# Read in config file
fpssConfig = read_config('Local.config')
DoRun = True
FirstTime = True
try:
while DoRun:
# Load splash screen
proc = loading()
# Connect to the Plex Server
plex = get_plex_server()
# Get the photos from Plex Server
plexPhotos = get_plex_photos()
# Write the feh playlist file
feh_write_playlist(plexPhotos, playlist_file=fpssConfig['fehplaylistfile'])
# Terminate splash screen
proc.kill()
#feh_slideshow(playlist_file=fpssConfig['fehplaylistfile'], debugonly=True)
ssproc = feh_slideshow(playlist_file=fpssConfig['fehplaylistfile'])
ssproc.wait()
except:
DoRun = False
proc.kill()
ssproc.kill()
exit()
#os.system("rm" + fpssConfig['fehplaylistfile'])