-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: crawler and add future annotations someplaces
- Loading branch information
1 parent
71c49d0
commit dd9ff49
Showing
11 changed files
with
129 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
"""Amazon review collection utilities""" | ||
from .ids import BestSellersLinks | ||
from .sync_reviews import AmazonScraper | ||
from .threaded_reviews import AmazonScraper as ParallelAmazonScraper | ||
from .generator import bestsellers_reviews |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Review generation helpers""" | ||
from __future__ import annotations | ||
from typing import Callable, Protocol, Any, cast, TYPE_CHECKING | ||
import subprocess | ||
import logging | ||
|
||
from .sync_reviews import AmazonScraper | ||
from .threaded_reviews import AmazonScraper as ParallelAmazonScraper | ||
|
||
if TYPE_CHECKING: | ||
from selenium.webdriver import Firefox | ||
|
||
|
||
class Scraper(Protocol): | ||
def __call__(self, email: str, password: str) -> None: | ||
... | ||
|
||
|
||
def kitty_captcha(browser: Firefox, _: Any) -> str: | ||
captcha_image = cast( | ||
str, | ||
browser.find_element("css selector", "img[alt='captcha']").get_attribute("src"), | ||
) | ||
subprocess.run(["/usr/bin/kitty", "icat", captcha_image], check=True) | ||
return input("(login) Please solve the provided captcha: ") | ||
|
||
|
||
def bestsellers_reviews(callback: Callable, headless: bool) -> Scraper: | ||
"""Returns a scraping function to scrape reviews from Amazon's bestselling""" | ||
|
||
def scraper(email: str, password: str) -> None: | ||
logging.info("Starting product ID gatherer") | ||
with AmazonScraper(headless) as products: | ||
logging.info("Collecting product IDs") | ||
product_ids = products.get_bestselling() | ||
logging.info("Collected following IDs: %s", ",".join(product_ids)) | ||
logging.info("Initializing review gatherer") | ||
with AmazonScraper(headless) as prop: | ||
with ParallelAmazonScraper(headless) as scrapers: | ||
scrapers.captcha_hook = kitty_captcha | ||
logging.info("Logging scrapers in") | ||
scrapers.login(email, password) | ||
|
||
for product_id in product_ids: | ||
logging.info("Initiating scrape process for: %s", product_id) | ||
logging.info("\tCollecting review proportions") | ||
proportions = prop.get_proportions(product_id) | ||
logging.info("\tScraping") | ||
scrapers.scrape(product_id, callback, proportions) # type: ignore | ||
|
||
return scraper |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.