Ejemplo n.º 1
0
    def populate_from_scope_entity(cls, scope_entity: AssetScope,
                                   sweep_id: str):
        asset_scope = JobScope.namespace
        tokens = list(scope_entity.platform_tokens)

        try:
            manager = PageTokenManager(asset_scope, sweep_id)
            with PlatformApiContext(tokens[0]) as fb_ctx:
                request = FacebookRequest(node_id='me',
                                          method='GET',
                                          endpoint='/accounts',
                                          api=fb_ctx.api,
                                          api_type='NODE')
                request.add_params({'limit': DEFAULT_PAGE_ACCESS_TOKEN_LIMIT})
                cnt = 0
                while True:
                    # I assume that there's a better way to do paginate over this,
                    # but I wasn't able to find the corresponding target class in SDK :/
                    response = request.execute()
                    response_json = response.json()
                    for page in response_json['data']:
                        manager.add(page['id'], page['access_token'])
                        cnt += 1

                    if 'next' in response_json['paging']:
                        request._path = response_json['paging']['next']
                    else:
                        break

                logger.warning(
                    f'Loaded {cnt} page tokens for scope "{scope_entity.scope}"'
                )
        except Exception as ex:
            print(ex)
            logger.warning(
                'Fetching page tokens has failed so organic data jobs will not work in this sweep'
            )