2009-08-14 01:48:21 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
2010-08-16 12:52:39 -04:00
|
|
|
from django.conf import settings
|
2010-12-14 23:10:13 -05:00
|
|
|
from django.contrib.auth.models import User
|
2012-02-24 16:43:08 -08:00
|
|
|
from apps.statistics.models import MStatistics
|
2010-08-16 12:52:39 -04:00
|
|
|
from apps.rss_feeds.models import Feed
|
2021-03-04 10:24:17 -05:00
|
|
|
from apps.reader.models import UserSubscription
|
2010-08-16 12:52:39 -04:00
|
|
|
from optparse import make_option
|
2009-08-29 19:34:42 +00:00
|
|
|
from utils import feed_fetcher
|
2009-08-30 00:43:13 +00:00
|
|
|
from utils.management_functions import daemonize
|
2011-11-27 02:41:12 -05:00
|
|
|
import django
|
2009-08-20 02:43:01 +00:00
|
|
|
import socket
|
2010-04-19 12:09:04 -04:00
|
|
|
import datetime
|
2009-08-14 01:48:21 +00:00
|
|
|
|
2009-08-21 13:14:44 +00:00
|
|
|
|
2009-08-14 01:48:21 +00:00
|
|
|
class Command(BaseCommand):
|
2020-06-08 07:55:17 -04:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument("-f", "--feed", default=None)
|
|
|
|
parser.add_argument("-d", "--daemon", dest="daemonize", action="store_true")
|
|
|
|
parser.add_argument("-F", "--force", dest="force", action="store_true")
|
|
|
|
parser.add_argument("-s", "--single_threaded", dest="single_threaded", action="store_true")
|
|
|
|
parser.add_argument('-t', '--timeout', type=int, default=10,
|
|
|
|
help='Wait timeout in seconds when connecting to feeds.')
|
|
|
|
parser.add_argument('-u', '--username', type=str, dest='username')
|
|
|
|
parser.add_argument('-V', '--verbose', action='store_true',
|
|
|
|
dest='verbose', default=False, help='Verbose output.')
|
|
|
|
parser.add_argument('-S', '--skip', type=int,
|
|
|
|
dest='skip', default=0, help='Skip stories per month < #.')
|
|
|
|
parser.add_argument('-w', '--workerthreads', type=int, default=4,
|
|
|
|
help='Worker threads that will fetch feeds in parallel.')
|
2009-08-14 01:48:21 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
if options['daemonize']:
|
|
|
|
daemonize()
|
2012-02-24 16:43:08 -08:00
|
|
|
|
2010-08-16 12:52:39 -04:00
|
|
|
settings.LOG_TO_STREAM = True
|
2010-10-10 23:55:00 -04:00
|
|
|
now = datetime.datetime.utcnow()
|
2010-08-16 13:21:32 -04:00
|
|
|
|
|
|
|
if options['skip']:
|
|
|
|
feeds = Feed.objects.filter(next_scheduled_update__lte=now,
|
2010-08-18 20:35:45 -04:00
|
|
|
average_stories_per_month__lt=options['skip'],
|
2010-08-18 21:54:33 -04:00
|
|
|
active=True)
|
2020-06-15 02:54:37 -04:00
|
|
|
print(" ---> Skipping %s feeds" % feeds.count())
|
2010-08-16 13:21:32 -04:00
|
|
|
for feed in feeds:
|
|
|
|
feed.set_next_scheduled_update()
|
2020-06-15 02:54:37 -04:00
|
|
|
print('.', end=' ')
|
2010-08-30 13:33:29 -04:00
|
|
|
return
|
2010-08-16 12:52:39 -04:00
|
|
|
|
2010-04-19 12:09:04 -04:00
|
|
|
socket.setdefaulttimeout(options['timeout'])
|
2010-08-21 20:42:38 -04:00
|
|
|
if options['force']:
|
|
|
|
feeds = Feed.objects.all()
|
2010-12-14 23:10:13 -05:00
|
|
|
elif options['username']:
|
2021-03-04 10:24:17 -05:00
|
|
|
usersubs = UserSubscription.objects.filter(user=User.objects.get(username=options['username']), active=True)
|
|
|
|
feeds = Feed.objects.filter(pk__in=usersubs.values('feed_id'))
|
2015-07-22 13:18:54 -07:00
|
|
|
elif options['feed']:
|
|
|
|
feeds = Feed.objects.filter(pk=options['feed'])
|
2010-12-14 23:10:13 -05:00
|
|
|
else:
|
|
|
|
feeds = Feed.objects.filter(next_scheduled_update__lte=now, active=True)
|
|
|
|
|
|
|
|
feeds = feeds.order_by('?')
|
|
|
|
|
2010-12-23 13:29:31 -05:00
|
|
|
for f in feeds:
|
|
|
|
f.set_next_scheduled_update()
|
2010-09-08 18:30:46 -07:00
|
|
|
|
2009-09-16 03:54:33 +00:00
|
|
|
num_workers = min(len(feeds), options['workerthreads'])
|
2010-04-09 16:37:19 -04:00
|
|
|
if options['single_threaded']:
|
|
|
|
num_workers = 1
|
2009-08-21 13:14:44 +00:00
|
|
|
|
2010-11-10 18:04:17 -05:00
|
|
|
options['compute_scores'] = True
|
2012-03-19 15:46:59 -07:00
|
|
|
options['quick'] = float(MStatistics.get('quick_fetch', 0))
|
2014-05-29 17:53:16 -07:00
|
|
|
options['updates_off'] = MStatistics.get('updates_off', False)
|
2010-11-10 18:04:17 -05:00
|
|
|
|
2009-09-16 03:54:33 +00:00
|
|
|
disp = feed_fetcher.Dispatcher(options, num_workers)
|
2009-08-20 02:43:01 +00:00
|
|
|
|
2009-09-16 03:54:33 +00:00
|
|
|
feeds_queue = []
|
|
|
|
for _ in range(num_workers):
|
|
|
|
feeds_queue.append([])
|
2011-11-27 02:41:12 -05:00
|
|
|
|
2009-09-16 03:54:33 +00:00
|
|
|
i = 0
|
2009-08-14 01:48:21 +00:00
|
|
|
for feed in feeds:
|
2010-10-03 18:04:40 -04:00
|
|
|
feeds_queue[i%num_workers].append(feed.pk)
|
2009-09-16 03:54:33 +00:00
|
|
|
i += 1
|
2010-04-23 21:19:19 -04:00
|
|
|
disp.add_jobs(feeds_queue, i)
|
2009-08-20 02:43:01 +00:00
|
|
|
|
2011-11-27 02:41:12 -05:00
|
|
|
django.db.connection.close()
|
|
|
|
|
2020-06-15 02:54:37 -04:00
|
|
|
print(" ---> Fetching %s feeds..." % feeds.count())
|
2009-09-10 03:48:22 +00:00
|
|
|
disp.run_jobs()
|