Merge pull request #793 from gpodder/remove-advertise
[mygpo.git] / mygpo / maintenance / management / podcastcmd.py
bloba17dff9365bb6e48e199f850ebad0b094028ae9e
1 from itertools import islice, chain
2 from optparse import make_option
3 import random
5 from django.core.management.base import BaseCommand
7 from mygpo.podcasts.models import Podcast
10 class PodcastCommand(BaseCommand):
11 """command that operates on a list of podcasts specified by parameters"""
13 def add_arguments(self, parser):
14 parser.add_argument(
15 "--toplist",
16 action="store_true",
17 dest="toplist",
18 default=False,
19 help="Update all entries from the Toplist.",
22 parser.add_argument(
23 "--update-new",
24 action="store_true",
25 dest="new",
26 default=False,
27 help="Update all podcasts with new Episodes",
30 parser.add_argument(
31 "--max",
32 action="store",
33 dest="max",
34 type=int,
35 default=0,
36 help="Set how many feeds should be updated at maximum",
39 parser.add_argument(
40 "--random",
41 action="store_true",
42 dest="random",
43 default=False,
44 help="Update random podcasts, best used with --max",
47 parser.add_argument(
48 "--next",
49 action="store_true",
50 dest="next",
51 default=False,
52 help="Podcasts that are due to be updated next",
55 parser.add_argument("urls", nargs="+", type=str)
57 def get_podcasts(self, *args, **options):
58 return chain.from_iterable(self._get_podcasts(*args, **options))
60 def _get_podcasts(self, *args, **options):
62 max_podcasts = options.get("max")
64 if options.get("toplist"):
65 yield (p.url for p in self.get_toplist(max_podcasts))
67 if options.get("new"):
68 query = Podcast.objects.filter(
69 episode__title__isnull=True, episode__outdated=False
71 podcasts = query.distinct("id")[:max_podcasts]
72 random.shuffle(podcasts)
73 yield (p.url for p in podcasts)
75 if options.get("random"):
76 podcasts = random_podcasts()
77 yield (p.url for p in podcasts)
79 if options.get("next"):
80 podcasts = Podcast.objects.all().order_by_next_update()[:max_podcasts]
81 yield (p.url for p in podcasts)
83 if options.get("urls"):
84 yield options.get("urls")
86 if (
87 not options.get("urls")
88 and not options.get("toplist")
89 and not options.get("new")
90 and not options.get("random")
91 and not options.get("next")
93 query = Podcast.objects.order_by("last_update")
94 podcasts = query.select_related("urls")[:max_podcasts]
95 yield (p.url for p in podcasts)
97 def get_toplist(self, max_podcasts=100):
98 return Podcast.objects.all().toplist()[:max_podcasts]
101 def random_podcasts():
102 while True:
103 yield Podcast.objects.all().random().first()