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")
|
2024-04-24 09:43:56 -04:00
|
|
|
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):
|
2024-04-24 09:43:56 -04:00
|
|
|
if options["daemonize"]:
|
2009-08-14 01:48:21 +00:00
|
|
|
daemonize()
|
2024-04-24 09:43:56 -04: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()
|
2024-04-24 09:43:56 -04:00
|
|
|
|
|
|
|
if options["skip"]:
|
|
|
|
feeds = Feed.objects.filter(
|
|
|
|
next_scheduled_update__lte=now, average_stories_per_month__lt=options["skip"], 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()
|
2024-04-24 09:43:56 -04:00
|
|
|
print(".", end=" ")
|
2010-08-30 13:33:29 -04:00
|
|
|
return
|
2024-04-24 09:43:56 -04:00
|
|
|
|
|
|
|
socket.setdefaulttimeout(options["timeout"])
|
|
|
|
if options["force"]:
|
2010-08-21 20:42:38 -04:00
|
|
|
feeds = Feed.objects.all()
|
2024-04-24 09:43:56 -04:00
|
|
|
elif options["username"]:
|
|
|
|
usersubs = UserSubscription.objects.filter(
|
|
|
|
user=User.objects.get(username=options["username"]), active=True
|
|
|
|
)
|
|
|
|
feeds = Feed.objects.filter(pk__in=usersubs.values("feed_id"))
|
|
|
|
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)
|
2024-04-24 09:43:56 -04:00
|
|
|
|
|
|
|
feeds = feeds.order_by("?")
|
|
|
|
|
2010-12-23 13:29:31 -05:00
|
|
|
for f in feeds:
|
|
|
|
f.set_next_scheduled_update()
|
2024-04-24 09:43:56 -04:00
|
|
|
|
|
|
|
num_workers = min(len(feeds), options["workerthreads"])
|
|
|
|
if options["single_threaded"]:
|
2010-04-09 16:37:19 -04:00
|
|
|
num_workers = 1
|
2024-04-24 09:43:56 -04:00
|
|
|
|
|
|
|
options["compute_scores"] = True
|
|
|
|
options["quick"] = float(MStatistics.get("quick_fetch", 0))
|
|
|
|
options["updates_off"] = MStatistics.get("updates_off", False)
|
|
|
|
|
|
|
|
disp = feed_fetcher.Dispatcher(options, num_workers)
|
|
|
|
|
2009-09-16 03:54:33 +00:00
|
|
|
feeds_queue = []
|
|
|
|
for _ in range(num_workers):
|
|
|
|
feeds_queue.append([])
|
2024-04-24 09:43:56 -04:00
|
|
|
|
2009-09-16 03:54:33 +00:00
|
|
|
i = 0
|
2009-08-14 01:48:21 +00:00
|
|
|
for feed in feeds:
|
2024-04-24 09:43:56 -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)
|
2024-04-24 09:43:56 -04:00
|
|
|
|
2011-11-27 02:41:12 -05:00
|
|
|
django.db.connection.close()
|
2024-04-24 09:43:56 -04:00
|
|
|
|
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()
|