-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpodcli.py
More file actions
executable file
·496 lines (446 loc) · 16.9 KB
/
podcli.py
File metadata and controls
executable file
·496 lines (446 loc) · 16.9 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env python
"""Command Line Podcast Manager
You can add podcast RSS feeds, download podcasts, and sync them to a directory.
If the podcast ID3 tags are messy or unclear you can also automatically edit
them by setting the id3_edit config.
id3_edit structure:
"id3_edit": {
"podcast table id": {
"album": "string to manually set album to",
"artist": "string to manually set artist to",
"title": "optional parameter: if set to copy_item will set to title
from the rss feed, otherwise if set to true will set
title to rss published date + album config"
}
}
"""
import json
import argparse
import urllib.request, urllib.parse, urllib.error
import os
import sys
import shutil
import urllib.parse
import subprocess
import feedparser
from peewee import CharField, ForeignKeyField, DateTimeField, BooleanField, SqliteDatabase, Model, IntegrityError, TextField
from time import mktime, sleep
from datetime import datetime, timedelta
from downloader import Download, DownloadError
import mutagen
from mutagen import easyid3
from bs4 import BeautifulSoup
import unicodedata
import textwrap
from terminaltables import AsciiTable
from terminaltables.width_and_alignment import max_dimensions
from terminaltables.build import flatten
from blessings import Terminal
from gevent import monkey
monkey.patch_all()
import gevent
def load_config():
f = open('podcli_config.json', 'r')
return json.load(f)
def get_enclosure(links):
for x in links:
if x['rel'] == 'enclosure':
return x['href']
# Models
db = SqliteDatabase(load_config()['db'])
class PodcastTable(Model):
feed = CharField(unique=True)
title = CharField()
class Meta:
database = db # this model uses the podcli database
class EpisodeTable(Model):
podcast = ForeignKeyField(PodcastTable, related_name='episodes')
title = CharField()
published = DateTimeField()
enclosure = CharField()
summary = TextField(null=True)
new = BooleanField()
class Meta:
database = db # this model uses the podcli database
def create_tables():
PodcastTable.create_table(fail_silently=True)
EpisodeTable.create_table(fail_silently=True)
def ascii_table_last(ascii_table):
dimensions = max_dimensions(ascii_table.table_data,
ascii_table.padding_left,
ascii_table.padding_right)[:3]
whole_table = ascii_table.table_data
ascii_table.table_data = (ascii_table.table_data[-1], )
last_table = flatten(ascii_table.gen_table(*dimensions))
ascii_table.table_data = whole_table
return last_table
def get_max_dimensions(ascii_table):
return max_dimensions(ascii_table.table_data, ascii_table.padding_left,
ascii_table.padding_right)[:3]
class PodCli(object):
def __init__(self):
self.config = load_config()
self.download_dir = self.get_download_dir()
self.check_download_dir()
def get_download_dir(self):
if 'download_folder' in list(self.config.keys()):
if os.path.isabs(self.config['download_folder']):
return self.config['download_folder']
else:
return os.path.join(
os.path.dirname(os.path.realpath(sys.argv[0])),
self.config['download_folder'])
else:
return os.path.dirname(os.path.realpath(sys.argv[0]))
def check_download_dir(self):
if not os.path.exists(self.download_dir):
os.mkdir(self.download_dir)
def add_podcast(self, rss_url):
feed = feedparser.parse(rss_url)
try:
PodcastTable.create(feed=rss_url, title=feed['feed']['title'])
except IntegrityError:
print('Podcast already exists.')
for pod in PodcastTable.select():
print(pod.title, pod.feed)
def get_summary(self, item):
text = BeautifulSoup(item["summary"], "html.parser").get_text()
return unicodedata.normalize("NFKD", text)
def print_summary(self, summary):
if summary:
term = Terminal()
for line in textwrap.wrap(
summary,
term.width,
initial_indent=' ',
subsequent_indent=' '):
print(line)
def print_summary_table(self, items=None):
table_headers = [['title', 'summary']]
table_data = []
ascii_table = None
if not items:
items = EpisodeTable.select().where(EpisodeTable.new)
for item in items:
term = Terminal()
summ = ""
for line in textwrap.wrap(
item.summary,
term.width * 0.7,
initial_indent=' ',
subsequent_indent=' '):
summ += line + "\n"
table_data.append([item.podcast.title, summ])
ascii_table = AsciiTable(table_headers + table_data)
ascii_table.inner_row_border = True
if ascii_table:
print(ascii_table.table)
else:
"Nothing to show"
def print_download_item(self, item, ascii_table):
dimensions = get_max_dimensions(ascii_table)
title = ""
for line in textwrap.wrap(
item.podcast.title,
dimensions[0][0],
initial_indent=' ',
subsequent_indent=' '):
title += line + "\n"
summ = ""
for line in textwrap.wrap(
item.summary,
dimensions[0][1],
initial_indent=' ',
subsequent_indent=' '):
summ += line + "\n"
if ascii_table:
ascii_table.table_data.append([title, summ])
print(ascii_table_last(ascii_table))
return ascii_table
else:
table_headers = [['title', 'summary']]
table_data = [[title, summ]]
ascii_table = AsciiTable(table_headers + table_data)
ascii_table.inner_row_border = True
print(ascii_table.table)
return ascii_table
def refresh_all(self):
# print("Refreshing feeds ...")
spawned = []
for pod in PodcastTable.select():
spawned.append(gevent.spawn(self.get_podcast_feed, pod.feed, pod))
gevent.iwait
dots = 1
while True:
alive = False
for gl in spawned:
if not gl.dead:
alive = True
if not alive:
print("\n")
break
message = "Refreshing feeds " + "." * dots
sys.stdout.write('\x1b[2K\r' + message)
sys.stdout.flush()
dots += 1
if dots > 4:
dots = 1
gevent.sleep(0.5)
def get_podcast_feed(self, url, pod):
feed = feedparser.parse(url)
for item in feed['entries']:
enclosure = self.get_enclosure(item)
if not enclosure:
# print('%s has no link, skipping...' % item.title)
continue
# If episode enclosure doesn't exist, add it
if EpisodeTable.select().where(
EpisodeTable.enclosure == enclosure).count() < 1:
dt = datetime.fromtimestamp(mktime(item['published_parsed']))
summary = self.get_summary(item)
# print('New Episode: ', pod.title, " -- ", item['title'], dt.strftime('%d/%m/%Y'))
# self.print_summary(summary)
# print("\n")
EpisodeTable.create(
podcast=pod,
title=item['title'],
published=dt,
enclosure=enclosure,
summary=summary,
new=True)
# print("Refreshed feed: %s" % pod.title)
def get_enclosure(self, episode):
if 'links' not in list(episode.keys()):
return False
for link in episode['links']:
if link['rel'] == 'enclosure':
return link['href']
def is_downloaded(self, url, filename):
if not os.path.exists(filename):
return False
try:
df = Download(url, filename)
filesize = df.get_url_file_size()
except urllib.error.HTTPError:
print("HTTTP Error Skipping")
return True
if not filesize:
return False
elif int(filesize) > os.path.getsize(filename):
print("filesize mismatch: %s %s" % (filesize,
os.path.getsize(filename)))
return False
else:
return True
def download_all_new(self):
print("Downloading ...")
spawned = []
ascii_table = None
for item in EpisodeTable.select().where(EpisodeTable.new):
filename = self.get_fullpath(item.enclosure)
if not self.is_downloaded(item.enclosure, filename):
ascii_table = self.print_download_item(item, ascii_table)
spawned.append(
gevent.spawn(self.download, item.enclosure, filename,
item))
gevent.joinall(spawned)
def download(self, url, fullpath, item):
try:
df = Download(url, fullpath)
df.download()
except urllib.error.HTTPError:
print("Http Error, skipping")
return False
self.check_id3_edit(item.podcast.id, fullpath, item)
return
def get_fullpath(self, url):
return os.path.join(
self.download_dir,
urllib.parse.unquote(
os.path.basename(urllib.parse.urlparse(url).path)))
def list(self, which):
if which == 'new':
self.print_summary_table()
# for item in EpisodeTable.select().where(EpisodeTable.new):
# self.print_summary(item.summary)
# print("\n")
if which == 'pod':
table_headers = [['id', 'title']]
table_data = []
for item in PodcastTable.select():
table_data.append([str(item.id), item.title])
ascii_table = AsciiTable(table_headers + table_data)
ascii_table.inner_row_border = True
print(ascii_table.table)
def check_id3_edit(self, podcast_id, filename, item):
if str(podcast_id) in list(self.config['id3_edit'].keys()):
id3_config = self.config['id3_edit'][str(podcast_id)]
album = id3_config['album']
artist = id3_config['artist']
if 'title' in list(id3_config.keys()):
if id3_config['title'] == 'copy_item':
title = item.title
else:
title = item.published.strftime('%d/%m-') + album
self.edit_id3(filename, album, artist, title)
else:
self.edit_id3(filename, album, artist)
def edit_id3(self, filename, album, artist, title=None):
try:
audio = easyid3.EasyID3(filename)
except mutagen.id3.ID3NoHeaderError:
audio = mutagen.File(filename, easy=True)
audio.add_tags()
audio["album"] = album
audio["artist"] = artist
audio["genre"] = "Podcast"
if title:
audio["title"] = title
audio.save()
def sync(self, which):
if which == 'new':
print("syncing ...")
for item in EpisodeTable.select().where(EpisodeTable.new):
filename = self.get_fullpath(item.enclosure)
if not os.path.exists(filename):
print("Haven't downloaded %s yet." %
os.path.basename(filename))
continue
self.print_summary_table([item])
if self.config["folder_mode"]:
pod_dir = os.path.join(self.config['sync_to'],
item.podcast.title)
writetopath = os.path.join(pod_dir,
os.path.basename(filename))
if not os.path.exists(pod_dir):
os.mkdir(pod_dir)
else:
writetopath = os.path.join(self.config['sync_to'],
os.path.basename(filename))
shutil.copyfile(filename, writetopath)
item.new = False
item.save()
def delete_podcast(self, podcast_id):
podcast = PodcastTable.select().\
where(PodcastTable.id == podcast_id).get()
podcast.delete_instance(recursive=True)
def delete_old(self, location):
if location == 'local':
self.delete_files_local(self.download_dir)
elif location == 'player':
self.delete_files(self.config['sync_to'])
def delete_files(self, direc, num_days=14):
cur_dir = os.getcwd()
os.chdir(direc)
pod_dirs = os.listdir(direc)
for pod_dir in pod_dirs:
os.chdir(cur_dir)
podcast = PodcastTable.select().\
where(PodcastTable.title == pod_dir).get()
os.chdir(direc)
files = os.listdir(pod_dir)
os.chdir(pod_dir)
for filename in files:
file_age = (datetime.now() -\
datetime.fromtimestamp(os.path.getctime(filename))).days
if str(podcast.id) in self.config["podcast_age"].keys():
conf_age = self.config["podcast_age"][str(podcast.id)]
else:
conf_age = num_days
if file_age > num_days or file_age > conf_age:
print('removing %s' % (str(filename)))
os.remove(filename)
os.chdir(direc)
os.chdir(cur_dir)
def delete_files_local(self, direc, num_days=14):
cur_dir = os.getcwd()
os.chdir(direc)
files = os.listdir(direc)
for filename in files:
if (datetime.now() - datetime.fromtimestamp(
os.path.getctime(filename))).days > num_days:
print('removing %s' % (str(filename)))
os.remove(filename)
os.chdir(cur_dir)
def mark_old(self, days, podcast_id=False):
if podcast_id:
podcast = PodcastTable.select().where(
PodcastTable.id == podcast_id).get()
episodes = EpisodeTable.select().\
where(EpisodeTable.new, EpisodeTable.podcast == podcast)
else:
episodes = EpisodeTable.select().where(EpisodeTable.new)
for item in episodes:
if item.published < datetime.now() - timedelta(days):
print('Marking old: ', item.title)
item.new = False
item.save()
def eject(self):
while subprocess.call(
['diskutil', 'unmount', self.config['eject_point']]):
print("Attempting to eject.")
sleep(5)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--add_podcast", help="add new podcast")
parser.add_argument(
"-r",
"--refresh_all",
help="refresh all podcasts",
nargs='?',
const=True)
parser.add_argument(
"-d",
"--download_all_new",
help="download all new podcasts",
nargs='?',
const=True)
parser.add_argument(
"-l",
"--list",
help=
"with no argument lists all new podcasts, with argument pod it lists all podcasts",
nargs='?',
const='new')
parser.add_argument(
"-s", "--sync", help="sync all new podcasts", nargs='?', const='new')
parser.add_argument(
"--delete", help="delete podcast, must specify podcast id")
parser.add_argument(
"--delete_old",
help=
"delete old episode downloads, defaults to local episodes, other option player",
nargs='?',
const='local')
parser.add_argument(
"--mark_old",
help="mark episodes older than arg as old",
nargs='?',
const='7')
parser.add_argument(
"--mark_old_podcast",
help="specifies mark_old podcast to mark old episodes",
nargs='?',
const=False)
parser.add_argument(
"-e", "--eject", help="eject player", nargs='?', const=True)
args = parser.parse_args()
podcli = PodCli()
if args.add_podcast:
podcli.add_podcast(args.add_podcast)
if args.refresh_all:
podcli.refresh_all()
if args.download_all_new:
podcli.download_all_new()
if args.list:
podcli.list(args.list)
if args.sync:
podcli.sync(args.sync)
if args.delete:
podcli.delete_podcast(args.delete)
if args.delete_old:
podcli.delete_old(args.delete_old)
if args.eject:
podcli.eject()
if args.mark_old:
podcli.mark_old(int(args.mark_old), args.mark_old_podcast)