-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(refactor) Replace AppScheduler with Redis + Celery
* introduce redis + celery for running background tasks * remove appscheduler and pyppeteer patch * refactor scraper
- Loading branch information
Showing
25 changed files
with
390 additions
and
670 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
sources = { | ||
"en": [ | ||
"Al Jazeera", | ||
"Associated Press", | ||
"Christian Science Monitor", | ||
"Consortium News", | ||
"Current Affairs", | ||
"New York Times", | ||
"NPR", | ||
"Reuters", | ||
"The Atlantic", | ||
"The Intercept", | ||
"UPI", | ||
"Wall Street Journal", | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import json | ||
|
||
from celery import group, shared_task | ||
from django.utils import timezone | ||
|
||
import scraper | ||
|
||
from .data import sources | ||
from .models import Article, Source | ||
|
||
|
||
@shared_task | ||
def get_articles_for_source(source_title: str): | ||
source: Source = Source.objects.get(name=source_title) | ||
sitemap = source.to_dict() | ||
starting_urls = [ | ||
sitemap["base_url"] + path for path in sitemap["paths"] | ||
] | ||
|
||
spider = scraper.Spider(starting_urls, sitemap) | ||
scraper.run(spider) | ||
data = [json.loads(article) for article in spider.articles] | ||
|
||
Article.objects.bulk_create([ | ||
Article( | ||
headline=article_data["headline"], | ||
slug=article_data["slug"], | ||
source=Source.objects.get(link=article_data["source_link"]), | ||
summary=article_data["summary"], | ||
language=article_data["language"], | ||
link=article_data["link"], | ||
created_at=timezone.now(), | ||
) for article_data in data | ||
], ignore_conflicts=True) | ||
|
||
|
||
@shared_task | ||
def get_articles(language: str): | ||
task_group = group( | ||
get_articles_for_source.s(source_title=title) for title in sources[language] | ||
) | ||
promise = task_group.apply_async() | ||
if promise.ready(): | ||
return promise.get() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .celery import app as celery_app | ||
|
||
__all__ = ("celery_app",) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import os | ||
|
||
from celery import Celery | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nous_aggregator.settings.local") | ||
|
||
app = Celery("nous_aggregator") | ||
|
||
app.config_from_object("django.conf:settings", namespace="CELERY") | ||
|
||
app.autodiscover_tasks() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
from .base import * | ||
# from .base import * | ||
|
||
SECRET_KEY = "hush-hush" | ||
# SECRET_KEY = "hush-hush" | ||
|
||
DEBUG = False | ||
# DEBUG = False | ||
|
||
ALLOWED_HOSTS = ['*'] | ||
# ALLOWED_HOSTS = ['*'] | ||
|
||
DATABASES = { | ||
"default": { | ||
"ENGINE": "django.db.backends.postgresql", | ||
"NAME": "nous_aggregator", | ||
"USER": "postgres", | ||
"PASSWORD": "postgres", | ||
"HOST": "localhost", | ||
"PORT": 5432, | ||
}, | ||
} | ||
# DATABASES = { | ||
# "default": { | ||
# "ENGINE": "django.db.backends.postgresql", | ||
# "NAME": "nous_aggregator", | ||
# "USER": "postgres", | ||
# "PASSWORD": "postgres", | ||
# "HOST": "localhost", | ||
# "PORT": 5432, | ||
# }, | ||
# } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import sentry_sdk | ||
from sentry_sdk.integrations.django import DjangoIntegration | ||
|
||
from .staging import * | ||
from .base import * | ||
|
||
sentry_sdk.init( | ||
dsn="https://[email protected]/6748377", | ||
|
Oops, something went wrong.