-
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.
Replace AppScheduler with Redis + Celery
* use Redis + Celery for running background tasks * remove Appscheduler and pyppeteer patch * refactor scraper * update docker-compose
- Loading branch information
Showing
28 changed files
with
410 additions
and
723 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
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 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 scraper.tasks import magazines | ||
|
||
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 magazines[language]["titles"] | ||
) | ||
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
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
Oops, something went wrong.