-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from scrapinghub/batched-create-requests
batch oriented request creation in crawling strategy
- Loading branch information
Showing
3 changed files
with
74 additions
and
6 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
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,58 @@ | ||
# -*- coding: utf-8 -*- | ||
from frontera.worker.strategies import BaseCrawlingStrategy | ||
from frontera.worker.strategy import StatesContext | ||
from frontera.settings import Settings | ||
from tests.mocks.frontier_manager import FakeFrontierManager | ||
|
||
from frontera.contrib.backends.memory import MemoryStates | ||
from frontera.core.components import States | ||
|
||
|
||
class TestingCrawlingStrategy(BaseCrawlingStrategy): | ||
def add_seeds(self, seeds): | ||
pass | ||
|
||
def page_crawled(self, response): | ||
pass | ||
|
||
def page_error(self, request, error): | ||
pass | ||
|
||
def links_extracted(self, request, links): | ||
pass | ||
|
||
|
||
class MessageBusStream(object): | ||
def send(self, request, score=1.0, dont_queue=False): | ||
pass | ||
|
||
def flush(self): | ||
pass | ||
|
||
|
||
class TestCrawlingStrategy(object): | ||
def strategy(self): | ||
settings = Settings() | ||
manager = FakeFrontierManager(settings) | ||
stream = MessageBusStream() | ||
states = MemoryStates(10) | ||
states_ctx = StatesContext(states) | ||
return TestingCrawlingStrategy.from_worker(manager, stream, states_ctx) | ||
|
||
def test_create_request(self): | ||
s = self.strategy() | ||
req = s.create_request("http://test.com/someurl") | ||
assert req.meta[b'fingerprint'] == b'955ac04f1b1a96de60a5139ad90c80be87822159' | ||
|
||
def test_states_refresh(self): | ||
s = self.strategy() | ||
states = s._states_context._states | ||
url = "http://test.com/someurl" | ||
req1 = s.create_request(url) | ||
req1.meta[b'state'] = States.CRAWLED | ||
states.update_cache(req1) | ||
|
||
req2 = s.create_request(url) | ||
s.refresh_states([req2]) | ||
assert req2.meta[b'state'] == req1.meta[b'state'] | ||
assert req2.meta[b'state'] == States.CRAWLED |