-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibrary.py
412 lines (352 loc) · 12.4 KB
/
library.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
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
import os
import re
import math
from datetime import timedelta
import pprint
import logging
from tinydb import TinyDB, Query
from tqdm import tqdm
from config import built_ins, MAX_BATCH
from util import chunks, file_time_str
from tablo.api import Api
from tablo.apiexception import APIError
from tablo.entities.show import Show
from recording import Recording
logger = logging.getLogger(__name__)
def view(args):
print()
path = built_ins['db']['recordings']
rec_db = TinyDB(path)
id_set = []
cnt = 0
for item in rec_db.all():
cnt += 1
if args.id_list:
obj_id = item['data']['object_id']
if obj_id not in id_set:
id_set.append(obj_id)
elif args.full:
pprint.pprint(item)
else:
Recording(item['data']).print()
if args.id_list:
print(id_set)
else:
print(f'Total recordings found: {cnt}')
def print_stats():
path = built_ins['db']['recordings']
rec_db = TinyDB(path)
shows = Query()
shows_qry = shows.data
field_title = '{:17}'
print("Overview")
print("-" * 50)
print(f"Built: {file_time_str(path)}")
cnt = len(rec_db.all())
print('{:10}'.format("Total Recordings") + ": " + f'{cnt}')
cnt = rec_db.count(shows_qry.user_info.watched == True) # noqa: E712
print('{:10}'.format("Total Watched") + ": " + f'{cnt}')
print()
print("By Current Recording State")
print("-"*50)
cnt = rec_db.count(shows_qry.video_details.state == 'finished')
print(field_title.format("Finished") + ": " + f'{cnt}')
cnt = rec_db.count(shows_qry.video_details.state == 'failed')
print(field_title.format("Failed") + ": " + f'{cnt}')
cnt = rec_db.count(shows_qry.video_details.state == 'recording')
print(field_title.format("Recording") + ": " + f'{cnt}')
print()
print("By Recording Type")
print("-" * 50)
cnt = rec_db.count(shows.path.matches(f'.*episode.*', flags=re.IGNORECASE))
print(field_title.format("Episodes/Series") + ": " + f'{cnt}')
cnt = rec_db.count(shows.path.matches(f'.*movie.*', flags=re.IGNORECASE))
print(field_title.format("Movies") + ": " + f'{cnt}')
cnt = rec_db.count(shows.path.matches(f'.*sports.*', flags=re.IGNORECASE))
print(field_title.format("Sports/Events") + ": " + f'{cnt}')
cnt = rec_db.count(
shows.path.matches(f'.*programs.*', flags=re.IGNORECASE)
)
print(field_title.format("Programs") + ": " + f'{cnt}')
print()
print("By Show")
print("-" * 50)
shows = {}
max_width = 0
for item in rec_db.all():
title = item['data']['airing_details']['show_title']
max_width = max(max_width, len(title))
key = _sortable_title(title)
if key not in shows.keys():
shows[key] = {'cnt': 1, 'title': title}
else:
shows[key]['cnt'] += 1
for key in sorted(shows.keys()):
# print(f"{shows[key]['title']} - {shows[key]['cnt']}")
print(
('{:' + str(max_width) + '}').format(shows[key]['title']) +
' - {:>2}'.format(shows[key]['cnt'])
)
def _sortable_title(title):
# toss a/an/the, force non-letters to end
articles = ['a', 'an', 'the']
word = title.split(' ', 1)[0].lower()
sort_title = title
if word in articles:
try:
sort_title = title.split(' ', 1)[1]
except Exception:
sort_title = title
if ord(sort_title[0]) not in range(ord('A'), ord('z') + 1):
sort_title = "ZZZZ" + sort_title
return sort_title
def build():
print("Building library. NO videos are being fetched.")
print("-"*50)
Api.discover()
connected = Api.selectDevice()
if not connected:
logger.exception("NOT CONNECTED")
# don't think we'll need this
# _build_guide()
_build_recordings()
def _build_guide():
guide_path = built_ins['db']['guide']
if not built_ins['dry_run']:
try:
os.unlink(guide_path)
except Exception:
pass
guide_db = {}
if not built_ins['dry_run']:
guide_db = TinyDB(guide_path)
# Load all the shows
print('Loading All Guide/Show data')
sections = Api.views('guide').shows.get()
total = sum(len(section.get('contents')) for section in sections)
print(f"Total Shows: {total}")
for section in sections:
contents = section.get('contents')
if not contents:
logger.info(f"Section {section.get('key').upper()} (0)")
continue
logger.info(f"Section {section.get('key').upper()} ({len(contents)})")
for piece in chunks(contents, MAX_BATCH):
shows = Api.batch.post(piece)
for path, data in shows.items():
show = Show.newFromData(data)
if not built_ins['dry_run']:
guide_db.insert({
'id': show.object_id,
'path': show.path,
'data': show.data,
'version': Api.device.version
})
def _build_recordings():
recs_path = built_ins['db']['recordings']
recshow_path = built_ins['db']['recording_shows']
if not built_ins['dry_run']:
try:
os.unlink(recs_path)
except Exception:
pass
try:
os.unlink(recshow_path)
except Exception:
pass
recs_db = TinyDB(recs_path)
programs = Api.recordings.airings.get()
show_paths = []
print(f"Total Recordings: {len(programs)}")
# cnt = 0
with tqdm(total=len(programs)) as pbar:
for piece in chunks(programs, MAX_BATCH):
airings = Api.batch.post(piece)
# cnt += len(airings)
# print(f"\tchunk: {cnt}/{len(programs)}")
for path, data in airings.items():
airing = Recording(data)
if airing.showPath not in show_paths:
show_paths.append(airing.showPath)
if not built_ins['dry_run']:
recs_db.insert({
'id': airing.object_id,
'path': airing.path,
'show_path': airing.showPath,
'data': airing.data,
'version': Api.device.version
})
pbar.update(1)
recshow_db = TinyDB(recshow_path)
print(f"Total Recorded Shows: {len(show_paths)}")
my_show = Query()
with tqdm(total=len(show_paths)) as pbar:
# this is silly and just to make the progress bar move :/
for piece in chunks(show_paths, math.ceil(MAX_BATCH/5)):
# not caring about progress, we'd use this:
# for piece in chunks(show_paths, MAX_BATCH):
airing_shows = Api.batch.post(piece)
for path, data in airing_shows.items():
stuff = recshow_db.search(my_show.show_path == path)
pbar.update(1)
if not stuff:
if not built_ins['dry_run']:
recshow_db.insert({
'id': data['object_id'],
'show_path': path,
'data': data,
'version': Api.device.version
})
print("Done!")
def print_dupes():
dupes = _find_dupes()
for key, data in dupes.items():
if len(data) > 1:
print(key + " = " + str(len(data)))
for item in data:
rec = Recording(item)
print("\t" + str(rec.object_id) + " | " +
rec.get_description() + " - " + rec.get_dur())
def _find_dupes():
path = built_ins['db']['recordings']
rec_db = TinyDB(path)
dupes = {}
for item in rec_db.all():
data = item['data']
if 'episode' in data.keys():
tmsid = data['episode']['tms_id']
if tmsid.startswith('SH'):
# TODO: this is easy, but wrong. SH* tms_id duplicates for
# every episode. Maybe replace with psuedo-title?
continue
if tmsid not in dupes:
dupes[tmsid] = []
dupes[tmsid].append(data)
else:
dupes[tmsid].append(data)
return dupes
def print_incomplete(args):
# weird way I made it work...
percent = args.incomplete
if percent == -1:
percent = 100
else:
percent = min(percent, 100)
percent = max(percent, 0)
percent = percent / 100
dupes = _find_dupes()
proper_dur = 0
matched = 0
total_recs = 0
id_set = []
for key, data in dupes.items():
if key.startswith('SH'):
continue
if len(data) > 0:
sum_actual_dur = 0
recs = []
for item in data:
rec = Recording(item)
actual_dur = rec.video_details['duration']
proper_dur = rec.airing_details['duration']
sum_actual_dur += actual_dur
if proper_dur > actual_dur:
recs.append(rec)
if (proper_dur * percent) > sum_actual_dur:
matched += 1
total_recs += len(recs)
header = None
for x in recs:
if args.id_list:
if x.object_id not in id_set:
id_set.append(x.object_id)
else:
if not header:
header = x.get_description() + \
" - " + x.episode['tms_id']
print(header)
print("\t" + str(x.object_id) + " | " +
x.get_description() + " - " + x.get_dur())
if not args.id_list:
sum_txt = str(timedelta(seconds=sum_actual_dur))
total_txt = str(timedelta(seconds=proper_dur))
pct = str(round(sum_actual_dur / proper_dur * 100, 2))
print(f"\n\t{sum_txt} / {total_txt} ({pct}%)")
print()
if args.id_list:
print(id_set)
else:
print(f"Total incomplete shows less than {percent*100}% - {matched} "
f"({total_recs} items)")
def delete(id_list, args):
# TODO: add a confirmation (sans --yyyyassss)
total = len(id_list)
if total == 0:
print(f"Nothing to delete, exiting...")
return
elif total == 1:
print(f"Deleting {total} recording")
else:
print(f"Deleting {total} recordings")
print("-" * 50)
# Load all the recs
path = built_ins['db']['recordings']
rec_db = TinyDB(path)
shows = Query()
# shortcut for later
shows_qry = shows.data
recs = []
total = 0
for obj_id in id_list:
obj = rec_db.get(
(shows_qry.object_id == int(obj_id))
&
(shows_qry.video_details.state != 'recording')
)
if not obj:
print(f'ERROR: Unable to find recording with '
f'object_id == "{obj_id}", skipping...')
continue
total += 1
recs.append(
{
'doc_id': obj.doc_id,
'obj_id': obj_id,
'rec': Recording(obj['data'])
})
# TODO: don't "total" like this
if total <= 0:
print(f"No recordings found; {len(id_list)} requested.")
elif total == 1:
print(f"Deleting {total} recording...")
else:
print(f"Deleting {total} recordings...")
if total > 0:
for rec in recs:
rec = rec['rec']
print(f" - {rec.get_actual_dur()} | {rec.get_description()} ")
print("-" * 50)
if not args.yes:
print()
print('\tAdd the "--yes" flag to actually delete things...')
print()
else:
for rec in recs:
_delete(rec, rec_db)
print("\nFINISHED")
def _delete(rec, rec_db):
doc_id = rec['doc_id']
item = rec['rec']
print(f"Deleting: {item.get_description()} ({item.get_actual_dur()})")
if built_ins['dry_run']:
print("DRY RUN: would have deleted...")
else:
try:
# try to delete the full recording
item.delete()
# delete the local db record instead of REBUILDing everything
rec_db.remove(doc_ids=[doc_id])
print("\tDeleted!")
except APIError:
print("Recording no longer exists")
pass