1 from itertools
import islice
, chain
2 from optparse
import make_option
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
):
19 help="Update all entries from the Toplist.",
27 help="Update all podcasts with new Episodes",
36 help="Set how many feeds should be updated at maximum",
44 help="Update random podcasts, best used with --max",
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")
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():
103 yield Podcast
.objects
.all().random().first()