Skip to content

Commit ecd5f2d

Browse files
authored
Merge pull request #63 from RyouMon/fix-file-download-exception
Fix FileException when downloading media. (caused by OffsiteMiddleware)
2 parents 6e828df + bc5ed0f commit ecd5f2d

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/favorites_crawler/pipelines.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class BasePipeline(FilesPipeline):
2323
def get_media_requests(self, item, info):
2424
item_dict = ItemAdapter(item).asdict()
2525
referer = item_dict.get('referer')
26-
return (Request(url, headers={'referer': referer}) for url in item_dict.get(self.files_urls_field, ()))
26+
return (
27+
Request(url, headers={'referer': referer}, dont_filter=True)
28+
for url in item_dict.get(self.files_urls_field, ())
29+
)
2730

2831
def file_path(self, request, response=None, info=None, *, item=None):
2932
return item.get_filepath(request.url, info.spider)

tests/test_pipelines.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ class TestBasePipeline:
1111
def test_should_set_referer_when_get_media_requests(self, mock_request):
1212
mock_item = {'referer': sentinel.referer, 'file_urls': [getattr(sentinel, f'url-{i}') for i in range(1, 10)]}
1313

14-
list(BasePipeline('mock_path').get_media_requests(mock_item, None))
14+
list(BasePipeline('mock_path', crawler=None).get_media_requests(mock_item, None))
1515

1616
calls = [
17-
call(getattr(sentinel, f'url-{i}'), headers={'referer': sentinel.referer})
17+
call(getattr(sentinel, f'url-{i}'), headers={'referer': sentinel.referer}, dont_filter=True)
1818
for i in range(1, 10)
1919
]
2020
mock_request.assert_has_calls(calls, any_order=True)
@@ -24,7 +24,7 @@ def test_file_path_should_call_item_get_filepath(self):
2424
mock_info = MagicMock()
2525
mock_item = MagicMock()
2626

27-
BasePipeline('mock_path').file_path(mock_request, None, mock_info, item=mock_item)
27+
BasePipeline('mock_path', crawler=None).file_path(mock_request, None, mock_info, item=mock_item)
2828

2929
mock_item.get_filepath.assert_called_once_with(mock_request.url, mock_info.spider)
3030

@@ -36,11 +36,11 @@ def test_process_item_should_drop_item_when_cbz_file_already_exist(self, tmp_pat
3636
mock_item.get_folder_name.return_value = 'abc'
3737

3838
with pytest.raises(DropItem):
39-
ComicPipeline(str(tmp_path)).process_item(mock_item, None)
39+
ComicPipeline(str(tmp_path), crawler=None).process_item(mock_item, None)
4040

4141
@patch('favorites_crawler.pipelines.create_comic_archive')
4242
def test_should_create_comic_archive_when_close_spider(self, mock_create_comic_archive, tmp_path):
43-
pipeline = ComicPipeline('mock_path')
43+
pipeline = ComicPipeline('mock_path', crawler=None)
4444
pipeline.files_path = tmp_path
4545
(tmp_path / 'comic').mkdir()
4646
pipeline.comic_comments = {'comic': b'comment'}
@@ -51,7 +51,7 @@ def test_should_create_comic_archive_when_close_spider(self, mock_create_comic_a
5151

5252
@patch('favorites_crawler.pipelines.create_comic_archive')
5353
def test_should_not_create_comic_archive_when_comic_comments_is_empty(self, mock_create_comic_archive, tmp_path):
54-
pipeline = ComicPipeline('mock_path')
54+
pipeline = ComicPipeline('mock_path', crawler=None)
5555
pipeline.comic_comments = {}
5656

5757
pipeline.close_spider(None)

0 commit comments

Comments
 (0)