Skip to content

Commit 9f872e5

Browse files
committed
DAY 12 OF 14 PYTHON CHALLENGE
1 parent 8e77272 commit 9f872e5

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed

Assignment 2/DAY 12 OF 14 PYTHON CHALLENGE.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,60 @@ def read():
3030
posts.sort(key=lambda post: post['votes'], reverse=True)
3131
return render_template("read.html", selected=selected, posts=posts)
3232

33+
app.run(host="0.0.0.0")
34+
35+
36+
# 정답
37+
from flask import Flask, render_template, request, redirect
38+
from scrapper import aggregate_subreddits, check_subreddit
39+
40+
app = Flask("RedditNews")
41+
42+
subreddits = [
43+
"javascript",
44+
"reactjs",
45+
"reactnative",
46+
"programming",
47+
"css",
48+
"golang",
49+
"flutter",
50+
"rust",
51+
"django"
52+
]
53+
54+
@app.route("/")
55+
def home():
56+
return render_template("home.html", subreddits=subreddits)
57+
58+
59+
@app.route("/read")
60+
def read():
61+
selected = []
62+
for subreddit in subreddits:
63+
if subreddit in request.args:
64+
selected.append(subreddit)
65+
posts = aggregate_subreddits(selected)
66+
posts.sort(key=lambda post: post['votes'], reverse=True)
67+
return render_template("read.html", selected=selected, posts=posts)
68+
69+
70+
@app.route("/add",methods=['POST'])
71+
def add():
72+
to_add = request.form.get('new-subreddit',None)
73+
if to_add:
74+
if "/" not in to_add:
75+
exists = check_subreddit(to_add)
76+
if exists:
77+
subreddits.append(to_add)
78+
return redirect("/")
79+
else:
80+
error = "That subreddit does not exist."
81+
else:
82+
error = "Write the name without /r/"
83+
else:
84+
error = "Write a subreddit."
85+
return render_template("add-failed.html", error=error)
86+
87+
88+
3389
app.run(host="0.0.0.0")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>
6+
Reddit Reader
7+
</title>
8+
<link href="https://andybrewer.github.io/mvp/mvp.css" rel="stylesheet"></link>
9+
</head>
10+
11+
<body>
12+
<header>
13+
<h1>Reddit Reader</h1>
14+
<h3>Couldn't add subreddit</h3>
15+
</header>
16+
<main>
17+
<h4>{{error}}</h4>
18+
<a href="/">&larr; Go back</a>
19+
</main>
20+
</body>
21+
22+
</html>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>
6+
Reddit Reader
7+
</title>
8+
<link href="https://andybrewer.github.io/mvp/mvp.css" rel="stylesheet"></link>
9+
</head>
10+
11+
<body>
12+
<header>
13+
<h1>Reddit Reader</h1>
14+
<h3>A service to aggregate all your favourite subreddits</h3>
15+
</header>
16+
<main>
17+
<article>
18+
<form action="/add" method="post">
19+
<h4>Add a subreddit:</h4>
20+
<h5>Write the name without /r/</h5>
21+
<input placeholder="Write a subreddit name" required name="new-subreddit" />
22+
<button type="submit">Add</button>
23+
</form>
24+
</article>
25+
<hr />
26+
<article>
27+
<form action="/read">
28+
<h4>Select the subreddits you're interested on:</h4>
29+
<ul>
30+
{% for subreddit in subreddits %}
31+
<li>
32+
<input type="checkbox" name="{{subreddit}}" id="{{subreddit}}" />
33+
<label for="{{subreddit}}">
34+
r/{{subreddit}}
35+
</label>
36+
</li>
37+
{% endfor %}
38+
</ul>
39+
<button type="submit">Aggregate</button>
40+
</form>
41+
</article>
42+
</main>
43+
</body>
44+
45+
</html>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>
6+
Reddit Reader
7+
</title>
8+
<link href="https://andybrewer.github.io/mvp/mvp.css" rel="stylesheet"></link>
9+
</head>
10+
11+
<body>
12+
<header>
13+
<h1><a href="/">Reddit Reader</a></h1>
14+
<h3>Reading: {% for subreddit in selected%}r/{{subreddit}} {% endfor %} </h3>
15+
</header>
16+
<main>
17+
{% for post in posts %}
18+
<div>
19+
<h3><a href="https://reddit.com{{post.link}}" target="_blank">{{post.title}}</a></h3>
20+
<h4>{{post.votes}} upvotes · r/{{post.subreddit}}</h4>
21+
<hr />
22+
</div>
23+
{% endfor %}
24+
</main>
25+
</body>
26+
27+
</html>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
4+
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'}
5+
6+
7+
def extract_post(html, subreddit):
8+
votes = html.find("div", {"class":"_1rZYMD_4xY3gRcSS3p8ODO"})
9+
if votes:
10+
votes = votes.string
11+
title = html.find("h3", {"class":"_eYtD2XCVieq6emjKBH3m"})
12+
if title:
13+
title = title.string
14+
link = html.find("a", {"class":"SQnoC3ObvgnGjWt90zD9Z _2INHSNB8V5eaWp4P0rY_mE"})
15+
if link:
16+
link = link['href']
17+
if votes and title and link:
18+
return {'votes':int(votes), 'title':title, 'link':link, 'subreddit':subreddit}
19+
else:
20+
return None
21+
22+
def scrape_subreddit(subreddit):
23+
all_posts = []
24+
try:
25+
url = f"https://www.reddit.com/r/{subreddit}/top/?t=month"
26+
request = requests.get(url, headers=headers)
27+
soup = BeautifulSoup(request.text, "html.parser")
28+
post_container = soup.find("div", {"class":"rpBJOHq2PR60pnwJlUyP0"})
29+
if post_container:
30+
posts = post_container.find_all("div", {"class": None}, recursive=False)
31+
for post in posts:
32+
exctracted_post = extract_post(post, subreddit)
33+
if exctracted_post:
34+
all_posts.append(exctracted_post)
35+
except Exception:
36+
pass
37+
return all_posts
38+
39+
def aggregate_subreddits(subreddits):
40+
aggregated = []
41+
for subreddit in subreddits:
42+
posts = scrape_subreddit(subreddit)
43+
aggregated = aggregated + posts
44+
return aggregated
45+
46+
47+
48+
def check_subreddit(to_check):
49+
try:
50+
check_request = requests.get(f"https://reddit.com/r/{to_check}", headers=headers)
51+
if check_request.status_code == 200:
52+
return True
53+
else:
54+
return False
55+
except Exception as e:
56+
return False

0 commit comments

Comments
 (0)