[Migration] remove/rewrite imports of CouchDB models
[mygpo.git] / mygpo / data / management / commands / tag-downloader.py
blobb55dbe092827d88dddc0e94ad489f7cdc7e4408e
1 import time
2 import urllib2
3 from optparse import make_option
5 from mygpo.decorators import repeat_on_conflict
6 from mygpo.data import delicious
7 from mygpo.maintenance.management.podcastcmd import PodcastCommand
10 SOURCE = 'delicious'
12 class Command(PodcastCommand):
13 """
14 Adds tags from the webservice delicious.com to podcasts
16 Podcasts are specified either by URL or the --toplist and --random
17 parameter. The delicious webservice is queried for the podcasts' websites.
18 The returned tags are added to the podcasts for the 'delicious' source.
19 """
21 def handle(self, *args, **options):
23 fetch_queue = self.get_podcasts()
25 for p in fetch_queue:
26 if not p or not p.link:
27 continue
29 # we don't want to spam delicious
30 time.sleep(1)
32 try:
33 f = urllib2.urlopen(p.link)
34 except urllib2.HTTPError:
35 continue
37 tags = delicious.get_tags(f.url)
39 self.update(podcast=p, tags=tags)
42 def update(self, podcast, tags):
43 podcast.tags[SOURCE] = tags
44 podcast.save()