-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewsAPi_Main.py
30 lines (27 loc) · 1.27 KB
/
NewsAPi_Main.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
import requests
from Data import Country
from Connection import save
CATEGORY_LIST = [
'general',
'business',
'entertainment',
'sport',
'technology',
]
COUNTRIES_LIST_API_NEWSAPI = [
Country('USA', 'us', 'en', 'us', 'us', CATEGORY_LIST, '0811267b223b454a9698204d0888722a'), #api_email = [email protected]
Country('United Kingdom', 'uk', 'en', 'gb', 'uk', CATEGORY_LIST, 'bc1069448aeb4cc0ab11f9253ff8b834'), #api_email = [email protected]
]
ARTICLES_JSON_FORMAT = {"articles": [], "category_name": ""}
def load():
for i, country in enumerate(COUNTRIES_LIST_API_NEWSAPI):
for j, category in enumerate(country.categories):
if country.edition_code_newsapi is not None:
req = requests.get(
'https://newsapi.org/v2/top-headlines?category=%s&language=%s&country=%s&apiKey=%s'
% (category, country.language_code, country.edition_code_newsapi, country.NEWSAPI_KEY))
json = req.json().get('articles')
(ARTICLES_JSON_FORMAT["articles"], ARTICLES_JSON_FORMAT["category_name"]) = json, category
save('/APIs/0/countries/%s/news/categories/' % i, '%s' % j, ARTICLES_JSON_FORMAT)
if __name__ == '__main__':
load()