-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetNews.py
More file actions
43 lines (32 loc) · 1.03 KB
/
Copy pathgetNews.py
File metadata and controls
43 lines (32 loc) · 1.03 KB
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
43
import django
import os
import traceback
from newsapi import NewsApiClient
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "login_api.settings")
django.setup()
def correcttime(timestamp: str):
if timestamp == "":
correct = "2000-01-01"
return correct
return timestamp
try:
import requests
from registration.models import News
token = os.environ.get('TOKEN_NEWS')
api = NewsApiClient(api_key=token)
for i in range(1,80):
values = api.get_everything(q="crypto",page_size=99,page=i)
for article in values["articles"]:
news = News(
source_name=article["source"]["name"],
author=article["author"],
title=article["title"],
description=article["description"],
url=article["url"],
image_url=article["urlToImage"],
published_at=article["publishedAt"],
content=article["content"],
)
news.save()
except:
traceback.print_exc()