c76415441c4a8849918ba602931122350cc4ea82
[mygpo.git] / mygpo / data / management / commands / update-related-podcasts.py
blobc76415441c4a8849918ba602931122350cc4ea82
1 from optparse import make_option
2 from operator import itemgetter
4 from django.core.management.base import BaseCommand
6 from mygpo.decorators import repeat_on_conflict
7 from mygpo.core.models import Podcast
8 from mygpo.data.podcast import calc_similar_podcasts
9 from mygpo.utils import progress
10 from mygpo.db.couchdb.podcast import podcast_count, all_podcasts
13 class Command(BaseCommand):
15 option_list = BaseCommand.option_list + (
16 make_option('--max', action='store', type='int', dest='max', default=15, help="Maximum number of similar podcasts to calculate for each podcast."),
20 def handle(self, *args, **options):
22 get_podcast = itemgetter(0)
24 max_related = options.get('max')
26 podcasts = all_podcasts()
27 total = podcast_count()
29 for (n, podcast) in enumerate(podcasts):
31 l = calc_similar_podcasts(podcast)[:max_related]
33 related = map(get_podcast, l)
35 @repeat_on_conflict(['podcast'])
36 def _update(podcast, related):
37 podcast.related_podcasts = related
38 podcast.save()
40 _update(podcast=podcast, related=related)
42 progress(n+1, total)