Ejemplo n.º 1
0
def main():
    args = singer.utils.parse_args([])

    account_ids = args.config.get("account_ids", [])
    access_token = args.config.get("access_token") or os.environ.get(
        "FACEBOOK_ACCESS_TOKEN")

    if not access_token:
        raise ValueError(
            f"missing required config 'access_token' or environment 'FACEBOOK_ACCESS_TOKEN'"
        )

    # use
    api = FacebookAdsApi.init(access_token=access_token, timeout=10)

    user = User("me", api=api)
    ad_accounts = cast(List[AdAccount],
                       user.get_ad_accounts(fields=["account_id", "id"]))

    all_account_ids = {
        accnt["account_id"]: accnt["id"]
        for accnt in ad_accounts
    }
    accnt_ids = []

    for account_id in account_ids:
        if account_id not in all_account_ids:
            logger.warn(
                f"invalid account id: {account_id} not in list of valid account ids: {all_account_ids.keys()}"
            )
            continue

        accnt_ids.append(all_account_ids[account_id])

    do_sync(accnt_ids, args.config, args.state)
Ejemplo n.º 2
0
    def extract(self, short=False):
        # todo adset and ad

        me = User("me")
        accounts = list(me.get_ad_accounts(self.accs_fields))
        if short:
            accounts = [accounts[2]]
            self.insights_params["time_range"]["since"] = "2020-07-17"
            self.insights_params["time_range"]["until"] = "2020-07-23"
        campaigns = []
        for ele in accounts:
            campaigns.extend(list(ele.get_campaigns(self.camps_fields)))

        insights = {}
        for e in campaigns:
            insights[e["id"]] = list(
                e.get_insights(
                    self.insights_fields,
                    self.insights_params,
                    is_async=False,
                ))

        if self.year_start != self.year_n_week_start:
            params2 = dict(
                time_increment=7,
                time_range=dict(
                    since=self.year_start,
                    until=self.year_n_week_start,
                ),
            )
            for e in campaigns:
                insights[e["id"]] += list(
                    e.get_insights(
                        self.insights_fields,
                        params2,
                        is_async=False,
                    ))

        accounts = {ele["id"][4:]: ele["name"] for ele in accounts}
        campaigns = {ele["id"]: dict(ele) for ele in campaigns}

        ans = [
            dict(
                **campaigns[k],
                account_name=accounts[campaigns[k]["account_id"]],
                data=[dict(ele) for ele in v],
            ) for k, v in insights.items() if len(v) > 0
        ]

        Extract.write("../exports/FB_extracted.p", ans)
        return ans
from facebook_business.adobjects.user import User
from facebook_business.api import FacebookAdsApi

app_id = 'COLOCAR_AQUI_SEU_ID_DE_APP'
app_secret = 'COLOCAR_AQUI_SUA_CHAVE_SECRETA'
access_token = 'COLOCAR_AQUI_SEU_TOKEN_DE_ACESSO'

FacebookAdsApi.init(app_id, app_secret, access_token)

me = User(fbid='me')
my_accounts = list(me.get_ad_accounts())
print(my_accounts)
Ejemplo n.º 4
0
 def __init__(self):
     me = User(fbid='me')
     self.getaccountlist = list(me.get_ad_accounts())