I am trying to fetch all questions of the Swift tag from Stack Overflow. I created the query using https://api.stackexchange.com/docs/questions and wrote a simple wrapper in Python.
def fetch_question(page_size = 1):
api_res = f"https://api.stackexchange.com/2.3/questions?page={int(page_size)}&pagesize=100&order=desc&sort=activity&tagged=swift&site=Stackoverflow&filter=!*MZqiH2lVtmP*wBz"
response = requests.get(api_res, verify = False)
return response.json()
def get_all_questions():
page_count = 1
api_init = fetch_question(page_count)
page_count+=1
has_more = api_init['has_more']
quota_max = api_init['quota_max']
quota_remain = api_init['quota_remaining']
total = api_init['total']
items_count = 100
while has_more:
if quota_remain == 1:
"""If exhausted quota then sleep for one day
to reset the quota limit"""
time.sleep(3600)
elif 'backoff' in api_init:
"""Respect API backoff time"""
backoff = int(api_init['backoff'])
time.sleep(backoff+1)
else:
api_init = fetch_question(page_count)
has_more = api_init['has_more']
quota_remain = api_init['quota_remaining']
count_+=100
page_count+=1
"""One-second break after each api call to not overload requests"""
time.sleep(1)
return {'total_fetched' : items_count,
'api_total' : total}
Is this the right way to fetch all questions with the Swift tag? Or can I improve it?