From b96e3a0acb6df2c0da1bb08a46f1ebe1c9842f56 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Wed, 27 Oct 2021 11:02:14 -0600 Subject: [PATCH] Make base search url a member of the request class Since the request class is loaded prior to values being read from the user's dotenv, the WHOOGLE_RESULT_PER_PAGE var wasn't being used for searches. This moves the definition of the base search url to be intialized in the request class to address this issue. Fixes #497 --- app/request.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/request.py b/app/request.py index 0c80f9b..71f4e9b 100644 --- a/app/request.py +++ b/app/request.py @@ -9,8 +9,6 @@ import os from stem import Signal, SocketError from stem.control import Controller -SEARCH_URL = 'https://www.google.com/search?gbv=1&num=' + str( - os.getenv('WHOOGLE_RESULTS_PER_PAGE', 10)) + '&q=' MAPS_URL = 'https://maps.google.com/maps' AUTOCOMPLETE_URL = ('https://suggestqueries.google.com/' 'complete/search?client=toolbar&') @@ -151,6 +149,8 @@ class Request: """ def __init__(self, normal_ua, root_path, config: Config): + self.search_url = 'https://www.google.com/search?gbv=1&num=' + str( + os.getenv('WHOOGLE_RESULTS_PER_PAGE', 10)) + '&q=' # Send heartbeat to Tor, used in determining if the user can or cannot # enable Tor for future requests send_tor_signal(Signal.HEARTBEAT) @@ -224,7 +224,7 @@ class Request: return [_.attrib['data'] for _ in root.findall('.//suggestion/[@data]')] - def send(self, base_url=SEARCH_URL, query='', attempt=0, + def send(self, base_url='', query='', attempt=0, force_mobile=False) -> Response: """Sends an outbound request to a URL. Optionally sends the request using Tor, if enabled by the user. @@ -285,7 +285,7 @@ class Request: disable=True) response = requests.get( - base_url + query, + (base_url or self.search_url) + query, proxies=self.proxies, headers=headers, cookies=cookies) @@ -295,6 +295,6 @@ class Request: attempt += 1 if attempt > 10: raise TorError("Tor query failed -- max attempts exceeded 10") - return self.send(base_url, query, attempt) + return self.send((base_url or self.search_url), query, attempt) return response