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

feat: add support for multiple proxies #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions search_engines/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
FAKE_USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; rv:84.0) Gecko/20100101 Firefox/84.0'

## Proxy server
# Example: PROXY = "1.1.1.3:8080,http://1.2.3.4:8080"
PROXY = None

## TOR proxy server
Expand Down
4 changes: 3 additions & 1 deletion search_engines/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ def search(self, query, pages=cfg.SEARCH_ENGINE_RESULTS_PAGES):
try:
response = self._get_page(request['url'], request['data'])
if not self._is_ok(response):
break
msg = f"google status not ok: {response.http}"
raise Exception(msg)

tags = BeautifulSoup(response.html, "html.parser")
items = self._filter_results(tags)
self._collect_results(items)
Expand Down
4 changes: 4 additions & 0 deletions search_engines/http_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
import random
from collections import namedtuple

from .config import TIMEOUT, PROXY, USER_AGENT
Expand Down Expand Up @@ -45,6 +46,9 @@ def _quote(self, url):
def _set_proxy(self, proxy):
'''Returns HTTP or SOCKS proxies dictionary.'''
if proxy:
if "," in proxy:
proxys = [x if utl.is_url(x) else f"http://{x}" for x in proxy.split(",") if x]
proxy = random.choice(proxys)
if not utl.is_url(proxy):
raise ValueError('Invalid proxy format!')
proxy = {'http':proxy, 'https':proxy}
Expand Down