-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
178 lines (149 loc) · 5.59 KB
/
main.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
from flask import Flask, jsonify, request
from newsapi import NewsApiClient
import string
import json
app = Flask(__name__)
newsapi = NewsApiClient(api_key='6fe8231d03d44f71a91518bcdc21b4ea')
cnn_headlines = newsapi.get_top_headlines(sources='cnn', language='en', page_size=30)
fox_headlines = newsapi.get_top_headlines(sources='fox-news', language='en', page_size=30)
slide_headlines = newsapi.get_top_headlines(language='en', page_size=30)
all_headlines = newsapi.get_top_headlines(language='en', page_size=100)
cnn = []
fox = []
slide = []
word_map = {}
titles = []
categorys = ['business', 'entertainment', 'general', 'health', 'science', 'sports', 'technology']
sources = []
search = []
for c in categorys:
sources.append(newsapi.get_sources(category=c, language='en', country='us'))
categorys.append('all')
sources.append(newsapi.get_sources(language='en', country='us'))
def split_word(s):
words = s.split()
remove = string.punctuation + string.digits
table = str.maketrans('', '', remove)
wordlist = []
for w in words:
w = w.translate(table)
w = w.lower()
if w != "" and not w.isdigit():
wordlist.append(w)
return wordlist
for headline in cnn_headlines['articles']:
notEmpty = True
for key in headline:
if headline[key] is None or headline[key] == 'null' or headline[key] == '':
notEmpty = False
break
if notEmpty:
cnn.append(headline)
if len(cnn) == 4:
break
for headline in fox_headlines['articles']:
notEmpty = True
for key in headline:
if headline[key] is None or headline[key] == 'null' or headline[key] == '':
notEmpty = False
break
if notEmpty:
fox.append(headline)
if len(fox) == 4:
break
for headline in slide_headlines['articles']:
notEmpty = True
for key in headline:
if headline[key] is None or headline[key] == 'null' or headline[key] == '':
notEmpty = False
break
if notEmpty:
slide.append(headline)
if len(slide) == 5:
break
f = open("stopwords_en.txt")
lines = f.readlines()
stopwords = []
for line in lines:
line = line.rstrip()
stopwords.append(line)
remove = string.punctuation + string.digits
for headline in all_headlines['articles']:
if headline['title'] is not None:
word_list = split_word(headline['title'])
for word in word_list:
word = ''.join(c for c in word if c.isalnum())
if word not in stopwords and word != '-':
if word in word_map:
word_map[word] = word_map[word] + 1
else:
word_map[word] = 1
sort_word_map = sorted(word_map.items(), key=lambda x: x[1], reverse=True)
filtered_sort_word_map = sort_word_map[0:25]
word_map_tuple = []
for submap in filtered_sort_word_map:
word_map_tuple.append((submap[0].capitalize(), submap[1]))
@app.after_request
def after_request(resp):
resp.headers["Cache-Control"] = "no-store"
resp.headers["Pragma"] = "no-cache"
return resp
@app.route('/api/cnn', methods=['GET'])
def get_cnn():
return jsonify({'cnn_headlines': cnn})
@app.route('/api/fox', methods=['GET'])
def get_fox():
return jsonify({'fox_headlines': fox})
@app.route('/api/slide', methods=['GET'])
def get_slide():
return jsonify({'slide': slide})
@app.route('/api/word_cloud', methods=['GET'])
def get_word_cloud():
return dict(word_map_tuple)
@app.route('/api/source', methods=['GET'])
def get_source():
return jsonify({categorys[0]: sources[0], categorys[1]: sources[1], categorys[2]: sources[2], categorys[3]: sources[3], categorys[4]: sources[4], categorys[5]: sources[5], categorys[6]: sources[6], categorys[7]: sources[7]})
@app.route('/api/search', methods=['GET'])
def get_search():
return jsonify({'search': search})
@app.route('/', methods=['GET', 'POST'])
def homepage():
if(request.method == 'POST'):
keyword = request.form['keyword']
fromDate = request.form['from']
toDate = request.form['to']
category = request.form['category']
source = request.form['source']
global search
try:
search = []
if(source == 'all'):
for i in range(0, 8):
if categorys[i] == category:
sourcelist = sources[i]['sources']
sourcestring = ""
for s in sourcelist:
sourcestring = sourcestring + s['id'] + ","
sourcestring = str(sourcestring[:-1])
search_headlines = newsapi.get_everything(q=keyword, from_param=fromDate, to=toDate, sources=sourcestring, language='en', sort_by='publishedAt')
break
else:
search_headlines = newsapi.get_everything(q=keyword, from_param=fromDate, to=toDate, sources=source, language='en', sort_by='publishedAt')
for headline in search_headlines['articles']:
notEmpty = True
for key in headline:
if headline[key] is None or headline[key] == 'null' or headline[key] == '':
notEmpty = False
break
if notEmpty:
search.append(headline)
if(len(search) == 15):
break
except Exception as e:
eString = str(e)
eJson = eval(eString)
search.clear()
search.append(eJson)
return app.send_static_file('index.html')
if __name__ == '__main__':
app.run(debug=open)