Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUP-956: Limit API request to avoid 500 #71

Merged
merged 6 commits into from
Nov 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions tap_facebook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,30 @@ class AdCreative(Stream):

def __iter__(self):
@retry_pattern(backoff.expo, FacebookRequestError, max_tries=5, factor=5)
def do_request():
def do_request(params):
return self.account.get_ad_creatives(fields=self.fields(), # pylint: disable=no-member
params={'limit': RESULT_RETURN_LIMIT})
ad_creative = do_request()
for a in ad_creative: # pylint: disable=invalid-name
yield {'record': a.export_all_data()}
params={'limit': RESULT_RETURN_LIMIT, 'time_range': params})

# `time_range` wants a value in the shape of {"since":
# "YYYY-MM-DD", "until": "YYYY-MM-DD"}, which is what
# pendulum.parse().date() returns
date_window_start = pendulum.parse(CONFIG['start_date']).date()
KAllan357 marked this conversation as resolved.
Show resolved Hide resolved
date_window_end = date_window_start.add(days=1)
end_date = TODAY.date()

while date_window_end <= end_date:

params = {
'since' : str(date_window_start),
'until' : str(date_window_end)
}
ad_creative = do_request(params)

for a in ad_creative: # pylint: disable=invalid-name
yield {'record': a.export_all_data()}

date_window_start = date_window_start.add(days=1)
date_window_end = date_window_start.add(days=1)


class Ads(IncrementalStream):
Expand Down Expand Up @@ -658,6 +676,9 @@ def main_impl():

CONFIG.update(args.config)

global RESULT_RETURN_LIMIT
RESULT_RETURN_LIMIT = CONFIG.get('result_return_limit', RESULT_RETURN_LIMIT)
KAllan357 marked this conversation as resolved.
Show resolved Hide resolved

FacebookAdsApi.init(access_token=access_token)
user = fb_user.User(fbid='me')
accounts = user.get_ad_accounts()
Expand Down