Moving story counter out of the feed fetcher. Will run nightly.

This commit is contained in:
Samuel Clay 2010-04-19 12:18:40 -04:00
parent 71b746a48d
commit c1c36ad182
2 changed files with 42 additions and 6 deletions

View file

@ -0,0 +1,40 @@
from django.core.management.base import BaseCommand
from django.core.handlers.wsgi import WSGIHandler
from apps.rss_feeds.models import Feed, Story
from django.core.cache import cache
from django.db.models import Q
from optparse import OptionParser, make_option
from utils.management_functions import daemonize
import os
import logging
import errno
import datetime
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option("-f", "--feed", dest="feed", default=None),
make_option("-t", "--title", dest="title", default=None),
make_option("-d", "--daemon", dest="daemonize", action="store_true"),
)
def handle(self, *args, **options):
if options['daemonize']:
daemonize()
if options['title']:
feeds = Feed.objects.filter(feed_title__icontains=options['title'])
elif options['feed']:
feeds = Feed.objects.filter(pk=options['feed'])
else:
feeds = Feed.objects.all()
# Count stories in past month to calculate next scheduled update
for feed in feeds:
month_ago = datetime.datetime.now() - datetime.timedelta(days=30)
stories_count = Story.objects.filter(story_feed=feed, story_date__gte=month_ago).count()
stories_count = stories_count
feed.stories_per_month = stories_count
feed.save()
print " ---> %s [%s]: %s stories" % (feed.feed_title, feed.pk, feed.stories_per_month)
print "\nCounted %s feeds" % feeds.count()

View file

@ -109,12 +109,8 @@ class ProcessFeed:
logging.debug(u'[%d] Processing %s' % (self.feed.id,
self.feed.feed_title))
# Count stories in past month to calculate next scheduled update
month_ago = datetime.datetime.now() - datetime.timedelta(days=30)
stories_count = Story.objects.filter(story_feed=self.feed, story_date__gte=month_ago).count()
stories_count = stories_count
self.feed.stories_per_month = stories_count
updates_per_day = max(30, stories_count) / 30.0 * 12
# Use stories per month to caluclate next feed update
updates_per_day = max(30, self.feed.stories_per_month) / 30.0 * 12
minutes_to_next_update = 60 * 24 / updates_per_day
random_factor = random.randint(0,int(minutes_to_next_update/4))
next_scheduled_update = datetime.datetime.now() + datetime.timedelta(