Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/facebook/facebook_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ def get_total_reach(
) -> List[Tuple[datetime, int]]:
"""
Get statistics on the total reach of new posts.
NOTE: 'page_posts_impressions_unique' is deprecated in v19.0.
Using 'page_impressions_unique' (Total Page Reach) as replacement.
"""
batches = self._get_all_batches(
connection_name="insights",
metric="page_posts_impressions_unique",
metric="page_impressions_unique",
period=period.value,
since=since,
until=until,
Expand All @@ -91,7 +93,7 @@ def get_organic_reach(
"""
batches = self._get_all_batches(
connection_name="insights",
metric="page_posts_impressions_organic_unique",
metric="page_impressions_organic_unique",
period=period.value,
since=since,
until=until,
Expand Down Expand Up @@ -127,7 +129,7 @@ def get_new_fan_count(
"""
batches = self._get_all_batches(
connection_name="insights",
metric="page_fan_adds_unique",
metric="page_daily_follows_unique",
period=period.value,
since=since,
until=until,
Expand All @@ -147,7 +149,12 @@ def _get_all_batches(
}
params.update(kwargs)
page = self._make_graph_api_call(f"{self._page_id}/{connection_name}", params)
result += page["data"]

if "error" in page:
logger.error(f"Error fetching {connection_name}: {page['error']}")
return result

result += page.get("data", [])
# process next
result += self._iterate_over_pages(connection_name, since, until, page, True)
# process previous
Expand Down Expand Up @@ -192,10 +199,16 @@ def _iterate_over_pages(
current_page = self._make_graph_api_call(
f"{self._page_id}/{connection_name}", args
)
if "error" in current_page:
logger.error(
f"Error in pagination for {connection_name}: {current_page['error']}"
)
break

if "data" in current_page:
result += current_page["data"]
else:
logger.error(f"Error in pagination: {current_page}")
logger.warning(f"No data found in pagination response: {current_page}")
break
return result

Expand Down
Loading