Skip to content

Commit d25b860

Browse files
committed
Add method that counds total pages that exists in a forum category
1 parent 60b065e commit d25b860

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

loading_sdk/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
API_VERSION = "v1"
44
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"
55
EDITORIAL_SORT = ["title"]
6+
FORUM_CATEGORIES = ["games", "other", "texts"]
67
EDITORIAL_POST_TYPES = [
78
"neRegular",
89
"review",

loading_sdk/sync_api/client.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from loading_sdk.settings import (
55
API_URL,
66
API_VERSION,
7+
FORUM_CATEGORIES,
78
EDITORIAL_POST_TYPES,
89
EDITORIAL_SORT,
910
USER_AGENT,
@@ -481,3 +482,60 @@ def get_socials(self):
481482
return {"code": 404, "message": "No results found", "data": None}
482483

483484
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

Comments
 (0)