|
4 | 4 | from loading_sdk.settings import ( |
5 | 5 | API_URL, |
6 | 6 | API_VERSION, |
| 7 | + FORUM_CATEGORIES, |
7 | 8 | EDITORIAL_POST_TYPES, |
8 | 9 | EDITORIAL_SORT, |
9 | 10 | USER_AGENT, |
@@ -481,3 +482,60 @@ def get_socials(self): |
481 | 482 | return {"code": 404, "message": "No results found", "data": None} |
482 | 483 |
|
483 | 484 | return {"code": 200, "message": "OK", "data": data} |
| 485 | + |
| 486 | + def get_total_thread_pages(self): |
| 487 | + pass |
| 488 | + |
| 489 | + def get_total_category_pages(self, category): |
| 490 | + if category not in FORUM_CATEGORIES: |
| 491 | + return {"code": 404, "message": "Invalid category", "data": None} |
| 492 | + |
| 493 | + working_page = None |
| 494 | + current_page = 1 |
| 495 | + url = f"{API_URL}/{API_VERSION}/posts/" |
| 496 | + headers = { |
| 497 | + "User-Agent": USER_AGENT, |
| 498 | + "page": str(current_page), |
| 499 | + category: category, |
| 500 | + } |
| 501 | + |
| 502 | + if category == "texts": |
| 503 | + headers["post-type"] = "neRegular" |
| 504 | + |
| 505 | + # Double current page until no results are returned |
| 506 | + # then we know all pages after that won't work either. |
| 507 | + while True: |
| 508 | + headers["page"] = str(current_page) |
| 509 | + response = requests.get(url, headers=headers, timeout=10) |
| 510 | + data = response.json() |
| 511 | + |
| 512 | + if not data["posts"]: |
| 513 | + break |
| 514 | + |
| 515 | + working_page = current_page |
| 516 | + current_page *= 2 |
| 517 | + |
| 518 | + # Check the page in the middle of highest known working page and |
| 519 | + # current page until they have the same page number. |
| 520 | + while True: |
| 521 | + page = working_page + (current_page - working_page) / 2 |
| 522 | + headers["page"] = str(page) |
| 523 | + |
| 524 | + response = requests.get(url, headers=headers, timeout=10) |
| 525 | + data = response.json() |
| 526 | + |
| 527 | + if data["posts"]: |
| 528 | + working_page = page |
| 529 | + else: |
| 530 | + current_page = page |
| 531 | + |
| 532 | + if math.floor(current_page) == math.floor(working_page): |
| 533 | + break |
| 534 | + |
| 535 | + total_pages = math.floor(working_page) |
| 536 | + |
| 537 | + return { |
| 538 | + "code": 200, |
| 539 | + "message": "OK", |
| 540 | + "data": {"total_pages": total_pages}, |
| 541 | + } |
0 commit comments