-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiveNews.py
23 lines (17 loc) · 954 Bytes
/
LiveNews.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Author : Bhaumik Darshan Choksi
import json
import urllib.request
class LiveNews:
def __init__(self, API_KEY):
self.API_KEY = API_KEY
def fetch(self, number_of_articles=100, sort_by="top", source="bbc-news"):
data = []
with urllib.request.urlopen("https://newsapi.org/v1/sources") as url:
sources_data = json.loads(url.read().decode())
all_sources_list = list(s['name'] for s in sources_data["sources"])
# print("https://newsapi.org/v1/articles?source=" + source + "&sortBy=" + sort_by + "&apiKey=" + self.API_KEY)
with urllib.request.urlopen(
"https://newsapi.org/v1/articles?source=" + source + "&sortBy=" + sort_by + "&apiKey=" + self.API_KEY) as url:
data = json.loads(url.read().decode())
return list({"title":article["title"], "url":article["url"]} for article in data["articles"])[:number_of_articles]
# Author : Bhaumik Darshan Choksi