Skip to content

Commit

Permalink
bugfix: ConnectionError
Browse files Browse the repository at this point in the history
Signed-off-by: Hari-Nagarajan <[email protected]>
  • Loading branch information
Hari-Nagarajan committed Sep 22, 2020
1 parent 9fd88dd commit ee6de84
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
55 changes: 30 additions & 25 deletions stores/nvidia.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import requests
from chromedriver_py import binary_path # this will get you the path variable
from furl import furl
from requests.exceptions import Timeout
from requests.packages.urllib3.util.retry import Retry
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException
Expand Down Expand Up @@ -327,31 +328,35 @@ def run_items(self):
self.get_product_ids()
self.run_items()

def buy(self, product_id, delay=3):
log.info(f"Checking stock for {product_id} at {delay} second intervals.")
while not self.add_to_cart(product_id) and self.enabled:
self.attempt = self.attempt + 1
time_delta = str(datetime.now() - self.started_at).split(".")[0]
with Spinner.get(
f"Still working (attempt {self.attempt}, have been running for {time_delta})..."
) as s:
sleep(delay)
if self.enabled:
self.apply_shopper_details()
if self.auto_buy_enabled:
self.notification_handler.send_notification(
f" {self.gpu_long_name} with product ID: {product_id} available!"
)
log.info("Auto buy enabled.")
# self.submit_cart()
self.selenium_checkout()
else:
log.info("Auto buy disabled.")
cart_url = self.open_cart_url()
self.notification_handler.send_notification(
f" {self.gpu_long_name} with product ID: {product_id} in stock: {cart_url}"
)
self.enabled = False
def buy(self, product_id, delay=5):
try:
log.info(f"Checking stock for {product_id} at {delay} second intervals.")
while not self.add_to_cart(product_id) and self.enabled:
self.attempt = self.attempt + 1
time_delta = str(datetime.now() - self.started_at).split(".")[0]
with Spinner.get(
f"Still working (attempt {self.attempt}, have been running for {time_delta})..."
) as s:
sleep(delay)
if self.enabled:
self.apply_shopper_details()
if self.auto_buy_enabled:
self.notification_handler.send_notification(
f" {self.gpu_long_name} with product ID: {product_id} available!"
)
log.info("Auto buy enabled.")
# self.submit_cart()
self.selenium_checkout()
else:
log.info("Auto buy disabled.")
cart_url = self.open_cart_url()
self.notification_handler.send_notification(
f" {self.gpu_long_name} with product ID: {product_id} in stock: {cart_url}"
)
self.enabled = False
except Timeout:
log.error("Had a timeout error.")
self.buy(product_id)

def open_cart_url(self):
log.info("Opening cart.")
Expand Down
8 changes: 7 additions & 1 deletion utils/http.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

DEFAULT_TIMEOUT = 5 # seconds


class TimeoutHTTPAdapter(HTTPAdapter):
def __init__(self, *args, **kwargs):
self.timeout = DEFAULT_TIMEOUT
super().__init__(*args, **kwargs)
super().__init__(max_retries=Retry(
total=10,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504],
method_whitelist=["HEAD", "GET", "OPTIONS"],
))

def send(self, request, **kwargs):
timeout = kwargs.get("timeout")
Expand Down

0 comments on commit ee6de84

Please sign in to comment.