Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (
response.status_code == 200
and "application/json" in response.headers["Content-Type"]
or "application/javascript" in response.headers["Content-Type"]
or "text/javascript" in response.headers["Content-Type"]
):
# trim initial characters
# some responses start with garbage characters, like ")]}',"
# these have to be cleaned before being passed to the json parser
content = response.text[trim_chars:]
# parse json
self.GetNewProxy()
return json.loads(content)
else:
# error
raise exceptions.ResponseError(
"The request failed: Google returned a "
"response with code {0}.".format(response.status_code),
response=response,
)
def _fetch_data(pytrends, build_payload, timeframe: str) -> pd.DataFrame:
"""Attempts to fecth data and retries in case of a ResponseError."""
attempts, fetched = 0, False
while not fetched:
try:
build_payload(timeframe=timeframe)
except ResponseError as err:
print(err)
print(f'Trying again in {60 + 5 * attempts} seconds.')
sleep(60 + 5 * attempts)
attempts += 1
if attempts > 3:
print('Failed after 3 attemps, abort fetching.')
break
else:
fetched = True
return pytrends.interest_over_time()