Skip to content

Commit

Permalink
chore: order dunder methods better
Browse files Browse the repository at this point in the history
  • Loading branch information
ajskateboarder committed Jan 15, 2024
1 parent fed0561 commit 71c49d0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
6 changes: 0 additions & 6 deletions src/crawling/ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,3 @@ def get_bestsellers_ids(self, scrolls: int = 1) -> list[Product]:
def get_query_ids(self, query: str, scrolls: int = 1) -> list[Product]:
"""Fetch product IDs from a Amazon search query"""
return self._get_ids(f"https://amazon.com/s?k={quote_plus(query)}", scrolls)

def __enter__(self) -> Self:
return self

def __exit__(self, *_: Any) -> None:
self.browser.quit()
12 changes: 6 additions & 6 deletions src/crawling/sync_reviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def __init__(self, fake_display: bool = True) -> None:

self.browser = Firefox(options=opts)

def __enter__(self) -> AmazonScraper:
return self

def __exit__(self, *_: Any) -> None:
self.close()

def get_bestselling(self) -> Generator[str, None, None]:
"""Fetch product IDs from Amazon's Bestsellers page"""
self.browser.get("https://www.amazon.com/gp/bestsellers/")
Expand Down Expand Up @@ -109,9 +115,3 @@ def select_reviews(content: Any) -> Generator[Review, None, None]:
def close(self) -> None:
"""Close the browser"""
self.browser.quit()

def __enter__(self) -> AmazonScraper:
return self

def __exit__(self, *_: Any) -> None:
self.close()
13 changes: 6 additions & 7 deletions src/crawling/threaded_reviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def __init__(self, fake_display: bool = True) -> None:

self.captcha_handler = self.handle_captcha

def __enter__(self) -> Self:
return self

def __exit__(self, *_: Any) -> None:
self.close()

def handle_captcha(self, future: Future, browsers: list) -> None:
"""Default CAPTCHA handler"""
try:
Expand Down Expand Up @@ -192,10 +198,3 @@ def close(self) -> None:
with ThreadPoolExecutor() as executor:
for browser in self.browsers:
executor.submit(browser.quit)

# these methods are practically useless at this point
def __enter__(self) -> Self:
return self

def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
self.close()

0 comments on commit 71c49d0

Please sign in to comment.