-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
42 lines (29 loc) · 1002 Bytes
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import asyncio
import os
import time
import django
import schedule
from coreNews.parser import main
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ReadAndGo.settings')
django.setup()
from django.db import transaction
from coreNews.models import News
def data_in_database():
info = asyncio.run(main())
with transaction.atomic():
News.objects.all().delete()
news_instances = []
for source, data in info.items():
publisher = source
for urls, content in data.items():
url = urls
article_title = content['title']
short_text = content['short_text']
news_instances.append(News(publisher=publisher, url=url, title=article_title, short_text=short_text))
News.objects.bulk_create(news_instances)
if __name__ == '__main__':
data_in_database()
schedule.every(3600).seconds.do(data_in_database)
while True:
schedule.run_pending()
time.sleep(1)