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: resend request if fails #22

Merged
merged 5 commits into from
Dec 21, 2023
Merged
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
83 changes: 70 additions & 13 deletions dags/github/github_api_helpers/smart_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,82 @@
import random

import requests
from requests import Response


def get(url: str, params=None):
class UniqueRandomNumbers:
_instance = None

def __new__(cls):
if cls._instance is None:
cls._instance = super(UniqueRandomNumbers, cls).__new__(cls)
return cls._instance

def __init__(self):
self.original_numbers = list(range(20001, 29981))
self.numbers = self.original_numbers.copy()
random.shuffle(self.numbers)
self.counter = 0

def get_unique_number(self):
# ! This is a sample code to limit the number of requests per specific time period
# if self.counter >= 3:
# time.sleep(60)
# self.counter = 0

if not self.numbers:
self.reset()

self.counter += 1
return self.numbers.pop()

def reset(self):
self.numbers = self.original_numbers.copy()
random.shuffle(self.numbers)


def get(url: str, params=None) -> Response:
"""
Sends a GET request With Smart Proxy.
Sends a GET request with Smart Proxy and retries up to 10 times if necessary.

:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param **kwargs: Optional arguments that ``request`` takes.
:param params: (optional) Dictionary, list of tuples or bytes to send in the query string for the :class:`Request`.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""

random_port = random.randint(20001, 29980)
proxy_url = f"http://spusfxy185:[email protected]:{random_port}"
logging.info(f"Using proxy {proxy_url}")
proxies = {
"http": proxy_url,
"https": proxy_url,
}
return requests.get(url=url, params=params, proxies=proxies)
urn = UniqueRandomNumbers()
max_attempts = 10
attempt = 0

while attempt < max_attempts:
random_port = urn.get_unique_number()
proxy_url = f"http://spusfxy185:[email protected]:{random_port}"
logging.info(f"Attempt {attempt + 1}: Using proxy {proxy_url}")
proxies = {
"http": proxy_url,
"https": proxy_url,
}

try:
response = requests.get(url=url, params=params, proxies=proxies)

if response.status_code == 200:
return response
else:
logging.error(
f"Failed to get {url} with status code {response.status_code}"
)
logging.error(f"Response: {response.text}")

except requests.exceptions.HTTPError as http_err:
logging.error(
f"HTTP error occurred، Failed to get {url} with error: {http_err}"
)
except Exception as err:
logging.error(f"Some error occurred during getting {url}, error: {err}")

attempt += 1

logging.error(f"All attempts failed for URL: {url}")
raise Exception(f"All attempts failed for URL: {url}")