-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bump version * Added spider logging * First draft of progress bar, docs still missing * Add progress bar doc * Fix progress bar log mess up * Reduce IL scraped items
- Loading branch information
Showing
11 changed files
with
390 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""Module for Scrapy extension.""" | ||
|
||
|
||
import logging | ||
|
||
from collections import defaultdict | ||
|
||
from scrapy import signals | ||
from scrapy.exceptions import NotConfigured, CloseSpider | ||
|
||
from tqdm.auto import tqdm | ||
|
||
from finscraper.utils import TqdmLogger | ||
|
||
|
||
class ProgressBar: | ||
|
||
def __init__(self, crawler): | ||
self.progress_bar_enabled = ( | ||
crawler.settings.get('PROGRESS_BAR_ENABLED', False)) | ||
self.closespider_itemcount = ( | ||
crawler.settings.get('CLOSESPIDER_ITEMCOUNT', None)) | ||
self.counter = defaultdict(int) | ||
|
||
if not self.progress_bar_enabled: | ||
raise NotConfigured | ||
|
||
crawler.signals.connect(self.on_response, | ||
signal=signals.response_received) | ||
crawler.signals.connect(self.on_item_scraped, | ||
signal=signals.item_scraped) | ||
crawler.signals.connect(self.on_error, signal=signals.spider_error) | ||
|
||
logger = logging.getLogger() | ||
self.progress_bar = tqdm(desc='Progress', unit=' items', | ||
total=self.closespider_itemcount, | ||
file=TqdmLogger(logger)) | ||
self.itemcount = 0 | ||
self.pagecount = 0 | ||
|
||
@classmethod | ||
def from_crawler(cls, crawler): | ||
return cls(crawler) | ||
|
||
def on_error(self, failure, response, spider): | ||
self.counter['errorcount'] += 1 | ||
self.progress_bar.set_postfix({ | ||
'pages': self.counter['pagecount'], | ||
'errors': self.counter['errorcount'] | ||
}) | ||
|
||
def on_response(self, response, request, spider): | ||
self.counter['pagecount'] += 1 | ||
self.progress_bar.set_postfix({ | ||
'pages': self.counter['pagecount'], | ||
'errors': self.counter['errorcount'] | ||
}) | ||
|
||
def on_item_scraped(self, item, spider): | ||
self.counter['itemcount'] += 1 | ||
self.progress_bar.update() |
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
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
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
Oops, something went wrong.