-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
87 lines (74 loc) · 2.46 KB
/
app.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
from flask import Flask, render_template, request
import requests
import yaml
# from news import news_head
app = Flask(__name__)
ak = yaml.load(open('ak.yaml'))
@app.route('/')
def index():
coun_code = 'in'
url = ('http://newsapi.org/v2/top-headlines?'
'country={}&'
'apiKey={}').format(coun_code, ak['api_key'])
top_head = requests.get(url)
top_head_json = top_head.json()
news_head = []
news_link = []
news_img = []
for i in top_head_json['articles']:
a = i['title']
b = i['url']
c = i['urlToImage']
news_head.append(a)
news_link.append(b)
news_img.append(c)
news_material = zip(news_head, news_link, news_img)
return render_template('index.html', news_material = news_material)
@app.route('/input')
def input():
return render_template('input.html')
@app.route('/search_result', methods=['POST', 'GET'])
def search_result():
if request.method== 'POST':
coun_code= request.form['coun_code']
# print(coun_code)
url = ('http://newsapi.org/v2/top-headlines?'
'country={}&'
'apiKey={}').format(coun_code, ak['api_key'])
top_head = requests.get(url)
top_head_json = top_head.json()
news_head = []
for i in top_head_json['articles']:
a = i['title']
news_head.append(a)
# print(url)
# print(news_head)
return render_template('search_result.html', news_head=news_head, coun_code = coun_code )
# return 'hi'
@app.route('/search', methods=['POST', 'GET'])
def search():
if request.method== 'POST':
search_key= request.form['search_key']
url = ('http://newsapi.org/v2/top-headlines?'
'country={}&'
'apiKey={}&'
'q={}').format('in', ak['api_key'],search_key)
top_head = requests.get(url)
top_head_json = top_head.json()
news_head = []
news_link = []
news_img = []
for i in top_head_json['articles']:
a = i['title']
b = i['url']
c = i['urlToImage']
news_head.append(a)
news_link.append(b)
news_img.append(c)
news_material = zip(news_head, news_link, news_img)
return render_template('search.html', news_material = news_material)
@app.route('/login')
def login():
return render_template('login.html')
if __name__ == "__main__":
app.run(debug=True)