-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewsAPI.py
66 lines (54 loc) · 1.65 KB
/
NewsAPI.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
63
64
65
66
import apiKey
import subprocess
import sys
import webbrowser
from os import system, name
try:
from newsapi import NewsApiClient
except:
subprocess.check_call([sys.executable, "-m", "pip", "install", "newsapi-python"])
finally:
from newsapi import NewsApiClient
class Article:
def __init__(self):
self.title = ''
self.url = ''
class NewsAPI:
def __init__(self):
self.Articles = []
def _getArticles(self):
client = NewsApiClient(api_key = apiKey.ApiKey)
top = client.get_top_headlines(page_size=10,country='us')
for v in top['articles']:
a = Article()
a.title = v['title']
a.url = v['url']
self.Articles.append(a)
def _printArticles(self):
count = 1
print(f"{'':10}*** Top US Articles ***")
print()
for i in self.Articles:
print(f"{count}) {i.title}\n")
count += 1
def _clear(self):
system('cls') if name == 'nt' else system('clear')
def _prompt(self):
selection = 0
while selection != '':
try:
selection = input("Print article (#) or Enter to quit?: ")
if selection != '':
webbrowser.open(n.Articles[int(selection)-1].url)
self._clear()
self._printArticles()
except (ValueError, IndexError):
print("Enter article number or Enter to quit")
def GetNews(self):
self._clear()
self._getArticles()
self._printArticles()
self._prompt()
if __name__ == "__main__":
n = NewsAPI()
n.GetNews()