File tree Expand file tree Collapse file tree 6 files changed +263
-0
lines changed Expand file tree Collapse file tree 6 files changed +263
-0
lines changed Original file line number Diff line number Diff line change 1+ import requests
2+ from flask import Flask , render_template , request
3+
4+ """
5+ When you try to scrape reddit make sure to send the 'headers' on your request.
6+ Reddit blocks scrappers so we have to include these headers to make reddit think
7+ that we are a normal computer and not a python script.
8+ How to use: requests.get(url, headers=headers)
9+ """
10+
11+ headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36' }
12+
13+
14+ """
15+ All subreddits have the same url:
16+ i.e : https://reddit.com/r/javascript
17+ You can add more subreddits to the list, just make sure they exist.
18+ To make a request, use this url:
19+ https://www.reddit.com/r/{subreddit}/top/?t=month
20+ This will give you the top posts in per month.
21+ """
22+
23+ subreddits = [
24+ "javascript" ,
25+ "reactjs" ,
26+ "reactnative" ,
27+ "programming" ,
28+ "css" ,
29+ "golang" ,
30+ "flutter" ,
31+ "rust" ,
32+ "django"
33+ ]
34+
35+
36+ app = Flask ("DayEleven" )
37+
38+
39+ @app .route ("/" )
40+ def home ():
41+ return render_template ("home.html" , subreddits = subreddits )
42+
43+
44+ @app .route ("/read" )
45+ def read ():
46+ selected = []
47+ for subreddit in subreddits :
48+ if subreddit in request .args :
49+ selected .append (subreddit )
50+ posts = subreddits (selected )
51+ posts .sort (key = lambda post : post ['votes' ], reverse = True )
52+ return render_template ("read.html" , selected = selected , posts = posts )
53+
54+
55+ app .run (host = "0.0.0.0" )
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+
4+ < head >
5+ < title >
6+ Wook's News |
7+ {% if order_by == 'new' %}
8+ New
9+ {% elif order_by == 'popular'%}
10+ Popular
11+ {% endif %}
12+ </ title >
13+ < link href ="https://andybrewer.github.io/mvp/mvp.css " rel ="stylesheet "> </ link >
14+ </ head >
15+
16+ < body >
17+ < header >
18+ < h1 > Wook's News</ h1 >
19+ < div >
20+ Order by:
21+ {% if order_by == 'new' %}
22+ < a href ="/?order_by=popular "> Popular</ a >
23+ {% else %}
24+ < strong > Popular</ strong >
25+ {% endif %}
26+ |
27+ {% if order_by == 'popular' %}
28+ < a href ="/?order_by=new "> New</ a >
29+ {% else %}
30+ < strong > New</ strong >
31+ {% endif %}
32+ </ div >
33+ </ header >
34+ < main >
35+ {% for result in results %}
36+ < div >
37+ < div >
38+ < a href ="/{{result.objectID}} ">
39+ < h3 > {{result.title}}</ h3 >
40+ </ a >
41+ (< a href ="{{result.url}} " target ="_blanl "> {{result.url}}</ a > )
42+ </ div >
43+ < div >
44+ {{result.points}} points | By: {{result.author}} | {{result.num_comments}} comments
45+ </ div >
46+ </ div >
47+ < hr />
48+ {% endfor %}
49+ </ main >
50+ </ body >
51+
52+ </ html >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+
4+ < head >
5+ < title >
6+ Wook's News | {{result.title}}
7+ </ title >
8+ < link href ="https://andybrewer.github.io/mvp/mvp.css " rel ="stylesheet "> </ link >
9+ </ head >
10+
11+ < body >
12+ < header >
13+ < h1 > {{result.title}}</ h1 >
14+ < div >
15+ {{result.points}} points | By {{result.author}} | < a href ="{{result.url}} " target ="_blank "> {{result.url}}</ a >
16+ </ div >
17+ </ header >
18+ < main >
19+ {% for comment in result.children %}
20+ < div >
21+ {% if comment.author == None%}
22+ [deleted]
23+ {% else %}
24+ < strong > {{comment.author}}:</ strong >
25+ < p > {{comment.text | safe}}</ p >
26+ {% endif %}
27+ </ div >
28+ < hr />
29+ {% endfor %}
30+ </ main >
31+ </ body >
32+
33+ </ html >
Original file line number Diff line number Diff line change @@ -46,4 +46,42 @@ def detail(id):
4646 return render_template ("detail.html" , result = result )
4747
4848
49+ app .run (host = "0.0.0.0" )
50+
51+
52+ # 정답
53+ import requests
54+ from flask import Flask , render_template , request
55+
56+ base_url = "http://hn.algolia.com/api/v1"
57+ new = f"{ base_url } /search_by_date?tags=story"
58+ popular = f"{ base_url } /search?tags=story"
59+
60+ def make_detail_url (id ):
61+ return f"{ base_url } /items/{ id } "
62+
63+ db = {}
64+ app = Flask ("DayNine" )
65+
66+ @app .route ("/" )
67+ def home ():
68+ order_by = request .args .get ('order_by' , 'popular' )
69+ if order_by not in db :
70+ print ("Requesting" )
71+ if order_by == 'popular' :
72+ news = requests .get (popular )
73+ elif order_by == 'new' :
74+ news = requests .get (new )
75+ results = news .json ()['hits' ]
76+ db [order_by ] = results
77+ results = db [order_by ]
78+ return render_template ("index.html" , order_by = order_by , results = results )
79+
80+
81+ @app .route ("/<id>" )
82+ def detail (id ):
83+ detail_request = requests .get (make_detail_url (id ))
84+ result = detail_request .json ()
85+ return render_template ("detail.html" ,result = result )
86+
4987app .run (host = "0.0.0.0" )
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+
4+ < head >
5+ < title >
6+ Wook's News | {{result.title}}
7+ </ title >
8+ < link href ="https://andybrewer.github.io/mvp/mvp.css " rel ="stylesheet "> </ link >
9+ </ head >
10+
11+ < body >
12+ < header >
13+ < h1 > {{result.title}}</ h1 >
14+ < div >
15+ {{result.points}} points | By {{result.author}} | < a href ="{{result.url}} " target ="_blank "> {{result.url}}</ a >
16+ </ div >
17+ </ header >
18+ < main >
19+ {% for comment in result.children %}
20+ < div >
21+ {% if comment.author == None%}
22+ [deleted]
23+ {% else %}
24+ < strong > {{comment.author}}:</ strong >
25+ < p > {{comment.text | safe}}</ p >
26+ {% endif %}
27+ </ div >
28+ < hr />
29+ {% endfor %}
30+ </ main >
31+ </ body >
32+
33+ </ html >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+
4+ < head >
5+ < title >
6+ Wook's News |
7+ {% if order_by == 'new' %}
8+ New
9+ {% elif order_by == 'popular'%}
10+ Popular
11+ {% endif %}
12+ </ title >
13+ < link href ="https://andybrewer.github.io/mvp/mvp.css " rel ="stylesheet "> </ link >
14+ </ head >
15+
16+ < body >
17+ < header >
18+ < h1 > Wook's News</ h1 >
19+ < div >
20+ Order by:
21+ {% if order_by == 'new' %}
22+ < a href ="/?order_by=popular "> Popular</ a >
23+ {% else %}
24+ < strong > Popular</ strong >
25+ {% endif %}
26+ |
27+ {% if order_by == 'popular' %}
28+ < a href ="/?order_by=new "> New</ a >
29+ {% else %}
30+ < strong > New</ strong >
31+ {% endif %}
32+ </ div >
33+ </ header >
34+ < main >
35+ {% for result in results %}
36+ < div >
37+ < div >
38+ < a href ="/{{result.objectID}} ">
39+ < h3 > {{result.title}}</ h3 >
40+ </ a >
41+ (< a href ="{{result.url}} " target ="_blanl "> {{result.url}}</ a > )
42+ </ div >
43+ < div >
44+ {{result.points}} points | By: {{result.author}} | {{result.num_comments}} comments
45+ </ div >
46+ </ div >
47+ < hr />
48+ {% endfor %}
49+ </ main >
50+ </ body >
51+
52+ </ html >
You can’t perform that action at this time.
0 commit comments