-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnews.py
226 lines (195 loc) · 7.57 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from urllib.request import urlopen
from urllib.error import HTTPError
import json
from logo import printlogo
from colorama import Fore, Style, Back
cmds = {"international": ["BBC News", "CNN", "Daily Mail", "The Guardian UK", "The New York Times", "The Telegraph"],
"national": ["The Times of India", "The Hindu"],
"technology": ["Engadget", "Hacker News", "Techcrunch", "TechRadar", "The Next Web"],
"sports": ["BBC Sport", "ESPN", "ESPN Cric info", "Fox Sports", "TalkSport"],
"finance": ["Bloomberg", "Business Insider", "Financial Times", "The Wall Street Journal", "Time", "Fortune"],
"entertainment": ["BuzzFeed", "Entertainment Weekly", "MTV News", "MTV News UK"],
"science": ["New Scientist"],
"blog": ["Reddit r all"],
"adventure": ["National Geographic"],
"help": ["help", "exit","list-news","sources"],
"list-news": ["international", "national", "technology", "sports", "finance", "entertainment", "science", "blog", "adventure"]
}
# global api key to be given by the user.
api_key = None
def news(source):
print(" 1. Top News\n 2. Latest News\n 3. Most Popular News\n 4. Return")
loop = 1
while loop:
flag = int(input("[1/2/3/4] >>> "))
if flag == 1:
url = "https://newsapi.org/v1/articles?source=" + source + "&sortBy=top&apiKey=" + api_key
loop = 0
elif flag == 2:
url = "https://newsapi.org/v1/articles?source=" + source + "&sortBy=latest&apiKey=" + api_key
loop = 0
elif flag == 3:
url = "https://newsapi.org/v1/articles?source=" + source + "&sortBy=popular&apiKey=" + api_key
loop = 0
elif flag == 4:
loop = 0
return console()
else:
print("Enter a valid number!")
# using with .. as to allow closing the url connection by python.
try:
with urlopen(url) as httpob:
decob = httpob.read().decode("utf-8")
jsonob = json.loads(decob)
news = jsonob["articles"]
# if api key is Invalid an HTTPError will be thrown.
except HTTPError as e:
print("Invalid API")
create_api_file(file_name)
return console()
# draws a border to seperate posts.
draw_border()
for n in news:
print(Fore.RESET)
try:
print(Back.YELLOW + src2(jsonob["source"]) + Back.RESET)
print((Style.BRIGHT +"By: " + n["author"]) + Style.RESET_ALL)
#Sometimes the author is not provided. For those cases, 'Except' has been put.
#If no author provided, The author will be give out to be the news publishing company.
except:
print((Style.BRIGHT +"By: " + src2(jsonob["source"])) + Style.RESET_ALL)
try:
print(Back.RED + (Style.BRIGHT + n["title"] + Style.RESET_ALL))
print(Fore.BLUE + n["description"] + Fore.RESET)
except:
print(Fore.RED + "SOME ERROR OCCURED!!!\n" + Fore.RESET)
print(Back.BLUE +(Style.BRIGHT + "url: "+ n["url"]) + Style.RESET_ALL + Back.RESET)
#Similar to author, sometimes the Publishing time is not provided.
#For those cases, there will be no publishing time put. So except case has been made.
try:
print(Fore.GREEN + "Published At: "+ n["publishedAt"] + Fore.RESET )
except:
draw_border()
continue
draw_border()
def sour():
#The is just one url for sources function and no further modification to it required,
#Hence url has been initialised at the starting for the same.
url = "https://newsapi.org/v1/sources?language=en"
try:
with urlopen(url) as httpob:
decob = httpob.read().decode("utf-8")
jsonob = json.loads(decob)
sources = jsonob["sources"]
except HTTPError as e:
print("Invalid API")
create_api_file(file_name)
return console()
draw_border()
key = 1
for s in sources:
print(Fore.RESET)
try:
print(key, end="")
print(". " + Back.BLUE + (Style.BRIGHT +s["name"] + Style.RESET_ALL) + Back.RESET)
key = key+1
except:
print(Fore.RED + "SOME ERROR OCCURED!!!\n" + Fore.RESET)
draw_border()
#Incase more detailed description of the source required.
print("Enter the index of any source if you want to know more about it.\nEnter -1 for returning back to main menu\n")
loop = True
while loop:
flag = input(">")
try:
if flag == '-1':
loop = False
elif int(flag)>0 or int(flag)<60:
s = sources[int(flag) -1]
print('\n')
print(Back.BLUE + (Style.BRIGHT +s["name"] + Style.RESET_ALL) + Back.RESET)
print("Description: " + Fore.BLUE + s["description"] + Fore.RESET)
print("Category: " + Fore.RED + s["category"] + Fore.RESET)
print("Url: " + Fore.GREEN + (Style.BRIGHT + s["url"]) + Fore.RESET)
print("\nEnter any other index. Enter -1 to exit\n")
except:
print("Invalid Entry!\n")
def draw_border():
width = 80
print(Style.BRIGHT+"-" * width +Style.RESET_ALL+"\n")
def src(n):
k = n.replace(" ", "-")
return k
def src2(n):
k = n.replace("-"," ")
return k
def create_api_file(file_name):
"""
This method creates a new file, with the name
'file_name'.
This file will store the api key for the user.
"""
global api_key
api_key = input("Enter a valid API key: ")
with open(file_name, "w") as f:
f.write(api_key + '\n')
f.close()
print("The API key is: " + api_key)
def get_api():
global api_key
# the api once entered will be stored in this file.
global file_name
file_name = "user-api.txt"
try:
f = open(file_name, "r")
api_key = f.readline()
f.close()
print("The API key is: " + api_key)
print("Do you want to change the API key? [Y/N]\n")
flag = input()
if flag == 'Y':
create_api_file(file_name)
# if the file was not found then create the file and store the api.
except FileNotFoundError:
create_api_file(file_name)
# if the api_key in the file was not present (i.e, empty file)
# get the api from the user.
if api_key == None or len(api_key) == 0:
create_api_file(file_name)
def console():
while True:
cmd = input(">>> ")
# command is invalid
if (cmd not in cmds["list-news"] and cmd not in cmds["help"]):
print(Fore.RED + "WRONG COMMAND!!!")
print(Fore.GREEN + "Try these COMMANDS" + Fore.RESET)
for c in cmds["help"]:
print(" " + c)
# help command
elif cmd == "help":
print(Fore.GREEN + "Try these COMMANDS" + Fore.RESET)
for c in cmds["help"]:
print(" " + c)
# list command
elif cmd == "list-news":
print(Fore.GREEN + "Find news about any of these topics" + Fore.RESET)
for l in cmds["list-news"]:
print(" " + l)
# exit command
elif cmd == "exit":
print(Style.RESET_ALL)
exit()
elif cmd == "sources":
sour()
# show news
else:
for n in cmds[cmd]:
s = src(n)
news(s)
if __name__ == "__main__":
printlogo()
print(Style.DIM + "powered by NewsAPI.org\n" + Style.NORMAL)
# get the user api first.
get_api()
# display the console.
console()