remove unnecessary imports
[mygpo.git] / mygpo / data / management / commands / tag-downloader.py
blobfd0ef6a6d7c58003c2dd2dfbd4a0664ab34178c7
1 from django.core.management.base import BaseCommand
2 from mygpo.api import models
3 from mygpo.data.models import PodcastTag
4 from mygpo.data import delicious
5 from optparse import make_option
6 import time
7 import urllib2
9 class Command(BaseCommand):
11 option_list = BaseCommand.option_list + (
12 make_option('--toplist', action='store_true', dest='toplist', default=False, help="Update all entries from the Toplist."),
13 make_option('--max', action='store', dest='max', type='int', default=-1, help="Set how many feeds should be updated at maximum"),
15 make_option('--random', action='store_true', dest='random', default=False, help="Update random podcasts, best used with --max option"),
19 def handle(self, *args, **options):
21 fetch_queue = []
23 if options.get('toplist'):
24 for e in models.ToplistEntry.objects.all().order_by('-subscriptions')[:100]:
25 fetch_queue.append(e.podcast)
27 if options.get('random'):
28 fetch_queue = models.Podcast.objects.all().order_by('?')
30 for url in args:
31 try:
32 fetch_queue.append(models.Podcast.objects.get(url=url))
33 except:
34 pass
36 max = options.get('max', -1)
37 if max > 0:
38 fetch_queue = fetch_queue[:max]
40 for p in fetch_queue:
41 if not p or not p.link:
42 continue
44 # we don't want to spam delicious
45 time.sleep(1)
47 try:
48 f = urllib2.urlopen(p.link)
49 except:
50 continue
52 tags = delicious.get_tags(f.url)
54 for tag, count in tags.iteritems():
55 try:
56 print ' %s' % tag.decode('utf-8')
57 except:
58 pass
59 PodcastTag.objects.filter(tag=tag, podcast=p, source='delicious').delete()
60 PodcastTag.objects.create(tag=tag, podcast=p, source='delicious', weight=count)