Ejemplo n.º 1
0
    def execute(self, context: Dict):
        service = FacebookAdsReportingHook(
            facebook_conn_id=self.facebook_conn_id,
            api_version=self.api_version)
        rows = service.bulk_facebook_report(params=self.params,
                                            fields=self.fields)

        converted_rows = [dict(row) for row in rows]
        self.log.info("Facebook Returned %s data points", len(converted_rows))

        if converted_rows:
            headers = converted_rows[0].keys()
            with tempfile.NamedTemporaryFile("w", suffix=".csv") as csvfile:
                writer = csv.DictWriter(csvfile, fieldnames=headers)
                writer.writeheader()
                writer.writerows(converted_rows)
                csvfile.flush()
                hook = GCSHook(
                    gcp_conn_id=self.gcp_conn_id,
                    impersonation_chain=self.impersonation_chain,
                )
                hook.upload(
                    bucket_name=self.bucket_name,
                    object_name=self.object_name,
                    filename=csvfile.name,
                    gzip=self.gzip,
                )
                self.log.info("%s uploaded to GCS", csvfile.name)
Ejemplo n.º 2
0
    def execute(self, context: 'Context'):
        service = FacebookAdsReportingHook(
            facebook_conn_id=self.facebook_conn_id, api_version=self.api_version
        )
        bulk_report = service.bulk_facebook_report(params=self.parameters, fields=self.fields)

        if isinstance(bulk_report, list):
            converted_rows_with_action = self._generate_rows_with_action(False)
            converted_rows_with_action = self._prepare_rows_for_upload(
                rows=bulk_report, converted_rows_with_action=converted_rows_with_action, account_id=None
            )
        elif isinstance(bulk_report, dict):
            converted_rows_with_action = self._generate_rows_with_action(True)
            for account_id in bulk_report.keys():
                rows = bulk_report.get(account_id, [])
                if rows:
                    converted_rows_with_action = self._prepare_rows_for_upload(
                        rows=rows,
                        converted_rows_with_action=converted_rows_with_action,
                        account_id=account_id,
                    )
                else:
                    self.log.warning("account_id: %s returned empty report", str(account_id))
        else:
            message = (
                "Facebook Ads Hook returned different type than expected. Expected return types should be "
                "List or Dict. Actual return type of the Hook: " + str(type(bulk_report))
            )
            raise AirflowException(message)
        total_row_count = self._decide_and_flush(converted_rows_with_action=converted_rows_with_action)
        self.log.info("Facebook Returned %s data points in total: ", total_row_count)
Ejemplo n.º 3
0
def mock_hook():
    with mock.patch("airflow.hooks.base.BaseHook.get_connection") as conn:
        hook = FacebookAdsReportingHook(api_version=API_VERSION)
        conn.return_value.extra_dejson = EXTRAS
        yield hook