Skip to content

Commit 4c0a688

Browse files
committed
refactor(page_info): improve code readability and error handling
1 parent 064cfd4 commit 4c0a688

1 file changed

Lines changed: 64 additions & 41 deletions

File tree

facebook_page_scraper/page_info.py

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ def scrape(self) -> Optional[Dict[str, Optional[str]]]:
5757
html_content, "profile_tile_items"
5858
)
5959
self.profile_info = self.extract_profile_info(profile_info_json)
60-
60+
6161
self.meta_html_info = self.extract_html_data(html_content)
6262

6363
# Combine both into one dictionary
6464
if self.general_info and self.profile_info:
65-
combined_info = {**self.general_info, **self.meta_html_info, **self.profile_info}
65+
combined_info = {
66+
**self.general_info,
67+
**self.meta_html_info,
68+
**self.profile_info,
69+
}
6670
return combined_info
6771
elif self.general_info:
6872
return self.general_info
@@ -88,8 +92,8 @@ def extract_general_info(self, json_data: dict) -> Dict[str, Optional[str]]:
8892
"cover_photo": None,
8993
"page_likes": None,
9094
"page_followers": None,
91-
"page_id" : None,
92-
"is_business_page" : None
95+
"page_id": None,
96+
"is_business_page": None,
9397
}
9498

9599
try:
@@ -110,34 +114,50 @@ def extract_general_info(self, json_data: dict) -> Dict[str, Optional[str]]:
110114

111115
general_info["page_name"] = user.get("name")
112116
general_info["page_url"] = user.get("url")
113-
114-
general_info["page_id"] = user.get("delegate_page", {}).get("id")
115-
116-
general_info["is_business_page"] = user.get("delegate_page", {}).get("is_business_page_active")
117+
118+
delegate_page = user.get("delegate_page")
119+
if delegate_page is not None:
120+
general_info["page_id"] = delegate_page.get("id")
121+
general_info["is_business_page"] = delegate_page.get(
122+
"is_business_page_active"
123+
)
117124

118125
general_info["profile_pic"] = (
119126
user.get("profilePicLarge", {}).get("uri")
120127
or user.get("profilePicMedium", {}).get("uri")
121128
or user.get("profilePicSmall", {}).get("uri")
122129
)
123-
124-
general_info["cover_photo"] = user.get("cover_photo", {}).get("photo", {}).get("image",{}).get("uri")
125-
126-
profile_social_contents = user.get(
127-
"profile_social_context", {}
128-
).get("content", [])
129-
for content in profile_social_contents:
130-
uri = content.get("uri", "")
131-
text = content.get("text", {}).get("text")
132-
if "friends_likes" in uri and not general_info["page_likes"]:
133-
general_info["page_likes"] = text
134-
elif "followers" in uri and not general_info["page_followers"]:
135-
general_info["page_followers"] = text
136-
if (
137-
general_info["page_likes"]
138-
and general_info["page_followers"]
139-
):
140-
break
130+
131+
general_info["cover_photo"] = (
132+
user.get("cover_photo", {})
133+
.get("photo", {})
134+
.get("image", {})
135+
.get("uri")
136+
)
137+
profile_social_context = user.get("profile_social_context")
138+
if profile_social_context is not None:
139+
140+
profile_social_contents = profile_social_context.get(
141+
"content", []
142+
)
143+
for content in profile_social_contents:
144+
uri = content.get("uri", "")
145+
text = content.get("text", {}).get("text")
146+
if (
147+
"friends_likes" in uri
148+
and not general_info["page_likes"]
149+
):
150+
general_info["page_likes"] = text
151+
elif (
152+
"followers" in uri
153+
and not general_info["page_followers"]
154+
):
155+
general_info["page_followers"] = text
156+
if (
157+
general_info["page_likes"]
158+
and general_info["page_followers"]
159+
):
160+
break
141161
return general_info
142162
except (IndexError, KeyError, TypeError, ValueError) as e:
143163
print(f"Error extracting general page information: {e}")
@@ -221,27 +241,31 @@ def extract_profile_info(self, json_data: dict) -> Dict[str, Optional[str]]:
221241
return profile_info
222242

223243
def extract_html_data(self, html_content: HTMLParser) -> Dict[str, Optional[str]]:
224-
"""Extracts the JSON data from the HTML content.
225-
244+
"""Extracts the JSON data from the HTML content.
245+
226246
Args:
227247
html_content (str): The raw HTML content of the page.
228-
248+
229249
Returns:
230250
dict: A dictionary with the extracted JSON data.
231251
"""
232252
meta_data = {
233-
"page_likes_count": None,
234-
"page_talking_count": None,
235-
"page_were_here_count": None,
236-
}
237-
253+
"page_likes_count": None,
254+
"page_talking_count": None,
255+
"page_were_here_count": None,
256+
}
257+
238258
try:
239259

240-
meta_description = html_content.css_first("meta[name=description]").attrs.get("content") if html_content.css_first("meta[name=description]") else None
241-
260+
meta_description = (
261+
html_content.css_first("meta[name=description]").attrs.get("content")
262+
if html_content.css_first("meta[name=description]")
263+
else None
264+
)
265+
242266
if not meta_description:
243267
return meta_data
244-
268+
245269
like_pattern = r"(?P<likes>[\d,]+)\s+likes"
246270
like_match = re.search(like_pattern, meta_description)
247271
likes = like_match.group("likes") if like_match else None
@@ -257,12 +281,11 @@ def extract_html_data(self, html_content: HTMLParser) -> Dict[str, Optional[str]
257281
meta_data["page_likes_count"] = likes
258282
meta_data["page_talking_count"] = talking
259283
meta_data["page_were_here_count"] = were
260-
284+
261285
return meta_data
262-
286+
263287
except Exception as e:
264-
print(
265-
f"Unexpected error in (extract_html_data) func: {e}")
288+
print(f"Unexpected error in (extract_html_data) func: {e}")
266289
return meta_data
267290

268291
@classmethod

0 commit comments

Comments
 (0)