-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgpo
More file actions
executable file
·1241 lines (954 loc) · 39.5 KB
/
gpo
File metadata and controls
executable file
·1241 lines (954 loc) · 39.5 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#
# gPodder: Media and podcast aggregator
# Copyright (c) 2005-2020 Thomas Perl and the gPodder Team
#
# gPodder is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# gPodder is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# gpo - A better command-line interface to gPodder using the gPodder API
# by Thomas Perl <thp@gpodder.org>; 2009-05-07
"""
Usage: gpo [--verbose|-v] [--batch|-b] [--help|-h] [COMMAND] [params...]
- Subscription management -
subscribe URL [TITLE] Subscribe to a new feed at URL (as TITLE)
rename URL TITLE Rename feed at URL to TITLE
rewrite OLDURL NEWURL Change the feed URL of [OLDURL] to [NEWURL]
unsubscribe URL Unsubscribe from feed at URL
import FILENAME|URL Subscribe to all podcasts in an OPML file
export FILENAME Export all subscriptions to an OPML file
info URL Show information about feed at URL
enable URL Enable feed updates for the feed at URL
disable URL Disable feed updates for the feed at URL
list List all subscribed podcasts
update [URL] Check for new episodes (all or only at URL)
- Episode management -
download [URL] Download new episodes (all or only from URL)
pending [URL] List new episodes (all or only from URL)
episodes [URL] List episodes (all or only from URL)
query EQL Query episodes based on an EQL expression
The commands "pending", "episodes" and "query" show an alphanumeric ID of
the episode that can be used with the following commands:
mark old|new EPISODE Mark episode as old or new (new = pending)
fetch EPISODE Download a single episode now
rm EPISODE Delete episode
details EPISODE Display episode details and shownotes
apply COMMAND Apply COMMAND (mark old|new, fetch, rm,
details) to episodes matched in the last
query command
- Podcast directory -
search QUERY Search for podcasts on gpodder.net
directory Show all static podcast directory results
- Configuration -
set [key] [value] List one (all) keys or set to a new value
- Other commands -
run Update and download episodes (useful with -b)
versioncheck Check if a new gPodder version is available
license Show the software license information
registry Print registered resolvers
shortcuts List available URL shortcuts
"""
import sys
import collections
import os
import re
import inspect
import functools
import itertools
try:
import readline
except ImportError:
readline = None
import shlex
import pydoc
import gettext
import logging
import subprocess
try:
import termios
import fcntl
import struct
except ImportError:
termios = None
fcntl = None
struct = None
# A poor man's argparse/getopt - but it works for our use case :)
class Flags:
verbose = ('-v', '--verbose')
batch = ('-b', '--batch')
@classmethod
def _parse(cls, argv):
for key in vars(cls):
if key.startswith('_'):
continue
setattr(cls, key, sum(cls._eat_argv(argv, getattr(cls, key))))
@classmethod
def _eat_argv(cls, args, candidates):
for candidate in candidates:
while candidate in args:
args.remove(candidate)
yield 1
Flags._parse(sys.argv)
gpodder_script = sys.argv[0]
if os.path.islink(gpodder_script):
gpodder_script = os.readlink(gpodder_script)
gpodder_dir = os.path.join(os.path.dirname(gpodder_script), '..')
prefix = os.path.abspath(os.path.normpath(gpodder_dir))
src_dir = os.path.join(prefix, 'src')
if os.path.exists(os.path.join(src_dir, 'gpodder', '__init__.py')):
# Run gPodder from local source folder (not installed)
sys.path.insert(0, src_dir)
# i18n setup
translation = gettext.translation('gpodder', fallback=True)
_ = translation.gettext
N_ = translation.ngettext
have_ansi = sys.stdout.isatty() and not Flags.batch
interactive_console = sys.stdin.isatty() and sys.stdout.isatty() and not Flags.batch
is_single_command = False
import gpodder
from gpodder import core
from gpodder import opml
from gpodder import util
from gpodder import query
from gpodder import registry
from gpodder import directory
from gpodder.config import config_value_to_string
def incolor(color_id, s):
if have_ansi and cli._config.ui.cli.colors:
return '\033[9%dm%s\033[0m' % (color_id, s)
return s
# ANSI Colors: red = 1, green = 2, yellow = 3, blue = 4
inred, ingreen, inyellow, inblue = (functools.partial(incolor, x) for x in range(1, 5))
LOG_LEVEL_PRINT = 60
logging.addLevelName(LOG_LEVEL_PRINT, 'OUT')
def out(*args, **kwargs):
if Flags.batch:
sep = kwargs.get('sep', ' ')
message = sep.join(a.strip() for a in args)
if message:
for line in message.split('\n'):
logger.log(LOG_LEVEL_PRINT, line)
else:
print(*args, **kwargs)
def FirstArgumentIsPodcastURL(function):
"""Decorator for functions that take a podcast URL as first arg"""
setattr(function, '_first_arg_is_podcast', True)
return function
def get_terminal_size():
if None in (termios, fcntl, struct):
return (80, 24)
s = struct.pack('HHHH', 0, 0, 0, 0)
stdout = sys.stdout.fileno()
x = fcntl.ioctl(stdout, termios.TIOCGWINSZ, s)
rows, cols, xp, yp = struct.unpack('HHHH', x)
return rows, cols
class gPodderCli(object):
COLUMNS = 80
EXIT_COMMANDS = ('quit', 'exit', 'bye')
def __init__(self, verbose):
self.core = core.Core(verbose=verbose, stdout=Flags.batch)
self._db = self.core.db
self._config = self.core.config
self._model = self.core.model
self._current_action = ''
self._current_progress = -1
self._commands = dict((name.rstrip('_'), func) for name, func in inspect.getmembers(self)
if inspect.ismethod(func) and not name.startswith('_'))
self._prefixes, self._expansions = self._build_prefixes_expansions()
self._prefixes.update({'?': 'help'})
self._valid_commands = sorted(self._prefixes.values())
self._last_query_match = []
def _build_prefixes_expansions(self):
prefixes = {}
expansions = collections.defaultdict(list)
names = sorted(self._commands.keys())
names.extend(self.EXIT_COMMANDS)
# Generator for all prefixes of a given string (longest first)
# e.g. ['gpodder', 'gpodde', 'gpodd', 'gpod', 'gpo', 'gp', 'g']
mkprefixes = lambda n: (n[:x] for x in range(len(n), 0, -1))
# Return True if the given prefix is unique in "names"
is_unique = lambda p: len([n for n in names if n.startswith(p)]) == 1
for name in names:
is_still_unique = True
unique_expansion = None
for prefix in mkprefixes(name):
if is_unique(prefix):
unique_expansion = '[%s]%s' % (prefix, name[len(prefix):])
prefixes[prefix] = name
continue
if unique_expansion is not None:
expansions[prefix].append(unique_expansion)
continue
return prefixes, expansions
def _start_action(self, msg, args):
line = msg % args
if len(line) > self.COLUMNS-7:
line = line[:self.COLUMNS-7-3] + '...'
else:
line = line + (' '*(self.COLUMNS-7-len(line)))
self._current_action = line
self._current_progress = -1
if not Flags.batch:
out(self._current_action, '[....]', end='')
self._update_action()
def _update_action(self, progress=None):
if Flags.batch:
# Don't show progress when in batch mode
return
if progress is None:
if self._current_progress == -1:
# No need to update the status message
return
self._current_progress = -1
result = '[....]'
else:
new_progress = int(progress * 100)
if new_progress == self._current_progress:
# No need to update the status message
return
self._current_progress = new_progress
progress = '%3d%%' % (self._current_progress,)
result = '['+inblue(progress)+']'
out(('' if Flags.batch else '\r') + self._current_action, result, end='')
def _finish_action(self, success=True, skip=False):
if skip:
result = '['+inyellow('SKIP')+']'
elif success:
result = '['+ingreen('DONE')+']'
else:
result = '['+inred('FAIL')+']'
out(('' if Flags.batch else '\r') + self._current_action, result)
self._current_action = ''
def _atexit(self):
self.core.shutdown()
# -------------------------------------------------------------------
def import_(self, url):
"""Import subscriptions from an OPML file
import http://example.com/subscriptions.opml
Import subscriptions from the given URL
import ./feeds.opml
Import subscriptions from a local file
"""
for channel in opml.Importer(url).items:
self.subscribe(channel['url'], channel.get('title'))
def export(self, filename):
"""Export subscriptions to an OPML file
export ./subscriptions.opml
Export the subscriptinos to a local file
"""
podcasts = self._model.get_podcasts()
opml.Exporter(filename).write(podcasts)
def _get_podcast(self, url, create=False, check_only=False):
"""Get a specific podcast by URL
Returns a podcast object for the URL or None if
the podcast has not been subscribed to.
"""
url = self.core.model.normalize_feed_url(url)
if url is None:
self._error(_('Invalid URL: %(url)s') % {'url': url})
return None
# Subscribe to new podcast
if create:
return self._model.load_podcast(url, create=True)
# Load existing podcast
for podcast in self._model.get_podcasts():
if podcast.url == url:
return podcast
if not check_only:
self._error(_('Not subscribed to %(url)s') % {'url': url})
return None
def subscribe(self, url, title=None):
"""Subscribe to a new podcast via a URL
subscribe http://example.org/feed.rss
Subscribe to the feed at the given URL
Use {shortcuts} for a list of shortcut URLs you can use here.
"""
self._start_action(_('Subscribing to %(url)s'), {'url': url})
existing = self._get_podcast(url, check_only=True)
if existing is not None:
self._finish_action(skip=True)
self._error(_('Already subscribed to %(url)s') % {'url': existing.url})
return True
try:
podcast = self._get_podcast(url, create=True)
if podcast is None:
self._error(_('Subscription to %(url)s failed') % {'url': url})
return True
if title is not None:
podcast.rename(title)
podcast.save()
self._finish_action()
if podcast.url != url:
self._info(_('Resolved feed URL: %(url)s') % {'url': ingreen(podcast.url)})
return True
except Exception as e:
self._finish_action(success=False)
logger.warn('Cannot subscribe: %s', e, exc_info=True)
self._error(getattr(e, 'strerror', str(e)))
return True
def _print_config(self, search_for):
for key in sorted(self._config.all_keys()):
if search_for is None or search_for.lower() in key.lower():
value = config_value_to_string(self._config._lookup(key))
out(key, '=', value)
def set(self, key=None, value=None):
"""Get and set configuration values
set
List all configuration options and their values
set a b
Set configuration option "a" to value "b"
"""
if value is None:
self._print_config(key)
return
try:
current_value = self._config._lookup(key)
current_type = type(current_value)
except KeyError:
self._error(_('Configuration option %(key)s not found') % {'key': key})
return
if current_type == dict:
self._error(_('Invalid configuration option: %(key)s') % {'key': key})
return
self._config.update_field(key, value)
self.set(key)
@FirstArgumentIsPodcastURL
def rename(self, url, title):
"""Rename an existing subscription
rename http://example.org/feed.xml "Some Podcast"
Rename the subscription denoted by the URL to "Some Podcast"
"""
podcast = self._get_podcast(url)
if podcast is not None:
old_title = podcast.title
podcast.rename(title)
self._info(old_title, '=>', title)
return True
@FirstArgumentIsPodcastURL
def unsubscribe(self, url):
"""Remove a subscription and all downloaded episodes
unsubscribe http://example.org/episodes.rss
Remove subscription and all downloads for the given URL
"""
podcast = self._get_podcast(url)
if podcast is not None:
podcast.unsubscribe()
self._error(_('Removed %(url)s') % {'url': url})
return True
def _is_episode_new(self, episode):
return (episode.state == gpodder.STATE_NORMAL and episode.is_new)
def _format_episode(self, episode):
status_str = ' '
if self._is_episode_new(episode):
# is new
status_str = 'NEW'
elif (episode.state == gpodder.STATE_DOWNLOADED):
# is downloaded
status_str = 'DL '
elif (episode.state == gpodder.STATE_DELETED):
# is deleted
status_str = 'DEL'
return '[%04x] %s %s (%s)' % (episode.id, status_str, episode.title, self._format_filesize(episode.file_size))
def _episodesList(self, podcast):
return (self._format_episode(e) for e in podcast.episodes)
@FirstArgumentIsPodcastURL
def info(self, url):
"""Get information about a podcast subscription
info http://example.org/show.rss
List metadata and episodes for the given podcast
"""
podcast = self._get_podcast(url)
if podcast is not None:
def feed_update_status_msg(podcast):
return ('disabled' if podcast.pause_subscription else 'enabled')
title, url, status = podcast.title, podcast.url, \
feed_update_status_msg(podcast)
strategy = 'episodic (newest first)' if podcast.download_strategy != podcast.STRATEGY_CHRONO else 'serial (oldest first)'
episodes = self._episodesList(podcast)
episodes = '\n '.join(episodes)
self._pager("""
Title: %(title)s
URL: %(url)s
Feed update is %(status)s
Feed Order: %(strategy)s
Episodes:
%(episodes)s
""" % locals())
return True
@FirstArgumentIsPodcastURL
def episodes(self, url=None):
"""List episodes (all or for a given subscription)
episodes
List episodes from all subscriptions
episodes http://example.net/podcast.xml
List episodes from the given subscription only
"""
output = []
for podcast in self._get_podcasts(url):
podcast_printed = False
episodes = self._episodesList(podcast)
episodes = '\n '.join(episodes)
output.append("""
Episodes from %s:
%s
""" % (podcast.url, episodes))
self._pager('\n'.join(output))
return True
def query(self, *args):
"""Query all episodes using an EQL expression
query video and not downloaded and mb < 10 and since < 7
Find all episodes that are videos, not yet downloaded, smaller than
10 MB and released in the last 7 days.
Use {apply} after this command to apply an action to the query results.
"""
self._last_query_match = []
if not args:
self._error(_('Usage: query EQL'))
return False
query_string = ' '.join(args)
eql = query.EQL(query_string)
output = []
count = 0
for podcast in self._model.get_podcasts():
have_header = False
for episode in podcast.episodes:
if eql.match(episode):
if not have_header:
output.append(self._format_podcast(podcast))
have_header = True
output.append(self._format_episode(episode))
count += 1
self._last_query_match.append(episode.id)
output.append(N_('%(count)d episode matched', '%(count)d episodes matched', count) % {'count': count})
if count:
output.append(inblue(_('Use "apply" to apply a single-episode command')))
out('\n'.join(output))
return True
def _format_podcast(self, podcast):
strategy = '' if podcast.download_strategy != podcast.STRATEGY_CHRONO else '(serial)'
if not podcast.pause_subscription:
return ' '.join(('#', ingreen(podcast.title)))
return ' '.join(('#', inred(podcast.title), '-', _('Subscription suspended')))
def list(self):
"""List all podcast subscriptions
list
Lists all subscribed podcasts (title and URL)
"""
for podcast in self._model.get_podcasts():
out(self._format_podcast(podcast))
out(podcast.url)
return True
def _update_podcast(self, podcast):
if not Flags.batch:
self._start_action('%s', podcast.title)
try:
podcast.update()
if not Flags.batch:
self._finish_action()
except Exception as e:
if Flags.batch:
self._start_action('%s', podcast.title)
self._finish_action(False)
def _pending_message(self, count):
if Flags.batch and count == 0:
# Don't announce non-actions in batch mode
return ''
return N_('%(count)d new episode', '%(count)d new episodes', count) % {'count': count}
def run(self):
"""Check for new episodes, then download them
run
Run "update" and then "download" in sequence
"""
self.update()
self.download()
@FirstArgumentIsPodcastURL
def update(self, url=None):
"""Check for new episodes
update
Check for new episodes for all subscriptions
update http://example.org/feed.atom
Check for new episodes in the given subscription only
"""
count = 0
if not Flags.batch:
out(_('Checking for new episodes'))
for podcast in self._get_podcasts(url):
if not podcast.pause_subscription:
self._update_podcast(podcast)
count += sum(1 for e in podcast.episodes if self._is_episode_new(e))
elif not Flags.batch:
self._start_action('%s', podcast.title)
self._finish_action(skip=True)
out(inblue(self._pending_message(count)))
return True
def _get_podcasts(self, url):
""" get either all podcasts or the one with the given URL """
for podcast in self._model.get_podcasts():
if url is None or podcast.url == url:
yield podcast
@FirstArgumentIsPodcastURL
def pending(self, url=None):
"""List new episodes
pending
List new episodes from all subscriptions
pending http://example.org/episodes.rss
List new episodes from the given subscription only
Use {download} to download all pending episodes.
"""
count = 0
for podcast in self._get_podcasts(url):
podcast_printed = False
for episode in podcast.episodes:
if self._is_episode_new(episode):
if not podcast_printed:
out('#', ingreen(podcast.title))
podcast_printed = True
out(' ', '[%04x]' % episode.id, episode.title)
count += 1
out(inblue(self._pending_message(count)))
return True
def _lookup_episode(self, episode_id):
try:
episode_id = int(episode_id, 16)
except Exception as e:
self._error(_('Invalid episode ID: %(error)s') % {'error': str(e)})
return None
for podcast in self._model.get_podcasts():
for episode in podcast.episodes:
if episode.id == episode_id:
return episode
self._error(_('Episode ID not found: %(id)x') % {'id': episode_id})
return None
def mark(self, action, episode_id):
"""Mark an episode as old or new
mark 00ab new
Mark episode with ID 00ab as new
mark af02 old
Mark episode with ID af02 as old
Use {episodes} or {query} to find episodes you can mark as new. Can be
used in combination with {apply} to mark multiple episodes as old/new.
"""
VALID_ACTIONS = ('new', 'old')
if action not in VALID_ACTIONS:
self._error(_('Invalid action: %(action)s. Valid actions: %(valid_actions)s') % {
'action': action, 'valid_actions': ', '.join(VALID_ACTIONS)})
return False
episode = self._lookup_episode(episode_id)
if episode is None:
return False
if action == 'old':
self._info(_('Episode marked as old: %(title)s') % {'title': ingreen(episode.title)})
episode.is_new = False
elif action == 'new':
self._info(_('Episode marked as new: %(title)s') % {'title': ingreen(episode.title)})
episode.is_new = True
if episode.is_new and episode.state == gpodder.STATE_DELETED:
episode.state = gpodder.STATE_NORMAL
episode.save()
return True
def fetch(self, episode_id):
"""Download an episode
fetch 0124
Download episode with ID 0124
Use {episodes} or {query} to find episode IDs required for this command.
Can be used in combination with {apply} to download multiple episodes.
"""
episode = self._lookup_episode(episode_id)
if episode is None:
return False
if episode.state == gpodder.STATE_DOWNLOADED:
self._error(_('Episode already downloaded'))
return False
self._download_episode(episode)
self._download_summary(1)
return True
def rm(self, episode_id):
"""Delete an episode
rm 0fa1
Delete episode with ID 0fa1
Use {episodes} or {query} to find episode IDs required for this command.
Can be used in combination with {apply} to delete multiple episodes.
"""
episode = self._lookup_episode(episode_id)
if episode is None:
return False
out(inred(_('Episode deleted: %(episode)s') % {'episode': episode.title}))
episode.delete_download()
return True
def _format_filesize(self, num):
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.1f %s" % (num, x)
num /= 1024.0
def details(self, episode_id):
"""Show details about an episode
details 00f3
Show details for episode 00f3
Use {episodes} or {query} to find episode IDs required for this command.
Can be used in combination with {apply} to show multiple episodes's details.
"""
episode = self._lookup_episode(episode_id)
if episode is None:
return False
out('[%04x]' % episode.id, inblue(episode.title))
out('from', ingreen(episode.podcast.title))
out('Size:', self._format_filesize(episode.file_size))
out('='*len(episode.title))
out(episode.description)
if episode.chapters:
out('Chapters:')
for chapter in episode.chapters:
out(' %s: %s' % (util.format_time(chapter['start']), chapter['title']))
out('')
return True
def apply(self, *args):
"""Apply a command to the last query result list
Must be used after {query}.
apply mark old
Mark all episodes in the last query result as old
apply fetch
Download all episodes found in the last query
Commands that can be used with {apply} are:
- fetch
- mark new
- mark old
- rm
- details
"""
if is_single_command:
self._error(_('"apply" can only be used during an interactive session'))
return False
if not self._last_query_match:
self._error(_('Empty query result (use "query" first)'))
return False
if args not in (('fetch',), ('mark', 'new'), ('mark', 'old'), ('rm',), ('details',)):
self._error(_('Cannot apply this command'))
return False
for episode_id in self._last_query_match:
cmd = list(args) + ['%x' % episode_id]
self._parse(cmd)
def search(self, *query):
""" search for podcasts """
if not query:
self._error(_('Please provide a search query'))
return
query = ' '.join(query)
def is_search_provider(provider):
return provider.kind == directory.Provider.PROVIDER_SEARCH
self._format_directory_results(registry.directory.select(is_search_provider),
lambda provider: provider.on_search(query))
def directory(self):
def is_static_provider(provider):
return provider.kind == directory.Provider.PROVIDER_STATIC
self._format_directory_results(registry.directory.select(is_static_provider),
lambda provider: provider.on_static())
def _format_directory_results(self, providers, get_entries_func):
result = []
for provider in providers:
result.extend([ingreen('==== {} ===='.format(provider.name)), ''])
def _format_result(entry):
if entry.subscribers > 0:
title = '{} ({} subscriber(s))'.format(entry.title, entry.subscribers)
else:
title = entry.title
return '\n'.join([title, inblue(entry.url), ''])
result.extend(_format_result(entry) for entry in get_entries_func(provider))
result.append('')
self._pager('\n'.join(result))
def _download_summary(self, count):
if Flags.batch and count == 0:
# Don't announce non-actions in batch mode
return
self._info(N_('%(count)d episode downloaded', '%(count)d episodes downloaded', count) % {'count': count})
def _download_episode(self, episode):
self._start_action(_('Downloading %(episode)s'), {'episode': episode.title})
assert episode.state != gpodder.STATE_DOWNLOADED
episode.download(self._update_action)
self._finish_action()
@FirstArgumentIsPodcastURL
def download(self, url=None):
"""Download all pending episodes
download
Download all pending episodes for all subscriptions
download http://example.com/podcast/episodes.xml
Download all pending episodes for the given subscription
Use {pending} to see a list of episodes that would be downloaded.
"""
count = 0
for podcast in self._get_podcasts(url):
podcast_printed = False
for episode in podcast.episodes:
if self._is_episode_new(episode):
if not podcast_printed:
out(inblue(podcast.title))
podcast_printed = True
self._download_episode(episode)
count += 1
self._download_summary(count)
return True
@FirstArgumentIsPodcastURL
def disable(self, url):
"""Pause subscription of a given feed
disable http://example.org/feed.rss
Disable feed updates for the given subscription.
Disabled subscriptions will not be included in checks for new episodes
with {update}. Use {enable} to re-enable subscriptions. Disabling of a
feed is useful if you want to keep downloaded episodes, but do not want
to check for new episodes. Use {unsubscribe} if you want to remove the
subscription and delete downloaded episodes.
"""
podcast = self._get_podcast(url)
if podcast is not None:
if not podcast.pause_subscription:
podcast.pause_subscription = True
podcast.save()
self._error(_('Subscription suspended: %(url)s') % {'url': url})
return True
@FirstArgumentIsPodcastURL
def enable(self, url):
"""Resume subscription of a given feed
enable http://example.net/podcast/latest.atom
Enable feed updates for the given subscription.
Use {disable} to disable subscriptions. Use {unsubscribe} if you want
to remove the subscription and delete downloaded episodes.
"""
podcast = self._get_podcast(url)
if podcast is not None:
if podcast.pause_subscription:
podcast.pause_subscription = False
podcast.save()
self._error(_('Subscription resumed: %(url)s') % {'url': url})
return True
def versioncheck(self):
"""Check if a new version of gPodder is available
versioncheck
Contact the gPodder server to check if there's a new version
"""
up_to_date, latest_version, releasedate, __ = util.get_update_info()
info = {'latestversion': ingreen(latest_version),
'latestdate': ingreen(releasedate),
'url': inblue(gpodder.__url__),
'thisversion': inred(gpodder.__version__),
'thisdate': inred(gpodder.__date__)}
if up_to_date:
self._info(_('No software updates available'))
else:
self._info(_('New version %(latestversion)s available (released: %(latestdate)s)') % info)
self._info(_('You have version %(thisversion)s (released: %(thisdate)s)') % info)
self._info(_('Download the new version from %(url)s') % info)
def registry(self):
"""Print all registered providers
registry
Print a list of provider types and providers
This is mostly useful for debugging and during development. Providers are
installed into the registry by plugins to support parsing, displaying
and handling of special content feeds that are not purely podcasts.
"""
registry.dump()
return True
def shortcuts(self):
"""List all URL shortcuts that can be used for feed URLs