fix reloading podcasts on update conflict
[mygpo.git] / mygpo / data / management / commands / tag-downloader.py
blobdbeb0002b6424dc1cb494f57f02495bf530b2acd
1 import time
2 import urllib2
3 from optparse import make_option
5 from mygpo.decorators import repeat_on_conflict
6 from mygpo.core.models import Podcast
7 from mygpo.data import delicious
8 from mygpo.maintenance.management.podcastcmd import PodcastCommand
9 from mygpo.db.couchdb.podcast import reload_podcast
12 SOURCE = 'delicious'
14 class Command(PodcastCommand):
15 """
16 Adds tags from the webservice delicious.com to podcasts
18 Podcasts are specified either by URL or the --toplist and --random
19 parameter. The delicious webservice is queried for the podcasts' websites.
20 The returned tags are added to the podcasts for the 'delicious' source.
21 """
23 def handle(self, *args, **options):
25 fetch_queue = self.get_podcasts()
27 for p in fetch_queue:
28 if not p or not p.link:
29 continue
31 # we don't want to spam delicious
32 time.sleep(1)
34 try:
35 f = urllib2.urlopen(p.link)
36 except urllib2.HTTPError:
37 continue
39 tags = delicious.get_tags(f.url)
41 self.update(podcast=p, tags=tags)
44 @repeat_on_conflict(['podcast'], reload_f=reload_podcast)
45 def update(self, podcast, tags):
46 podcast.tags[SOURCE] = tags
47 podcast.save()