diff options
Diffstat (limited to 'ryuslash/aggregator/management/commands')
-rw-r--r-- | ryuslash/aggregator/management/commands/loadfeeds.py | 53 |
1 files changed, 23 insertions, 30 deletions
diff --git a/ryuslash/aggregator/management/commands/loadfeeds.py b/ryuslash/aggregator/management/commands/loadfeeds.py index 035e2ef..aba82c1 100644 --- a/ryuslash/aggregator/management/commands/loadfeeds.py +++ b/ryuslash/aggregator/management/commands/loadfeeds.py @@ -8,7 +8,7 @@ import urllib2 from django.conf import settings from django.core.management.base import BaseCommand -from aggregator.models import Post +from aggregator.models import Feed, Post class Command(BaseCommand): @@ -18,50 +18,42 @@ class Command(BaseCommand): value = re.sub('[^\w\s-]', '', value).strip().lower() return re.sub('[-\s]+', '-', value) - def get_ext(self, options): - if 'favicon_ext' in options.keys(): - return options['favicon_ext'] - else: - return 'ico' - - def get_logopath(self, feedname, options): - ext = self.get_ext(options) - filename = self.prep_feedname(feedname) + '.' + ext + def get_logopath(self, feed): + ext = feed.favicon_ext + filename = self.prep_feedname(feed.name) + '.' + ext basedir = settings.STATICFILES_DIRS[0] return os.path.join(basedir, 'images/logos', filename) - def have_logo(self, feedname, options): - logopath = self.get_logopath(feedname, options) + def have_logo(self, feed): + logopath = self.get_logopath(feed) return os.path.exists(logopath) - def save_logo(self, feedname, options): - ext = self.get_ext(options) - url = options['base_url'] + '/favicon.' + ext + def save_logo(self, feed): + ext = feed.favicon_ext + url = feed.base_url + '/favicon.' + ext try: logo = urllib2.urlopen(url) except: return - save = open(self.get_logopath(feedname, options), 'w') + save = open(self.get_logopath(feed), 'w') save.write(logo.read()) save.close() logo.close() def construct_feed_url(self, feed): - return feed['base_url'] + feed['feed_url'] + return feed.base_url + feed.feed_url def handle(self, *args, **kwargs): - for feedname, feedoptions in settings.FEEDS.iteritems(): - parsed = \ - feedparser.parse(self.construct_feed_url(feedoptions)) - icon = (self.prep_feedname(feedname) + '.' + - self.get_ext(feedoptions)) + for feed in Feed.objects.all(): + parsed = feedparser.parse(self.construct_feed_url(feed)) + icon = self.prep_feedname(feed.name) + '.' + feed.favicon_ext newcount = 0 - if not self.have_logo(feedname, feedoptions): - self.save_logo(feedname, feedoptions) + if not self.have_logo(feed): + self.save_logo(feed) for entry in parsed.entries: if Post.objects.filter(post_id=entry.id).exists(): @@ -81,23 +73,24 @@ class Command(BaseCommand): else: content = entry.summary - if feedoptions['markdown']: + if feed.uses_markdown: content = markdown.markdown(content) - if feedoptions.get('nl2br'): + if feed.convert_newlines: content = re.sub('\n', '</br>\n', content) post = Post( post_id=entry.id, - title=entry.title if feedoptions.get('title') else '', - category=feedoptions['category'], + title=entry.title if feed.uses_titles else '', + category=feed.category, link=entry.link, updated=updated, icon=icon, - content=content + content=content, + feed=feed ) post.save() newcount += 1 - print 'Grabbed %d new feeds from %s' % (newcount, feedname) + print 'Grabbed %d new posts from %s' % (newcount, feed.name) |