Skip to content

Commit 8c44249

Browse files
committed
Make async version for get_total_category_pages
1 parent 607cd85 commit 8c44249

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

loading_sdk/async_api/client.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
API_VERSION,
77
EDITORIAL_POST_TYPES,
88
EDITORIAL_SORT,
9+
FORUM_CATEGORIES,
10+
POSTS_PER_PAGE,
911
USER_AGENT,
1012
)
1113
from loading_sdk.async_api.extractors import extract_data
@@ -517,3 +519,70 @@ async def get_socials(self):
517519
return {"code": 404, "message": "No results found", "data": None}
518520

519521
return {"code": 200, "message": "OK", "data": data}
522+
523+
async def get_total_thread_pages(self, thread_id):
524+
"""Returns total pages of a thread.
525+
526+
:param thread_id: Unique thread id
527+
:type thread_id: str
528+
:rtype: dict
529+
"""
530+
pass
531+
532+
async def get_total_category_pages(self, category):
533+
"""Returns total pages of a forum category.
534+
535+
:param category: Category name. Can be games, other, or texts
536+
:type category: str
537+
:rtype: dict
538+
"""
539+
540+
if category not in FORUM_CATEGORIES:
541+
return {"code": 404, "message": "Invalid category", "data": None}
542+
543+
working_page = None
544+
current_page = 1
545+
url = f"{API_URL}/{API_VERSION}/posts/"
546+
headers = {
547+
"User-Agent": USER_AGENT,
548+
"page": str(current_page),
549+
category: category,
550+
}
551+
552+
if category == "texts":
553+
headers["post-type"] = "neRegular"
554+
555+
async with aiohttp.ClientSession() as session:
556+
# Double current page until no results are returned
557+
# then we know all pages after that won't work either.
558+
while True:
559+
headers["page"] = str(current_page)
560+
async with session.get(url, headers=headers) as response:
561+
data = await response.json()
562+
563+
if not data["posts"]:
564+
break
565+
566+
working_page = current_page
567+
current_page *= 2
568+
569+
# Check the page in the middle of highest known working page and
570+
# current page until they have the same page number.
571+
while True:
572+
page = working_page + (current_page - working_page) / 2
573+
headers["page"] = str(page)
574+
575+
async with session.get(url, headers=headers) as response:
576+
data = await response.json()
577+
578+
if data["posts"]:
579+
working_page = page
580+
else:
581+
current_page = page
582+
583+
if math.floor(current_page) == math.floor(working_page):
584+
break
585+
586+
total_pages = math.floor(working_page)
587+
588+
return total_pages

0 commit comments

Comments
 (0)