Skip to content

Commit 58fabe9

Browse files
committed
Add print to alert_callback
1 parent 1ad76a6 commit 58fabe9

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

tools/plex-bootstraptest.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,26 @@ def get_tvshow_path(name, season, episode):
7272
return os.path.join(tvshows_path, name, 'S%02dE%02d.mp4' % (season, episode))
7373

7474

75-
def create_section(server, section):
75+
def add_library_section(server, section, opts):
76+
""" Add the specified section to our Plex instance. This tends to be a bit
77+
flaky, so we retry a few times here.
78+
"""
79+
start = time.time()
80+
runtime = 0
81+
while runtime < opts.bootstrap_timeout:
82+
try:
83+
server.library.add(**section)
84+
return True
85+
except BadRequest as err:
86+
if 'server is still starting up. Please retry later' in str(err):
87+
time.sleep(1)
88+
continue
89+
raise
90+
runtime = time.time() - start
91+
raise SystemExit('Timeout adding section to Plex instance.')
92+
93+
94+
def create_section(server, section, opts):
7695
processed_media = 0
7796
expected_media_count = section.pop('expected_media_count', 0)
7897
expected_media_type = (section['type'], )
@@ -81,7 +100,12 @@ def create_section(server, section):
81100
expected_media_type = tuple(SEARCHTYPES[t] for t in expected_media_type)
82101

83102
def alert_callback(data):
103+
""" Listen to the Plex notifier to determine when metadata scanning is complete.
104+
* state=1 means record processed, when no metadata source was set
105+
* state=5 means record processed, applicable only when metadata source was set
106+
"""
84107
global processed_media
108+
print(data)
85109
if data['type'] == 'timeline':
86110
for entry in data['TimelineEntry']:
87111
if entry.get('identifier', 'com.plexapp.plugins.library') == 'com.plexapp.plugins.library':
@@ -99,29 +123,17 @@ def alert_callback(data):
99123
elif entry['state'] == 1 and entry['type'] == SEARCHTYPES['photo']:
100124
bar.update()
101125

126+
runtime = 0
127+
start = time.time()
102128
bar = tqdm(desc='Scanning section ' + section['name'], total=expected_media_count)
103129
notifier = server.startAlertListener(alert_callback)
104-
# I don't know how to determinate of plex successfully started,
105-
# so let's do it in creepy way
106-
success = False
107-
start_time = time.time()
108-
while not success and (time.time() - start_time < opts.bootstrap_timeout):
109-
try:
110-
server.library.add(**section)
111-
success = True
112-
except BadRequest as e:
113-
if 'the server is still starting up. Please retry later' in str(e):
114-
time.sleep(1)
115-
else:
116-
raise
117-
if not success:
118-
print('Something went wrong :(')
119-
exit(1)
130+
add_library_section(server, section, opts)
120131
while bar.n < bar.total:
121-
if time.time() - start_time >= opts.bootstrap_timeout:
122-
print('Metadata scan takes too long, probably something went really wrong')
132+
if runtime >= opts.bootstrap_timeout:
133+
print('Metadata scan taking too long, probably something went really wrong')
123134
exit(1)
124135
time.sleep(3)
136+
runtime = time.time() - start
125137
bar.close()
126138
notifier.stop()
127139

0 commit comments

Comments
 (0)