[Migration] handle Episode.listeners = None in episode toplist
[mygpo.git] / mygpo / maintenance / management / podcastcmd.py
blob7369f176f7066d4682f7b5c6f3b5dfbd4c2df93d
1 from itertools import islice, chain, imap as map
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 option_list = BaseCommand.option_list + (
14 make_option('--toplist', action='store_true', dest='toplist',
15 default=False, help="Update all entries from the Toplist."),
17 make_option('--update-new', action='store_true', dest='new',
18 default=False, help="Update all podcasts with new Episodes"),
20 make_option('--max', action='store', dest='max', type='int',
21 default=0, help="Set how many feeds should be updated at maximum"),
23 make_option('--random', action='store_true', dest='random',
24 default=False, help="Update random podcasts, best used with --max"),
26 make_option('--next', action='store_true', dest='next',
27 default=False, help="Podcasts that are due to be updated next"),
32 def get_podcasts(self, *args, **options):
33 return chain.from_iterable(self._get_podcasts(*args, **options))
36 def _get_podcasts(self, *args, **options):
38 max_podcasts = options.get('max')
40 if options.get('toplist'):
41 yield (p.url for p in self.get_toplist(max_podcasts))
43 if options.get('new'):
44 query = Podcast.objects.filter(episode__title__isnull=True,
45 episode__outdated=False)
46 podcasts = query.distinct('id')[:max_podcasts]
47 random.shuffle(podcasts)
48 yield (p.url for p in podcasts)
50 if options.get('random'):
51 podcasts = random_podcasts()
52 yield (p.url for p in podcasts)
54 if options.get('next'):
55 podcasts = Podcast.objects.all().order_by_next_update()[:max_podcasts]
56 yield (p.url for p in podcasts)
59 if args:
60 yield args
62 if not args and not options.get('toplist') and not options.get('new') \
63 and not options.get('random') and not options.get('next'):
64 query = Podcast.objects.order_by('last_update')
65 podcasts = query.select_related('urls')[:max_podcasts]
66 yield (p.url for p in podcasts)
69 def get_toplist(self, max_podcasts=100):
70 return Podcast.objects.all().toplist()[:max_podcasts]
73 def random_podcasts():
74 while True:
75 yield Podcast.objects.all().random().first()