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 Jun 10, 2022
1 parent 706975c commit 72be67c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
python -m pip install flake8 pytest pytest-timeout
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
python -m pip install -e .
- name: Lint with flake8
Expand Down
15 changes: 6 additions & 9 deletions pytrends/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,12 @@ def GetGoogleCookie(self):
"""
while True:
if "proxies" in self.requests_args:
try:
return dict(filter(lambda i: i[0] == 'NID', requests.get(
'https://trends.google.com/?geo={geo}'.format(
geo=self.hl[-2:]),
timeout=self.timeout,
**self.requests_args
).cookies.items()))
except:
continue
return dict(filter(lambda i: i[0] == 'NID', requests.get(
'https://trends.google.com/?geo={geo}'.format(
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
10 changes: 10 additions & 0 deletions pytrends/test_trendReq.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest import TestCase
import pandas.api.types as ptypes
import pytest
from requests.exceptions import ProxyError

from pytrends.request import TrendReq

Expand Down Expand Up @@ -116,3 +118,11 @@ def test_ispartial_dtype_timeframe_all(self):
timeframe='all')
df = pytrend.interest_over_time()
assert ptypes.is_bool_dtype(df.isPartial)

@pytest.mark.timeout(3, method="thread")
def test_dead_requests_args_proxy_does_not_loop_infinitely(self):
with pytest.raises(ProxyError, match='Cannot connect to proxy'):
proxies = {'https': 'non-existent-proxy.example.com:3333'}
_ = TrendReq(requests_args={
'proxies': proxies
})

0 comments on commit 72be67c

Please sign in to comment.