-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnews.py
62 lines (56 loc) · 1.76 KB
/
news.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import requests
from info import news_sources
from datetime import datetime, timedelta
import json
import os
class news(object):
def __init__(self,keywords):
self.api_key = os.environ['API_KEY']
self.keywords = keywords
self.url = "http://newsapi.org/v2/everything"
self.from_date = (
(datetime.now() - timedelta(days=15)).date().strftime("%Y-%m-%d")
)
def build_parameters(self):
print(self.keywords)
return {
"q": self.keywords,
# "qInTitle":self.keywords,
"pageSize": 20,
"apiKey": self.api_key,
"sources": ",".join(news_sources),
"sortBy": "popularity",
"from": self.from_date,
"language":"en",
}
def get_news(self):
try:
response = requests.get(self.url, params=self.build_parameters())
return response.json()
except ConnectionError as e:
print("Issue with Connecting with news sources")
return None
def remove_dupe_dicts(self,news_list):
list_of_strings = [
json.dumps(news, sort_keys=True)
for news in news_list
]
list_of_strings = set(list_of_strings)
return [
json.loads(s)
for s in list_of_strings
]
def cleanup_news(self):
all_news = self.get_news()
if all_news:
data = [
{
"title": news["title"],
"url": news["url"],
"source":news["source"]["name"],
"urlToImage":news["urlToImage"]
}
for news in all_news["articles"]
]
return data
return None