From 6392f87b21d92458e0c189acbea5fe0f7c7b6f1c Mon Sep 17 00:00:00 2001 From: DIGITALCRIMINAL <89371864+DIGITALCRIMINALS@users.noreply.github.com> Date: Sat, 16 Oct 2021 05:01:24 +0100 Subject: [PATCH] Fixed KeyError: 'author' error It was due to posts being reported. --- apis/api_helper.py | 8 ++++---- apis/onlyfans/classes/create_post.py | 2 +- apis/onlyfans/classes/create_user.py | 26 +++++++++++++++++++------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/apis/api_helper.py b/apis/api_helper.py index 686638a..12b0f50 100644 --- a/apis/api_helper.py +++ b/apis/api_helper.py @@ -370,8 +370,8 @@ def restore_missing_data(master_set2, media_set, split_by): return new_set -async def scrape_endpoint_links(links, session_manager: session_manager, api_type): - media_set = [] +async def scrape_endpoint_links(links:list[str], session_manager: Union[session_manager,None], api_type:str): + media_set:list[dict[str,str]] = [] max_attempts = 100 api_type = api_type.capitalize() for attempt in list(range(max_attempts)): @@ -404,8 +404,8 @@ async def scrape_endpoint_links(links, session_manager: session_manager, api_typ else: media_set.extend(not_faulty) break - media_set = list(chain(*media_set)) - return media_set + final_media_set = list(chain(*media_set)) + return final_media_set def calculate_the_unpredictable(link, limit, multiplier=1): diff --git a/apis/onlyfans/classes/create_post.py b/apis/onlyfans/classes/create_post.py index 6f27ed1..3bf85f9 100644 --- a/apis/onlyfans/classes/create_post.py +++ b/apis/onlyfans/classes/create_post.py @@ -10,7 +10,7 @@ def __init__(self, option, user) -> None: self.postedAt: str = option.get("postedAt") self.postedAtPrecise: str = option.get("postedAtPrecise") self.expiredAt: Any = option.get("expiredAt") - self.author = create_user.create_user(option["author"]) + self.author = create_user.create_user(option.get("author", {})) self.text: str = option.get("text") self.rawText: str = option.get("rawText") self.lockedText: bool = option.get("lockedText") diff --git a/apis/onlyfans/classes/create_user.py b/apis/onlyfans/classes/create_user.py index 8e9a48a..e7d645d 100644 --- a/apis/onlyfans/classes/create_user.py +++ b/apis/onlyfans/classes/create_user.py @@ -229,7 +229,9 @@ def is_me(self) -> bool: status = True return status - async def get_stories(self, refresh=True, limit=100, offset=0) -> list[create_story]: + async def get_stories( + self, refresh=True, limit=100, offset=0 + ) -> list[create_story]: api_type = "stories" if not refresh: result = handle_refresh(self, api_type) @@ -251,7 +253,7 @@ async def get_stories(self, refresh=True, limit=100, offset=0) -> list[create_st async def get_highlights( self, identifier="", refresh=True, limit=100, offset=0, hightlight_id="" - ) -> Union[list[create_highlight] , list[create_story]]: + ) -> Union[list[create_highlight], list[create_story]]: api_type = "highlights" if not refresh: result = handle_refresh(self, api_type) @@ -275,7 +277,7 @@ async def get_highlights( return results async def get_posts( - self, links: Optional[list] = None, limit=10, offset=0, refresh=True + self, links: Optional[list[str]] = None, limit=10, offset=0, refresh=True ) -> Optional[list[create_post]]: api_type = "posts" if not refresh: @@ -299,9 +301,7 @@ async def get_posts( results = await api_helper.scrape_endpoint_links( links, self.session_manager, api_type ) - # Filter out posts that are reported by the user as these will not have content - filtered_results = list(filter(lambda opt: not ("isReportedByMe" in opt and opt["isReportedByMe"]), results)) - final_results = [create_post(x, self) for x in filtered_results] + final_results = self.finalize_content_set(results) self.temp_scraped.Posts = final_results return final_results @@ -427,7 +427,8 @@ async def get_archived_posts( results = await api_helper.scrape_endpoint_links( links, self.session_manager, api_type ) - final_results = [create_post(x, self) for x in results if x] + final_results = self.finalize_content_set(results) + self.temp_scraped.Archived.Posts = final_results return final_results @@ -516,3 +517,14 @@ async def buy_subscription(self): def set_scraped(self, name, scraped): setattr(self.scraped, name, scraped) + def finalize_content_set(self,results:list[dict[str,str]]): + final_results:list[create_post] = [] + for result in results: + content_type = result["responseType"] + match content_type: + case "post": + created = create_post(result,self) + final_results.append(created) + case _: + print + return final_results \ No newline at end of file