Skip to content

Commit 25ab9d8

Browse files
committed
Add get_thread method that only returns data when it is a thread start
1 parent d76ea2f commit 25ab9d8

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

loading_api_wrapper/api.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,40 @@ def get_post(self, post_id):
8181
}
8282

8383
return response.json()
84+
85+
def get_thread(self, thread_id):
86+
"""Returns post data if the id belongs to a thread start."""
87+
88+
if not thread_id:
89+
return {"code": 404, "message": '"thread_id" is not allowed to be empty'}
90+
91+
url = f"{API_URL}/{API_VERSION}/posts/{thread_id}"
92+
headers = {"User-Agent": USER_AGENT}
93+
response = requests.get(url, headers=headers)
94+
95+
if response.status_code == 200:
96+
post_data = response.json()
97+
parent_post = post_data["posts"][-1]
98+
parent_user = None
99+
100+
# Only thread starts has a title so anything else is a regular post.
101+
if "title" in parent_post:
102+
for user in post_data["users"]:
103+
if user["id"] == parent_post["userId"]:
104+
parent_user = user
105+
break
106+
107+
return {
108+
"code": response.status_code,
109+
"post": {
110+
"posts": [parent_post],
111+
"users": [parent_user],
112+
},
113+
}
114+
else:
115+
return {
116+
"code": response.status_code,
117+
"message": "Exists, but was not a thread id",
118+
}
119+
120+
return response.json()

0 commit comments

Comments
 (0)