Ejemplo n.º 1
0
    def _get_insights(self, story: IGMedia) -> MutableMapping[str, Any]:
        """Get insights for specific story"""

        # Story IG Media object metrics with values less than 5 will return an error code 10 with the message (#10)
        # Not enough viewers for the media to show insights.
        try:
            insights = story.get_insights(params={"metric": self.metrics})
            return {
                record["name"]: record["values"][0]["value"]
                for record in insights
            }
        except FacebookRequestError as error:
            if error.api_error_code() == 10:
                self.logger.error(
                    f"Insights error: {error.api_error_message()}")
                return {}
            raise error
Ejemplo n.º 2
0
    def read_records(
        self,
        sync_mode: SyncMode,
        cursor_field: List[str] = None,
        stream_slice: Mapping[str, Any] = None,
        stream_state: Mapping[str, Any] = None,
    ) -> Iterable[Mapping[str, Any]]:
        account = stream_slice["account"]
        ig_account = account["instagram_business_account"]
        stories = ig_account.get_stories(params=self.request_params(),
                                         fields=[])
        for ig_story in stories:
            insights = self._get_insights(IGMedia(ig_story["id"]))
            if not insights:
                continue

            insights["id"] = ig_story["id"]
            insights["page_id"] = account["page_id"]
            insights["business_account_id"] = ig_account["id"]
            yield self.transform(insights)
Ejemplo n.º 3
0
 def _get_children(self, ids: List):
     children_fields = list(
         set(self.fields) - set(self.INVALID_CHILDREN_FIELDS))
     for pk in ids:
         yield self.transform(
             IGMedia(pk).api_get(fields=children_fields).export_all_data())
Ejemplo n.º 4
0
 def _get_single_record(self,
                        media_id: str,
                        fields: Sequence[str] = None) -> IGMedia:
     return IGMedia(media_id).api_get(fields=fields)