Update implementation of management commands
[mygpo.git] / mygpo / maintenance / management / podcastcmd.py
blobf59113204628fac9c0d211d48b94113a7bd2d05d
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 """
14 def add_arguments(self, parser):
15 parser.add_argument('--toplist', action='store_true', dest='toplist',
16 default=False, help="Update all entries from the Toplist."),
18 parser.add_argument('--update-new', action='store_true', dest='new',
19 default=False, help="Update all podcasts with new Episodes"),
21 parser.add_argument('--max', action='store', dest='max', type='int',
22 default=0, help="Set how many feeds should be updated at maximum"),
24 parser.add_argument('--random', action='store_true', dest='random',
25 default=False, help="Update random podcasts, best used with --max"),
27 parser.add_argument('--next', action='store_true', dest='next',
28 default=False, help="Podcasts that are due to be updated next"),
31 def get_podcasts(self, *args, **options):
32 return chain.from_iterable(self._get_podcasts(*args, **options))
35 def _get_podcasts(self, *args, **options):
37 max_podcasts = options.get('max')
39 if options.get('toplist'):
40 yield (p.url for p in self.get_toplist(max_podcasts))
42 if options.get('new'):
43 query = Podcast.objects.filter(episode__title__isnull=True,
44 episode__outdated=False)
45 podcasts = query.distinct('id')[:max_podcasts]
46 random.shuffle(podcasts)
47 yield (p.url for p in podcasts)
49 if options.get('random'):
50 podcasts = random_podcasts()
51 yield (p.url for p in podcasts)
53 if options.get('next'):
54 podcasts = Podcast.objects.all().order_by_next_update()[:max_podcasts]
55 yield (p.url for p in podcasts)
58 if args:
59 yield args
61 if options.get('urls'):
62 yield options.get('urls')
64 if not args and not options.get('toplist') and not options.get('new') \
65 and not options.get('random') and not options.get('next'):
66 query = Podcast.objects.order_by('last_update')
67 podcasts = query.select_related('urls')[:max_podcasts]
68 yield (p.url for p in podcasts)
71 def get_toplist(self, max_podcasts=100):
72 return Podcast.objects.all().toplist()[:max_podcasts]
75 def random_podcasts():
76 while True:
77 yield Podcast.objects.all().random().first()