feat: Add get_post_metrics to all publishers for analytics
- Add abstract get_post_metrics() method to BasePublisher - Implement get_post_metrics() in XPublisher using Twitter API v2 - Returns: likes, comments, shares, retweets, quotes, impressions - Implement get_post_metrics() in ThreadsPublisher using Meta Graph API - Implement get_post_metrics() in FacebookPublisher with insights - Implement get_post_metrics() in InstagramPublisher with insights This enables the fetch_post_metrics task to collect engagement data from all platforms, populating the analytics dashboard. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -250,3 +250,46 @@ class FacebookPublisher(BasePublisher):
|
||||
|
||||
except httpx.HTTPError:
|
||||
return False
|
||||
|
||||
async def get_post_metrics(self, post_id: str) -> Optional[Dict]:
|
||||
"""Obtener métricas de un post de Facebook."""
|
||||
if not self.access_token:
|
||||
return None
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
url = f"{self.base_url}/{post_id}"
|
||||
params = {
|
||||
"fields": "likes.summary(true),comments.summary(true),shares,insights.metric(post_impressions,post_engaged_users)",
|
||||
"access_token": self.access_token
|
||||
}
|
||||
|
||||
response = await client.get(url, params=params)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
# Extraer métricas
|
||||
likes = data.get("likes", {}).get("summary", {}).get("total_count", 0)
|
||||
comments = data.get("comments", {}).get("summary", {}).get("total_count", 0)
|
||||
shares = data.get("shares", {}).get("count", 0)
|
||||
|
||||
# Insights (pueden no estar disponibles)
|
||||
impressions = 0
|
||||
reach = 0
|
||||
insights = data.get("insights", {}).get("data", [])
|
||||
for insight in insights:
|
||||
if insight.get("name") == "post_impressions":
|
||||
impressions = insight.get("values", [{}])[0].get("value", 0)
|
||||
elif insight.get("name") == "post_engaged_users":
|
||||
reach = insight.get("values", [{}])[0].get("value", 0)
|
||||
|
||||
return {
|
||||
"likes": likes,
|
||||
"comments": comments,
|
||||
"shares": shares,
|
||||
"impressions": impressions,
|
||||
"reach": reach,
|
||||
}
|
||||
|
||||
except httpx.HTTPError:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user