Skip to content

Commit

Permalink
avoid infinite loop when dead proxy is passed via requests_args
Browse files Browse the repository at this point in the history
  • Loading branch information
avant1 committed Mar 10, 2023
1 parent d2ad592 commit 37b4494
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions pytrends/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,19 @@ def __init__(self, hl='en-US', tz=360, geo='', timeout=(2, 5), proxies='',

self.headers = {'accept-language': self.hl}
self.headers.update(self.requests_args.pop('headers', {}))

def GetGoogleCookie(self):
"""
Gets google cookie (used for each and every proxy; once on init otherwise)
Removes proxy from the list on proxy error
"""
while True:
if "proxies" in self.requests_args:
try:
return dict(filter(lambda i: i[0] == 'NID', requests.get(
f'{BASE_TRENDS_URL}/?geo={self.hl[-2:]}',
timeout=self.timeout,
**self.requests_args
).cookies.items()))
except:
continue
return dict(filter(lambda i: i[0] == 'NID', requests.get(
f'{BASE_TRENDS_URL}/?geo={self.hl[-2:]}',
timeout=self.timeout,
**self.requests_args
).cookies.items()))
else:
if len(self.proxies) > 0:
proxy = {'https': self.proxies[self.proxy_index]}
Expand Down Expand Up @@ -177,7 +174,7 @@ def build_payload(self, kw_list, cat=0, timeframe='today 5-y', geo='',
for index, kw in enumerate(self.kw_list):
keyword_payload = {'keyword': kw, 'time': timeframe[index], 'geo': self.geo}
self.token_payload['req']['comparisonItem'].append(keyword_payload)
else:
else:
# build out json for each keyword with
for kw in self.kw_list:
keyword_payload = {'keyword': kw, 'time': timeframe, 'geo': self.geo}
Expand Down Expand Up @@ -298,9 +295,9 @@ def multirange_interest_over_time(self):
# Split dictionary columns into seperate ones
for i, column in enumerate(result_df.columns):
result_df["[" + str(i) + "] " + str(self.kw_list[i]) + " date"] = result_df[i].apply(pd.Series)["formattedTime"]
result_df["[" + str(i) + "] " + str(self.kw_list[i]) + " value"] = result_df[i].apply(pd.Series)["value"]
result_df["[" + str(i) + "] " + str(self.kw_list[i]) + " value"] = result_df[i].apply(pd.Series)["value"]
result_df = result_df.drop([i], axis=1)

# Adds a row with the averages at the top of the dataframe
avg_row = {}
for i, avg in enumerate(req_json['default']['averages']):
Expand All @@ -310,7 +307,7 @@ def multirange_interest_over_time(self):
result_df.loc[-1] = avg_row
result_df.index = result_df.index + 1
result_df = result_df.sort_index()

return result_df


Expand Down

0 comments on commit 37b4494

Please sign in to comment.