-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseNewsAPI.py
40 lines (31 loc) · 1.53 KB
/
BaseNewsAPI.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
import requests
import json
from datetime import datetime
class BaseNewsAPI:
def __init__(self, api_key):
self.api_key = api_key
def backupAPI(self):
categories = ['business', 'entertainment', 'general', 'health', 'science', 'sports', 'technology']
for category in categories:
url = (f'https://newsapi.org/v2/top-headlines?country=mx&category={category}&apiKey={self.api_key}')
response = requests.get(url)
response_json = response.json()
with open(f'database/{category}.json', 'w') as f:
json.dump(response_json.get('articles'), f)
def getArticles(self, category):
# read the json file for the category
with open(f'database/{category}.json', 'r') as f:
articles = json.load(f)
return articles
def getTitle(self, article):
return article.get('title') if article.get('title') != None else ''
def getDescription(self, article):
return article.get('description') if article.get('description') != None else ''
def getUrl(self, article):
return article.get('url') if article.get('url') != None else ''
def getImage(self, article):
return article.get('urlToImage') if article.get('urlToImage') != None else ''
def getAuthor(self, article):
return article.get('author') if article.get('author') != None else ''
def getPublished(self, article):
return datetime.fromisoformat(article.get('publishedAt')[:-1]) if article.get('publishedAt') != None else ''