forked from redstone-dev/Voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
120 lines (98 loc) · 3.38 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
from flask import Flask, render_template, request, jsonify
import requests
import bs4
from utils import r_category_ids
app = Flask("Voyager")
@app.get("/")
def home():
return render_template("home.html")
@app.get("/v1/forum/category/topics/<category>/<page>")
def topic_list(category, page):
args = request.args.to_dict()
try:
id = r_category_ids()[category]
except KeyError:
return (
jsonify(
{
"error": "CategoryNotFoundError",
"message": "Valid categories are: "
+ str(list(r_category_ids().keys())),
}
),
400,
)
response = requests.get(
f"https://scratch.mit.edu/discuss/{id}?page={page}", timeout=10
)
soup = bs4.BeautifulSoup(response.content, "html.parser")
msg = soup.find("td", class_="djangobbcon1")
if msg and msg.text.strip() == "Forum is empty.":
return (
jsonify(
{
"error": "NoMoreTopicsError",
"message": "There are no more topics on that page.",
}
),
400,
)
# Create an empty list to store topic information
topics = []
# Find all the post elements
post_elements = soup.find_all("h3", class_="topic_isread")
closed_post_divs = soup.find_all("div", class_="iclosed")
sticky_post_divs = soup.find_all("div", class_="isticky")
closed_posts = []
sticky_posts = []
for closed_div in closed_post_divs:
post_row = closed_div.find_parent("tr")
# Extract the post URL
post_url = post_row.find("h3").a["href"]
# Extract the post ID from the URL (assuming it's the last part after the last '/')
post_id = post_url.split("/")[-2]
# Append the post ID to the list of closed posts
if closed_div:
closed_posts.append(post_id)
for sticky_div in sticky_post_divs:
post_row = sticky_div.find_parent("tr")
# Extract the post URL
post_url = post_row.find("h3").a["href"]
# Extract the post ID from the URL
post_id = post_url.split("/")[-2]
# Append the post ID to the list of sticky posts
if sticky_div:
sticky_posts.append(post_id)
def is_closed(post_id):
if post_id in closed_posts:
return "1"
else:
return "0"
def is_sticky(post_id):
if post_id in sticky_posts:
return "1"
else:
return "0"
# Loop through each post element and extract the relevant information
for post_element in post_elements:
post_id = post_element.a["href"].split("/")[-2]
post_title = post_element.a.get_text()
post_author = post_element.find_next_sibling(
"span", class_="byuser"
).get_text()[3:]
post_category = category
# Append the extracted information to the topics list as a dictionary
topics.append(
{
"id": post_id,
"title": post_title,
"author": post_author,
"category": post_category,
"closed": is_closed(post_id),
"sticky": is_sticky(post_id),
}
)
# Return the topics list as a JSON response
return jsonify(topics)
if __name__ == "__main__":
app.run()