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

avoid infinite loop when dead proxy is passed via requests_args #519

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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