File: file.py

package info (click to toggle)
python-scrapy 2.13.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,648 kB
  • sloc: python: 51,968; xml: 199; makefile: 25; sh: 7
file content (24 lines) | stat: -rw-r--r-- 672 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

from w3lib.url import file_uri_to_path

from scrapy.responsetypes import responsetypes
from scrapy.utils.decorators import defers

if TYPE_CHECKING:
    from scrapy import Request, Spider
    from scrapy.http import Response


class FileDownloadHandler:
    lazy = False

    @defers
    def download_request(self, request: Request, spider: Spider) -> Response:
        filepath = file_uri_to_path(request.url)
        body = Path(filepath).read_bytes()
        respcls = responsetypes.from_args(filename=filepath, body=body)
        return respcls(url=request.url, body=body)