api를 이용해서 기사 정렬 페이지 만들기
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
import requests
from flask import Flask, render_template, request
base_url = "http://hn.algolia.com/api/v1"
# This URL gets the newest stories.
new = f"{base_url}/search_by_date?tags=story"
# This URL gets the most popular stories
popular = f"{base_url}/search?tags=story"
# This function makes the URL to get the detail of a storie by id.
# Heres the documentation: https://hn.algolia.com/api
def make_detail_url(id):
return f"{base_url}/items/{id}"
db = {}
app = Flask("DayNine")
def get_info(url):
datas = requests.get(url).json()['hits']
info = []
for data in datas:
title = data['title']
url = data['url']
points = data['points']
author = data['author']
num_comments = data['num_comments']
obj_id = data['objectID']
info.append({
"title": title,
"url" : url,
"points" : points,
"author" : author,
"num_comments" : num_comments,
"id" : obj_id
})
return info
def get_comments(url):
data = requests.get(url).json()
comms = data['children']
info = []
comments = []
for comm in comms:
author = comm['author']
comment = comm['text']
comments.append({"author":author, "comment":comment})
title = data["title"]
author = data["author"]
url = data["url"]
points = data["points"]
info.append({"title":title, "author":author, "url":url, "points":points})
return info, comments
@ app.route("/")
def home():
query_info = request.args.to_dict()
# /?order_by=popular
# {'order_by': 'popular'}
if query_info.get('order_by') == None:
query = 'popular'
else:
query = query_info['order_by']
if query == "popular":
exist = db.get("popular")
if not exist:
exist = get_info(popular)
db["popular"] = exist
else :
exist = db.get("new")
if not exist:
exist = get_info(new)
db["new"] = exist
return render_template("index.html",info = exist,toggle = query)
@ app.route("/<id>")
def comments(id):
# receive id and make detail url
url = make_detail_url(id)
info,comments = get_comments(url)
return render_template("detail.html",info = info,comments = comments)
app.run(host="0.0.0.0")
templates
- index.html
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
<!DOCTYPE html>
<html>
<head>
<link href="https://andybrewer.github.io/mvp/mvp.css" rel="stylesheet"></link>
</head>
<body>
<header>
<h1>Nomad News</h1>
<div>
Order by:
{% if toggle == 'popular' %}
<strong>Popular</strong>
<a href="/?order_by=new">New</a>
{% else %}
<a href="/?order_by=popular">Popular</a>
<strong>New</strong>
{% endif %}
</div>
</header>
<main>
{% for i in info %}
<div>
<div>
<a href="/{{i.id}}">
<h3>{{i.title}}</h3>
</a>
(
<a href="{{i.url}}" target="_blanl">{{i.url}}</a>
)
</div>
<div>
{{i.points}} points | By: {{i.author}} | {{i.num_comments}} comments
</div>
</div>
<hr>
{% endfor %}
</main>
</body>
</html>
- detail.html
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
<!DOCTYPE html>
<html>
<head>
<link href="https://andybrewer.github.io/mvp/mvp.css" rel="stylesheet"></link>
</head>
<body>
</body>
<header>
<h1>{{info[0].title}}</h1>
<div>
{{info[0].points}} points | By {{info[0].author}} | <a href="{{info[0].url}}">{{info[0].url}}</a>
</div>
</header>
<main>
{% for com in comments %}
<div>
{%if com.author==None %}
<p>deleted</p>
{% else %}
<strong>{{com.author}} : </strong>
<div> {{com.comment|safe}} </div>
{% endif %}
</div>
<hr>
{% endfor %}
</main>
</html>
메모
-
파이썬 문법부터 flask 사용까지 처음이라 매우 헤맸다
-
어려웠던 부분
- api 사용 하기(무슨 의미인지 파악 하기 까지)
- 가져온 정보를 fake db에 저장할 때의 구조 생각하기
- route 만드는 논리
- html에 데이터를 가져가 사용하는 부분