Use Django's new UUIDField
[mygpo.git] / mygpo / data / tasks.py
blobff1143c1ecd5e8857daea52877dffe2ee94513d5
1 from operator import itemgetter
2 from datetime import datetime, timedelta
4 from celery.decorators import periodic_task
6 from mygpo.data.podcast import calc_similar_podcasts
7 from mygpo.celery import celery
8 from mygpo.podcasts.models import Podcast
10 from celery.utils.log import get_task_logger
11 logger = get_task_logger(__name__)
14 @celery.task
15 def update_podcasts(podcast_urls):
16 """ Task to update a podcast """
17 from mygpo.data.feeddownloader import update_podcasts as update
18 podcasts = update(podcast_urls)
19 return list(podcasts)
22 @celery.task
23 def update_related_podcasts(podcast, max_related=20):
24 get_podcast = itemgetter(0)
26 related = calc_similar_podcasts(podcast)[:max_related]
27 related = map(get_podcast, related)
29 for p in related:
30 podcast.related_podcasts.add(p)
33 # interval in which podcast updates are scheduled
34 UPDATE_INTERVAL = timedelta(hours=1)
37 @periodic_task(run_every=UPDATE_INTERVAL)
38 def schedule_updates(interval=UPDATE_INTERVAL):
39 """ Schedules podcast updates that are due within ``interval`` """
40 now = datetime.utcnow()
42 # fetch podcasts for which an update is due within the next hour
43 podcasts = Podcast.objects.all()\
44 .next_update_between(now, now+interval)\
45 .prefetch_related('urls')\
46 .only('pk')
48 logger.error('Scheduling %d podcasts for update', podcasts.count())
49 # queue all those podcast updates
50 for podcast in podcasts:
51 update_podcasts.delay([podcast.url])