forked from xenon134/Nova
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newsApp_v2.py
182 lines (159 loc) · 5.38 KB
/
newsApp_v2.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
from tkinter import ttk, font
import tkinter as tk
from json import loads
import urllib.request as urlr
bgColor = "#121212"
fgColor = "#ffffff"
headFont = "Franklin Gothic Demi"
hed2Font = "Courier New"
bodyFont = "Tahoma"
with open("countryCode.json", "r") as f:
countries = loads(f.read())
categories = (
" Business",
" Entertainment",
" General",
" Health",
" Science",
" Sports",
" Technology",
)
def loadData(search, country, category):
url = "https://newsapi.org/v2/top-headlines?country={}&category={}&apiKey=5286afcbe52e4e65ab101cce7af5182b".format(
country, category
)
if search:
url = url + "&q=" + search
raw = urlr.urlopen(url)
content = loads(raw.read())
if content["status"] == "error":
raise Exception(content["message"])
else:
return content
root = tk.Tk()
root.geometry("1036x570")
root.configure(bg=bgColor)
root.title("festival0156n NEWS App")
def displayNews(data):
if data["totalResults"] == 0:
tk.Label(
newsContentFrame,
text="No articles match selectors.",
fg=fgColor,
font=(hed2Font, 30),
bg=bgColor,
).place(relx=0.6, rely=0.5, anchor="center")
else:
for i in data["articles"]:
frame = tk.Frame(newsContentFrame, bg=bgColor)
"""tk.Label(frame, text = i['title'], fg = fgColor, wraplength = 2000, bd = 3, padx = 10,
font = (hed2Font, 10), justify = 'left', bg = bgColor).pack(side = tk.TOP, fill = tk.X, pady = 10)"""
txt = tk.Text(
frame,
bg=bgColor,
fg=fgColor,
bd=2,
height=2,
pady=10,
padx=10,
font=font.Font(family=bodyFont, size=14),
)
txt.insert(tk.END, i["title"])
txt.configure(state="disabled")
txt.pack(fill=tk.X)
print(i["title"] + "\n")
frame.pack(fill=tk.X)
articles.append(txt)
def showError(exs):
tk.Label(
newsContentFrame,
text="Error loading news data.",
fg=fgColor,
font=(hed2Font, 30),
bg=bgColor,
).place(relx=0.5, rely=0.5, anchor="center")
def reload():
for i in articles:
i.destroy()
cn = countries[countryS.current()]
ct = categories[categoryS.current()].strip()
hTxt.configure(
text="Top{} Headlines from {}".format(
"" if ct == "General" else " " + ct, cn[0]
)
)
loadingFrames = [
tk.PhotoImage(file="Spinner-1s-200px.gif", format="gif -index " + str(i))
for i in range(31)
]
def update(ind):
try:
currentFrame = loadingFrames[ind]
ind += 1
imgLbl.configure(image=currentFrame)
root.after(20, update, ind % 31)
except:
pass
imgLbl = tk.Label(newsContentFrame, bd=0)
imgLbl.place(relx=0.5, rely=0.4, anchor="center")
t = tk.Label(
newsContentFrame,
text="Fetching news data ...",
fg=fgColor,
font=(hed2Font, 30),
bg=bgColor,
)
t.place(relx=0.5, rely=0.75, anchor="center")
root.after(0, update, 0)
def loadNews():
try:
data = loadData(sbox.get(), cn[1], ct)
except Exception as exc:
imgLbl.destroy()
t.destroy()
showError(str(exc))
else:
imgLbl.destroy()
t.destroy()
displayNews(data)
__import__("threading").Thread(target=loadNews).start()
header = tk.Frame(root, bg=bgColor)
hTxt = tk.Label(header, fg=fgColor, font=(headFont, 30), bg=bgColor)
hTxt.pack(fill=tk.X, pady=20)
tk.Label(header, text="Country: ", bg=bgColor, fg=fgColor, padx=7).pack(side=tk.LEFT)
countryS = ttk.Combobox(
header,
width=30,
textvariable=tk.StringVar(),
values=[" " + i[0] for i in countries],
)
countryS.current(17)
countryS.configure(state="readonly")
countryS.pack(side=tk.LEFT)
tk.Label(header, text="Category: ", bg=bgColor, fg=fgColor, padx=7).pack(side=tk.LEFT)
categoryS = ttk.Combobox(
header, width=30, textvariable=tk.StringVar(), values=categories
)
categoryS.current(6)
categoryS.configure(state="readonly")
categoryS.pack(side=tk.LEFT)
simg = tk.Label(header, bd=0)
simg.image = tk.PhotoImage(file="ic_search_48px-512.gif", format="gif")
simg.configure(image=simg.image)
simg.place(x=560, rely=0.9, anchor="center")
sbox = tk.Entry(header)
sbox.place(relx=0.77, rely=0.9, relwidth=0.38, anchor="center")
refreshButtonImage = tk.PhotoImage(file="1024px-Refresh_icon.gif", format="gif")
rimg = tk.Button(header, image=refreshButtonImage, bd=0, bg=bgColor, command=reload)
rimg.place(relx=0.987, rely=0.9, anchor="center")
header.pack(fill=tk.X)
canvas = tk.Canvas(root, bd=0, bg=bgColor, highlightthickness=0)
newsContentFrame = tk.Frame(canvas, bg=bgColor, padx=7, pady=7, bd=2)
newsContentFrame.place(relx=0.5, rely=0.5, anchor="center", relheight=1, relwidth=1)
canvas.place(y=116, relwidth=1, relheight=0.7)
sb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=sb.set)
sb.pack(side=tk.RIGHT, fill=tk.Y)
articles = list()
reload()
root.mainloop()