|
| 1 | +from concurrent.futures import thread |
1 | 2 | import math |
| 3 | +from pydoc import resolve |
2 | 4 |
|
3 | 5 | import requests |
4 | 6 |
|
@@ -203,3 +205,73 @@ def get_editorials(self, page=None, post_type=None, sort=None): |
203 | 205 | return {"code": 404, "post": data} |
204 | 206 |
|
205 | 207 | return {"code": 200, "post": data} |
| 208 | + |
| 209 | + def create_post(self, thread_id, message): |
| 210 | + if not thread_id: |
| 211 | + return {"code": 400, "message": '"thread_id" is not allowed to be empty'} |
| 212 | + |
| 213 | + url = f"{API_URL}/{API_VERSION}/posts/{thread_id}" |
| 214 | + headers = { |
| 215 | + "User-Agent": USER_AGENT, |
| 216 | + "content-type": "application/x-www-form-urlencoded", |
| 217 | + } |
| 218 | + data = {"body": message} |
| 219 | + response = requests.post( |
| 220 | + url, |
| 221 | + headers=headers, |
| 222 | + data=data, |
| 223 | + cookies=self._cookies, |
| 224 | + ) |
| 225 | + |
| 226 | + # Has no auth token. |
| 227 | + if response.status_code == 401: |
| 228 | + return response.json() |
| 229 | + |
| 230 | + # Post id doesn't exist. |
| 231 | + if response.status_code == 404: |
| 232 | + return response.json() |
| 233 | + |
| 234 | + if response.status_code == 201: |
| 235 | + return { |
| 236 | + "code": response.status_code, |
| 237 | + "message": "Post created", |
| 238 | + "data": response.json(), |
| 239 | + } |
| 240 | + |
| 241 | + # Handle any other unknown status code. |
| 242 | + return response.json() |
| 243 | + |
| 244 | + def edit_post(self, post_id, message): |
| 245 | + if not message: |
| 246 | + return {"code": 400, "message": '"message" is not allowed to be empty'} |
| 247 | + |
| 248 | + url = f"{API_URL}/{API_VERSION}/posts/{post_id}" |
| 249 | + headers = { |
| 250 | + "User-Agent": USER_AGENT, |
| 251 | + "content-type": "application/x-www-form-urlencoded", |
| 252 | + } |
| 253 | + data = {"body": message} |
| 254 | + response = requests.patch( |
| 255 | + url, |
| 256 | + headers=headers, |
| 257 | + data=data, |
| 258 | + cookies=self._cookies, |
| 259 | + ) |
| 260 | + |
| 261 | + # Has no auth token. |
| 262 | + if response.status_code == 401: |
| 263 | + return response.json() |
| 264 | + |
| 265 | + # Post id doesn't exist. |
| 266 | + if response.status_code == 404: |
| 267 | + return response.json() |
| 268 | + |
| 269 | + if response.status_code == 200: |
| 270 | + return { |
| 271 | + "code": response.status_code, |
| 272 | + "message": "Post updated", |
| 273 | + "data": response.json(), |
| 274 | + } |
| 275 | + |
| 276 | + # Handle any other unknown status code. |
| 277 | + return response.json() |
0 commit comments