raspbytes 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- raspbytes/__init__.py +87 -0
- raspbytes/_core/__init__.py +1 -0
- raspbytes/_core/constants.py +38 -0
- raspbytes/_core/http.py +160 -0
- raspbytes/_core/transport.py +175 -0
- raspbytes/_core/types.py +11 -0
- raspbytes/_core/version.py +8 -0
- raspbytes/client.py +88 -0
- raspbytes/config.py +122 -0
- raspbytes/countries.py +278 -0
- raspbytes/exceptions.py +115 -0
- raspbytes/managed_scrapers.py +4 -0
- raspbytes/models.py +89 -0
- raspbytes/py.typed +1 -0
- raspbytes/resources/__init__.py +17 -0
- raspbytes/resources/_base.py +75 -0
- raspbytes/resources/_polling.py +73 -0
- raspbytes/resources/browser.py +185 -0
- raspbytes/resources/proxies.py +49 -0
- raspbytes/resources/scraper/__init__.py +45 -0
- raspbytes/resources/scraper/_generated/__init__.py +1 -0
- raspbytes/resources/scraper/_generated/managed.py +190 -0
- raspbytes/resources/scraper/_generated/wire.py +632 -0
- raspbytes/resources/scraper/_managed.py +14 -0
- raspbytes/resources/scraper/_request.py +128 -0
- raspbytes/resources/scraper/builder.py +199 -0
- raspbytes/resources/scraper/hub.py +168 -0
- raspbytes/resources/scraper/monitoring.py +132 -0
- raspbytes/resources/scraper/runs.py +202 -0
- raspbytes/resources/scraper/schedules.py +172 -0
- raspbytes/resources/scraper/storage.py +155 -0
- raspbytes/resources/scraper/tasks.py +134 -0
- raspbytes/resources/scraper/types.py +96 -0
- raspbytes/resources/unblocker.py +131 -0
- raspbytes/scraper_types.py +4 -0
- raspbytes-1.1.0.dist-info/METADATA +297 -0
- raspbytes-1.1.0.dist-info/RECORD +39 -0
- raspbytes-1.1.0.dist-info/WHEEL +4 -0
- raspbytes-1.1.0.dist-info/licenses/LICENSE +25 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"""Scraper run submission, history, cancellation, and polling."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import AsyncIterator, Iterator, Mapping
|
|
6
|
+
from http import HTTPMethod
|
|
7
|
+
from typing import cast
|
|
8
|
+
|
|
9
|
+
from ..._core.constants import ResponseField
|
|
10
|
+
from ..._core.transport import AsyncTransport, SyncTransport
|
|
11
|
+
from ..._core.types import ResponseData
|
|
12
|
+
from ...config import ScraperConfig, _validate_polling
|
|
13
|
+
from ...exceptions import ConfigurationError, ScraperRunError
|
|
14
|
+
from .._polling import ObjectResponse, poll, poll_async
|
|
15
|
+
from ._generated.wire import ScrapeJobPage, ScrapeJobResponse, ScrapeRequest, ScraperRoute
|
|
16
|
+
from ._request import _AsyncScraperEndpoints, _continuation, _path_id, _SyncScraperEndpoints
|
|
17
|
+
|
|
18
|
+
_FAILED_STATUSES = frozenset({"failed", "cancelled", "canceled"})
|
|
19
|
+
_COMPLETED = "completed"
|
|
20
|
+
_COMPLETED_AT = "completed_at"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _run_done(run: ObjectResponse) -> bool:
|
|
24
|
+
status = run[ResponseField.STATUS]
|
|
25
|
+
completed = run.get(_COMPLETED_AT) is not None
|
|
26
|
+
error = run.get(ResponseField.ERROR)
|
|
27
|
+
if status in _FAILED_STATUSES or (completed and error is not None):
|
|
28
|
+
raise ScraperRunError(run["id"], status, run)
|
|
29
|
+
return status == _COMPLETED or completed
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _run_filters(
|
|
33
|
+
page_size: int | None,
|
|
34
|
+
scraper_id: str | None,
|
|
35
|
+
status: str | None,
|
|
36
|
+
) -> dict[str, str | int | None]:
|
|
37
|
+
if page_size is not None and page_size < 1:
|
|
38
|
+
raise ConfigurationError("page_size must be positive")
|
|
39
|
+
return {"page_size": page_size, "scraper_id": scraper_id, "status": status}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _poll_options(
|
|
43
|
+
config: ScraperConfig,
|
|
44
|
+
run_id: str,
|
|
45
|
+
timeout: float | None,
|
|
46
|
+
poll_interval: float | None,
|
|
47
|
+
) -> tuple[float, float, str]:
|
|
48
|
+
timeout = config.poll_timeout if timeout is None else timeout
|
|
49
|
+
poll_interval = config.poll_interval if poll_interval is None else poll_interval
|
|
50
|
+
_validate_polling(timeout, poll_interval)
|
|
51
|
+
return timeout, poll_interval, f"Timed out waiting for scraper run {run_id}"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class ScraperRuns(_SyncScraperEndpoints):
|
|
55
|
+
def __init__(self, transport: SyncTransport, config: ScraperConfig) -> None:
|
|
56
|
+
super().__init__(transport, config)
|
|
57
|
+
self._config = config
|
|
58
|
+
|
|
59
|
+
def submit(self, request: ScrapeRequest) -> ScrapeJobResponse:
|
|
60
|
+
"""Submit a direct extraction using a declarative extractor."""
|
|
61
|
+
return self._submit_run(ScraperRoute.SCRAPER_SUBMIT_CONVENIENCE, request)
|
|
62
|
+
|
|
63
|
+
def create(self, request: ScrapeRequest) -> ScrapeJobResponse:
|
|
64
|
+
"""Create a direct extraction in request history."""
|
|
65
|
+
return self._submit_run(ScraperRoute.SCRAPER_REQUESTS_CREATE, request)
|
|
66
|
+
|
|
67
|
+
def get(self, run_id: str) -> ScrapeJobResponse:
|
|
68
|
+
path = ScraperRoute.SCRAPER_REQUESTS_RETRIEVE.format(request_id=_path_id(run_id))
|
|
69
|
+
return self._request(HTTPMethod.GET, path, ScrapeJobResponse)
|
|
70
|
+
|
|
71
|
+
def cancel(self, run_id: str) -> ScrapeJobResponse:
|
|
72
|
+
path = ScraperRoute.SCRAPER_REQUESTS_CANCEL.format(request_id=_path_id(run_id))
|
|
73
|
+
return self._request(HTTPMethod.DELETE, path, ScrapeJobResponse, idempotent=True)
|
|
74
|
+
|
|
75
|
+
def list(
|
|
76
|
+
self,
|
|
77
|
+
*,
|
|
78
|
+
page_size: int | None = None,
|
|
79
|
+
scraper_id: str | None = None,
|
|
80
|
+
status: str | None = None,
|
|
81
|
+
) -> ScrapeJobPage:
|
|
82
|
+
return self._page(_run_filters(page_size, scraper_id, status))
|
|
83
|
+
|
|
84
|
+
def _page(self, params: Mapping[str, object]) -> ScrapeJobPage:
|
|
85
|
+
return self._request(
|
|
86
|
+
HTTPMethod.GET, ScraperRoute.SCRAPER_REQUESTS_LIST, ScrapeJobPage, params=params
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
def iterate(
|
|
90
|
+
self,
|
|
91
|
+
*,
|
|
92
|
+
page_size: int | None = None,
|
|
93
|
+
scraper_id: str | None = None,
|
|
94
|
+
status: str | None = None,
|
|
95
|
+
) -> Iterator[ScrapeJobResponse]:
|
|
96
|
+
params = _run_filters(page_size, scraper_id, status)
|
|
97
|
+
seen: set[tuple[tuple[str, str], ...]] = set()
|
|
98
|
+
page = self._page(params)
|
|
99
|
+
while True:
|
|
100
|
+
yield from page["results"]
|
|
101
|
+
if not page["next"]:
|
|
102
|
+
return
|
|
103
|
+
page = self._page({**params, **_continuation(page["next"], seen)})
|
|
104
|
+
|
|
105
|
+
def wait(
|
|
106
|
+
self,
|
|
107
|
+
run_id: str,
|
|
108
|
+
*,
|
|
109
|
+
timeout: float | None = None,
|
|
110
|
+
poll_interval: float | None = None,
|
|
111
|
+
) -> ScrapeJobResponse:
|
|
112
|
+
timeout, interval, message = _poll_options(self._config, run_id, timeout, poll_interval)
|
|
113
|
+
return cast(
|
|
114
|
+
ScrapeJobResponse,
|
|
115
|
+
poll(
|
|
116
|
+
lambda: cast(ResponseData, self.get(run_id)),
|
|
117
|
+
resource_id=run_id,
|
|
118
|
+
is_complete=_run_done,
|
|
119
|
+
failed_states=(),
|
|
120
|
+
timeout=timeout,
|
|
121
|
+
poll_interval=interval,
|
|
122
|
+
timeout_message=message,
|
|
123
|
+
),
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class AsyncScraperRuns(_AsyncScraperEndpoints):
|
|
128
|
+
def __init__(self, transport: AsyncTransport, config: ScraperConfig) -> None:
|
|
129
|
+
super().__init__(transport, config)
|
|
130
|
+
self._config = config
|
|
131
|
+
|
|
132
|
+
async def submit(self, request: ScrapeRequest) -> ScrapeJobResponse:
|
|
133
|
+
"""Submit a direct extraction using a declarative extractor."""
|
|
134
|
+
return await self._submit_run(ScraperRoute.SCRAPER_SUBMIT_CONVENIENCE, request)
|
|
135
|
+
|
|
136
|
+
async def create(self, request: ScrapeRequest) -> ScrapeJobResponse:
|
|
137
|
+
"""Create a direct extraction in request history."""
|
|
138
|
+
return await self._submit_run(ScraperRoute.SCRAPER_REQUESTS_CREATE, request)
|
|
139
|
+
|
|
140
|
+
async def get(self, run_id: str) -> ScrapeJobResponse:
|
|
141
|
+
path = ScraperRoute.SCRAPER_REQUESTS_RETRIEVE.format(request_id=_path_id(run_id))
|
|
142
|
+
return await self._request(HTTPMethod.GET, path, ScrapeJobResponse)
|
|
143
|
+
|
|
144
|
+
async def cancel(self, run_id: str) -> ScrapeJobResponse:
|
|
145
|
+
path = ScraperRoute.SCRAPER_REQUESTS_CANCEL.format(request_id=_path_id(run_id))
|
|
146
|
+
return await self._request(HTTPMethod.DELETE, path, ScrapeJobResponse, idempotent=True)
|
|
147
|
+
|
|
148
|
+
async def list(
|
|
149
|
+
self,
|
|
150
|
+
*,
|
|
151
|
+
page_size: int | None = None,
|
|
152
|
+
scraper_id: str | None = None,
|
|
153
|
+
status: str | None = None,
|
|
154
|
+
) -> ScrapeJobPage:
|
|
155
|
+
return await self._page(_run_filters(page_size, scraper_id, status))
|
|
156
|
+
|
|
157
|
+
async def _page(self, params: Mapping[str, object]) -> ScrapeJobPage:
|
|
158
|
+
return await self._request(
|
|
159
|
+
HTTPMethod.GET, ScraperRoute.SCRAPER_REQUESTS_LIST, ScrapeJobPage, params=params
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
async def iterate(
|
|
163
|
+
self,
|
|
164
|
+
*,
|
|
165
|
+
page_size: int | None = None,
|
|
166
|
+
scraper_id: str | None = None,
|
|
167
|
+
status: str | None = None,
|
|
168
|
+
) -> AsyncIterator[ScrapeJobResponse]:
|
|
169
|
+
params = _run_filters(page_size, scraper_id, status)
|
|
170
|
+
seen: set[tuple[tuple[str, str], ...]] = set()
|
|
171
|
+
page = await self._page(params)
|
|
172
|
+
while True:
|
|
173
|
+
for run in page["results"]:
|
|
174
|
+
yield run
|
|
175
|
+
if not page["next"]:
|
|
176
|
+
return
|
|
177
|
+
page = await self._page({**params, **_continuation(page["next"], seen)})
|
|
178
|
+
|
|
179
|
+
async def wait(
|
|
180
|
+
self,
|
|
181
|
+
run_id: str,
|
|
182
|
+
*,
|
|
183
|
+
timeout: float | None = None,
|
|
184
|
+
poll_interval: float | None = None,
|
|
185
|
+
) -> ScrapeJobResponse:
|
|
186
|
+
timeout, interval, message = _poll_options(self._config, run_id, timeout, poll_interval)
|
|
187
|
+
|
|
188
|
+
async def fetch() -> ResponseData:
|
|
189
|
+
return cast(ResponseData, await self.get(run_id))
|
|
190
|
+
|
|
191
|
+
return cast(
|
|
192
|
+
ScrapeJobResponse,
|
|
193
|
+
await poll_async(
|
|
194
|
+
fetch,
|
|
195
|
+
resource_id=run_id,
|
|
196
|
+
is_complete=_run_done,
|
|
197
|
+
failed_states=(),
|
|
198
|
+
timeout=timeout,
|
|
199
|
+
poll_interval=interval,
|
|
200
|
+
timeout_message=message,
|
|
201
|
+
),
|
|
202
|
+
)
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""Recurring Scraper schedules and immediate runs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import builtins
|
|
6
|
+
from collections.abc import Mapping
|
|
7
|
+
from http import HTTPMethod
|
|
8
|
+
|
|
9
|
+
from ...exceptions import ConfigurationError
|
|
10
|
+
from ._generated.wire import ScraperRoute
|
|
11
|
+
from ._request import (
|
|
12
|
+
_AsyncScraperEndpoints,
|
|
13
|
+
_path_id,
|
|
14
|
+
_SyncScraperEndpoints,
|
|
15
|
+
_with_origin_country,
|
|
16
|
+
)
|
|
17
|
+
from .types import (
|
|
18
|
+
CreateScraperScheduleParams,
|
|
19
|
+
ScraperRun,
|
|
20
|
+
ScraperSchedule,
|
|
21
|
+
UpdateScraperScheduleParams,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _validate_days(weekdays: list[int] | None, month_day: int | None) -> None:
|
|
26
|
+
if weekdays is not None:
|
|
27
|
+
if any(type(day) is not int or not 1 <= day <= 7 for day in weekdays):
|
|
28
|
+
raise ConfigurationError("weekdays must contain ISO weekday integers from 1 to 7")
|
|
29
|
+
if len(set(weekdays)) != len(weekdays):
|
|
30
|
+
raise ConfigurationError("weekdays must be unique")
|
|
31
|
+
if month_day is not None and (type(month_day) is not int or not 1 <= month_day <= 31):
|
|
32
|
+
raise ConfigurationError("month_day must be an integer from 1 to 31")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _validate_cadence(request: CreateScraperScheduleParams) -> None:
|
|
36
|
+
weekdays = request.get("weekdays")
|
|
37
|
+
month_day = request.get("month_day")
|
|
38
|
+
_validate_days(weekdays, month_day)
|
|
39
|
+
frequency = request["frequency"]
|
|
40
|
+
if frequency == "weekly" and len(weekdays or []) != 1:
|
|
41
|
+
raise ConfigurationError("weekly schedules require exactly one weekday")
|
|
42
|
+
if frequency == "custom" and not weekdays:
|
|
43
|
+
raise ConfigurationError("custom schedules require at least one weekday")
|
|
44
|
+
if frequency == "monthly" and month_day is None:
|
|
45
|
+
raise ConfigurationError("monthly schedules require month_day")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _schedule_body(request: Mapping[str, object]) -> dict[str, object]:
|
|
49
|
+
overrides = request.get("request_overrides")
|
|
50
|
+
if isinstance(overrides, Mapping):
|
|
51
|
+
return {**request, "request_overrides": _with_origin_country(overrides)}
|
|
52
|
+
return dict(request)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class ScraperSchedules(_SyncScraperEndpoints):
|
|
56
|
+
"""Recurring Scraper schedules and immediate runs."""
|
|
57
|
+
|
|
58
|
+
def list(self) -> builtins.list[ScraperSchedule]:
|
|
59
|
+
return self._request(
|
|
60
|
+
HTTPMethod.GET, ScraperRoute.SCRAPER_SCHEDULES_LIST, list[ScraperSchedule]
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
def get(self, schedule_id: str) -> ScraperSchedule:
|
|
64
|
+
return self._request(
|
|
65
|
+
HTTPMethod.GET,
|
|
66
|
+
ScraperRoute.SCRAPER_SCHEDULES_RETRIEVE.format(schedule_id=_path_id(schedule_id)),
|
|
67
|
+
ScraperSchedule,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
def create(self, request: CreateScraperScheduleParams) -> ScraperSchedule:
|
|
71
|
+
"""Create a recurring schedule for a saved task."""
|
|
72
|
+
_validate_cadence(request)
|
|
73
|
+
return self._request(
|
|
74
|
+
HTTPMethod.POST,
|
|
75
|
+
ScraperRoute.SCRAPER_SCHEDULES_CREATE,
|
|
76
|
+
ScraperSchedule,
|
|
77
|
+
body=_schedule_body(request),
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
def update(self, schedule_id: str, request: UpdateScraperScheduleParams) -> ScraperSchedule:
|
|
81
|
+
_validate_days(request.get("weekdays"), request.get("month_day"))
|
|
82
|
+
return self._request(
|
|
83
|
+
HTTPMethod.PATCH,
|
|
84
|
+
ScraperRoute.SCRAPER_SCHEDULES_UPDATE.format(schedule_id=_path_id(schedule_id)),
|
|
85
|
+
ScraperSchedule,
|
|
86
|
+
body=_schedule_body(request),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
def toggle_pause(self, schedule_id: str) -> ScraperSchedule:
|
|
90
|
+
"""Toggle the schedule's paused state using the API's pause operation."""
|
|
91
|
+
return self._request(
|
|
92
|
+
HTTPMethod.DELETE,
|
|
93
|
+
ScraperRoute.SCRAPER_SCHEDULES_PAUSE.format(schedule_id=_path_id(schedule_id)),
|
|
94
|
+
ScraperSchedule,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
def run_now(self, schedule_id: str) -> ScraperRun:
|
|
98
|
+
"""Run once without changing the next scheduled occurrence."""
|
|
99
|
+
return self._request(
|
|
100
|
+
HTTPMethod.POST,
|
|
101
|
+
ScraperRoute.SCRAPER_SCHEDULES_RUN_NOW.format(schedule_id=_path_id(schedule_id)),
|
|
102
|
+
ScraperRun,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
def pause(self, schedule_id: str) -> ScraperSchedule:
|
|
106
|
+
"""Pause future occurrences."""
|
|
107
|
+
return self.update(schedule_id, {"enabled": False})
|
|
108
|
+
|
|
109
|
+
def resume(self, schedule_id: str) -> ScraperSchedule:
|
|
110
|
+
"""Resume future occurrences."""
|
|
111
|
+
return self.update(schedule_id, {"enabled": True})
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class AsyncScraperSchedules(_AsyncScraperEndpoints):
|
|
115
|
+
"""Recurring Scraper schedules and immediate runs."""
|
|
116
|
+
|
|
117
|
+
async def list(self) -> builtins.list[ScraperSchedule]:
|
|
118
|
+
return await self._request(
|
|
119
|
+
HTTPMethod.GET, ScraperRoute.SCRAPER_SCHEDULES_LIST, list[ScraperSchedule]
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
async def get(self, schedule_id: str) -> ScraperSchedule:
|
|
123
|
+
return await self._request(
|
|
124
|
+
HTTPMethod.GET,
|
|
125
|
+
ScraperRoute.SCRAPER_SCHEDULES_RETRIEVE.format(schedule_id=_path_id(schedule_id)),
|
|
126
|
+
ScraperSchedule,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
async def create(self, request: CreateScraperScheduleParams) -> ScraperSchedule:
|
|
130
|
+
"""Create a recurring schedule for a saved task."""
|
|
131
|
+
_validate_cadence(request)
|
|
132
|
+
return await self._request(
|
|
133
|
+
HTTPMethod.POST,
|
|
134
|
+
ScraperRoute.SCRAPER_SCHEDULES_CREATE,
|
|
135
|
+
ScraperSchedule,
|
|
136
|
+
body=_schedule_body(request),
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
async def update(
|
|
140
|
+
self, schedule_id: str, request: UpdateScraperScheduleParams
|
|
141
|
+
) -> ScraperSchedule:
|
|
142
|
+
_validate_days(request.get("weekdays"), request.get("month_day"))
|
|
143
|
+
return await self._request(
|
|
144
|
+
HTTPMethod.PATCH,
|
|
145
|
+
ScraperRoute.SCRAPER_SCHEDULES_UPDATE.format(schedule_id=_path_id(schedule_id)),
|
|
146
|
+
ScraperSchedule,
|
|
147
|
+
body=_schedule_body(request),
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
async def toggle_pause(self, schedule_id: str) -> ScraperSchedule:
|
|
151
|
+
"""Toggle the schedule's paused state using the API's pause operation."""
|
|
152
|
+
return await self._request(
|
|
153
|
+
HTTPMethod.DELETE,
|
|
154
|
+
ScraperRoute.SCRAPER_SCHEDULES_PAUSE.format(schedule_id=_path_id(schedule_id)),
|
|
155
|
+
ScraperSchedule,
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
async def run_now(self, schedule_id: str) -> ScraperRun:
|
|
159
|
+
"""Run once without changing the next scheduled occurrence."""
|
|
160
|
+
return await self._request(
|
|
161
|
+
HTTPMethod.POST,
|
|
162
|
+
ScraperRoute.SCRAPER_SCHEDULES_RUN_NOW.format(schedule_id=_path_id(schedule_id)),
|
|
163
|
+
ScraperRun,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
async def pause(self, schedule_id: str) -> ScraperSchedule:
|
|
167
|
+
"""Pause future occurrences."""
|
|
168
|
+
return await self.update(schedule_id, {"enabled": False})
|
|
169
|
+
|
|
170
|
+
async def resume(self, schedule_id: str) -> ScraperSchedule:
|
|
171
|
+
"""Resume future occurrences."""
|
|
172
|
+
return await self.update(schedule_id, {"enabled": True})
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Retained Scraper output and download links."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import builtins
|
|
6
|
+
from collections.abc import AsyncIterator, Iterator
|
|
7
|
+
from http import HTTPMethod
|
|
8
|
+
|
|
9
|
+
from ..._core.types import Query
|
|
10
|
+
from ...exceptions import ConfigurationError
|
|
11
|
+
from ._generated.wire import ScraperRoute
|
|
12
|
+
from ._request import _AsyncScraperEndpoints, _continuation, _path_id, _SyncScraperEndpoints
|
|
13
|
+
from .types import (
|
|
14
|
+
ScraperArtifact,
|
|
15
|
+
ScraperArtifactPage,
|
|
16
|
+
ScraperDownloadLink,
|
|
17
|
+
ScraperStorageArtifact,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _filters(
|
|
22
|
+
format: str | None,
|
|
23
|
+
page_size: int | None,
|
|
24
|
+
request_id: str | None,
|
|
25
|
+
scraper_id: str | None,
|
|
26
|
+
search: str | None,
|
|
27
|
+
status: str | None,
|
|
28
|
+
) -> Query:
|
|
29
|
+
if page_size is not None and (type(page_size) is not int or page_size < 1):
|
|
30
|
+
raise ConfigurationError("page_size must be a positive integer")
|
|
31
|
+
return {
|
|
32
|
+
"format": format,
|
|
33
|
+
"page_size": page_size,
|
|
34
|
+
"request_id": request_id,
|
|
35
|
+
"scraper_id": scraper_id,
|
|
36
|
+
"search": search,
|
|
37
|
+
"status": status,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class ScraperStorage(_SyncScraperEndpoints):
|
|
42
|
+
"""Retained Scraper output and download links."""
|
|
43
|
+
|
|
44
|
+
def list(
|
|
45
|
+
self,
|
|
46
|
+
*,
|
|
47
|
+
format: str | None = None,
|
|
48
|
+
page_size: int | None = None,
|
|
49
|
+
request_id: str | None = None,
|
|
50
|
+
scraper_id: str | None = None,
|
|
51
|
+
search: str | None = None,
|
|
52
|
+
status: str | None = None,
|
|
53
|
+
) -> ScraperArtifactPage:
|
|
54
|
+
"""List retained outputs matching the supplied filters."""
|
|
55
|
+
return self._page(_filters(format, page_size, request_id, scraper_id, search, status))
|
|
56
|
+
|
|
57
|
+
def _page(self, params: Query) -> ScraperArtifactPage:
|
|
58
|
+
return self._request(
|
|
59
|
+
HTTPMethod.GET, ScraperRoute.SCRAPER_ARTIFACTS_LIST, ScraperArtifactPage, params=params
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def list_for_run(self, request_id: str) -> builtins.list[ScraperArtifact]:
|
|
63
|
+
return self._request(
|
|
64
|
+
HTTPMethod.GET,
|
|
65
|
+
ScraperRoute.SCRAPER_REQUEST_ARTIFACTS_LIST.format(request_id=_path_id(request_id)),
|
|
66
|
+
list[ScraperArtifact],
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def create_download_link(self, artifact_id: str) -> ScraperDownloadLink:
|
|
70
|
+
"""Create a short-lived link to a retained output."""
|
|
71
|
+
return self._request(
|
|
72
|
+
HTTPMethod.POST,
|
|
73
|
+
ScraperRoute.SCRAPER_ARTIFACT_DOWNLOAD.format(artifact_id=_path_id(artifact_id)),
|
|
74
|
+
ScraperDownloadLink,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def iterate(
|
|
78
|
+
self,
|
|
79
|
+
*,
|
|
80
|
+
format: str | None = None,
|
|
81
|
+
page_size: int | None = None,
|
|
82
|
+
request_id: str | None = None,
|
|
83
|
+
scraper_id: str | None = None,
|
|
84
|
+
search: str | None = None,
|
|
85
|
+
status: str | None = None,
|
|
86
|
+
) -> Iterator[ScraperStorageArtifact]:
|
|
87
|
+
"""Yield matching outputs across all pages."""
|
|
88
|
+
params = _filters(format, page_size, request_id, scraper_id, search, status)
|
|
89
|
+
seen: set[tuple[tuple[str, str], ...]] = set()
|
|
90
|
+
page = self._page(params)
|
|
91
|
+
while True:
|
|
92
|
+
yield from page["results"]
|
|
93
|
+
next_link = page["next"]
|
|
94
|
+
if not next_link:
|
|
95
|
+
return
|
|
96
|
+
page = self._page({**params, **_continuation(next_link, seen)})
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class AsyncScraperStorage(_AsyncScraperEndpoints):
|
|
100
|
+
"""Retained Scraper output and download links."""
|
|
101
|
+
|
|
102
|
+
async def list(
|
|
103
|
+
self,
|
|
104
|
+
*,
|
|
105
|
+
format: str | None = None,
|
|
106
|
+
page_size: int | None = None,
|
|
107
|
+
request_id: str | None = None,
|
|
108
|
+
scraper_id: str | None = None,
|
|
109
|
+
search: str | None = None,
|
|
110
|
+
status: str | None = None,
|
|
111
|
+
) -> ScraperArtifactPage:
|
|
112
|
+
"""List retained outputs matching the supplied filters."""
|
|
113
|
+
return await self._page(_filters(format, page_size, request_id, scraper_id, search, status))
|
|
114
|
+
|
|
115
|
+
async def _page(self, params: Query) -> ScraperArtifactPage:
|
|
116
|
+
return await self._request(
|
|
117
|
+
HTTPMethod.GET, ScraperRoute.SCRAPER_ARTIFACTS_LIST, ScraperArtifactPage, params=params
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
async def list_for_run(self, request_id: str) -> builtins.list[ScraperArtifact]:
|
|
121
|
+
return await self._request(
|
|
122
|
+
HTTPMethod.GET,
|
|
123
|
+
ScraperRoute.SCRAPER_REQUEST_ARTIFACTS_LIST.format(request_id=_path_id(request_id)),
|
|
124
|
+
list[ScraperArtifact],
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
async def create_download_link(self, artifact_id: str) -> ScraperDownloadLink:
|
|
128
|
+
"""Create a short-lived link to a retained output."""
|
|
129
|
+
return await self._request(
|
|
130
|
+
HTTPMethod.POST,
|
|
131
|
+
ScraperRoute.SCRAPER_ARTIFACT_DOWNLOAD.format(artifact_id=_path_id(artifact_id)),
|
|
132
|
+
ScraperDownloadLink,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
async def iterate(
|
|
136
|
+
self,
|
|
137
|
+
*,
|
|
138
|
+
format: str | None = None,
|
|
139
|
+
page_size: int | None = None,
|
|
140
|
+
request_id: str | None = None,
|
|
141
|
+
scraper_id: str | None = None,
|
|
142
|
+
search: str | None = None,
|
|
143
|
+
status: str | None = None,
|
|
144
|
+
) -> AsyncIterator[ScraperStorageArtifact]:
|
|
145
|
+
"""Yield matching outputs across all pages."""
|
|
146
|
+
params = _filters(format, page_size, request_id, scraper_id, search, status)
|
|
147
|
+
seen: set[tuple[tuple[str, str], ...]] = set()
|
|
148
|
+
page = await self._page(params)
|
|
149
|
+
while True:
|
|
150
|
+
for artifact in page["results"]:
|
|
151
|
+
yield artifact
|
|
152
|
+
next_link = page["next"]
|
|
153
|
+
if not next_link:
|
|
154
|
+
return
|
|
155
|
+
page = await self._page({**params, **_continuation(next_link, seen)})
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Reusable Scraping Tasks and their revisions."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import builtins
|
|
6
|
+
from http import HTTPMethod
|
|
7
|
+
|
|
8
|
+
from ._generated.wire import ScraperRoute
|
|
9
|
+
from ._request import _AsyncScraperEndpoints, _path_id, _SyncScraperEndpoints
|
|
10
|
+
from .types import (
|
|
11
|
+
CreateScrapingTaskParams,
|
|
12
|
+
CreateScrapingTaskRevisionParams,
|
|
13
|
+
RunScrapingTaskParams,
|
|
14
|
+
ScraperRun,
|
|
15
|
+
ScrapingTask,
|
|
16
|
+
ScrapingTaskRevision,
|
|
17
|
+
UpdateScrapingTaskParams,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ScraperTasks(_SyncScraperEndpoints):
|
|
22
|
+
"""Reusable Scraping Tasks and their revisions."""
|
|
23
|
+
|
|
24
|
+
def list(self) -> builtins.list[ScrapingTask]:
|
|
25
|
+
return self._request(HTTPMethod.GET, ScraperRoute.SCRAPING_TASKS_LIST, list[ScrapingTask])
|
|
26
|
+
|
|
27
|
+
def get(self, task_id: str) -> ScrapingTask:
|
|
28
|
+
return self._request(
|
|
29
|
+
HTTPMethod.GET,
|
|
30
|
+
ScraperRoute.SCRAPING_TASKS_RETRIEVE.format(task_id=_path_id(task_id)),
|
|
31
|
+
ScrapingTask,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def create(self, request: CreateScrapingTaskParams) -> ScrapingTask:
|
|
35
|
+
return self._request(
|
|
36
|
+
HTTPMethod.POST, ScraperRoute.SCRAPING_TASKS_CREATE, ScrapingTask, body=request
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
def update(self, task_id: str, request: UpdateScrapingTaskParams) -> ScrapingTask:
|
|
40
|
+
return self._request(
|
|
41
|
+
HTTPMethod.PATCH,
|
|
42
|
+
ScraperRoute.SCRAPING_TASKS_UPDATE.format(task_id=_path_id(task_id)),
|
|
43
|
+
ScrapingTask,
|
|
44
|
+
body=request,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
def archive(self, task_id: str) -> ScrapingTask:
|
|
48
|
+
return self._request(
|
|
49
|
+
HTTPMethod.DELETE,
|
|
50
|
+
ScraperRoute.SCRAPING_TASKS_ARCHIVE.format(task_id=_path_id(task_id)),
|
|
51
|
+
ScrapingTask,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
def list_revisions(self, task_id: str) -> builtins.list[ScrapingTaskRevision]:
|
|
55
|
+
return self._request(
|
|
56
|
+
HTTPMethod.GET,
|
|
57
|
+
ScraperRoute.SCRAPING_TASK_REVISIONS_LIST.format(task_id=_path_id(task_id)),
|
|
58
|
+
list[ScrapingTaskRevision],
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def create_revision(
|
|
62
|
+
self, task_id: str, request: CreateScrapingTaskRevisionParams
|
|
63
|
+
) -> ScrapingTaskRevision:
|
|
64
|
+
return self._request(
|
|
65
|
+
HTTPMethod.POST,
|
|
66
|
+
ScraperRoute.SCRAPING_TASK_REVISIONS_CREATE.format(task_id=_path_id(task_id)),
|
|
67
|
+
ScrapingTaskRevision,
|
|
68
|
+
body=request,
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
def run(self, task_id: str, request: RunScrapingTaskParams | None = None) -> ScraperRun:
|
|
72
|
+
"""Run a task with optional overrides and an idempotency key."""
|
|
73
|
+
return self._submit_run(
|
|
74
|
+
ScraperRoute.SCRAPING_TASKS_RUN.format(task_id=_path_id(task_id)), request or {}
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class AsyncScraperTasks(_AsyncScraperEndpoints):
|
|
79
|
+
"""Reusable Scraping Tasks and their revisions."""
|
|
80
|
+
|
|
81
|
+
async def list(self) -> builtins.list[ScrapingTask]:
|
|
82
|
+
return await self._request(
|
|
83
|
+
HTTPMethod.GET, ScraperRoute.SCRAPING_TASKS_LIST, list[ScrapingTask]
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
async def get(self, task_id: str) -> ScrapingTask:
|
|
87
|
+
return await self._request(
|
|
88
|
+
HTTPMethod.GET,
|
|
89
|
+
ScraperRoute.SCRAPING_TASKS_RETRIEVE.format(task_id=_path_id(task_id)),
|
|
90
|
+
ScrapingTask,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
async def create(self, request: CreateScrapingTaskParams) -> ScrapingTask:
|
|
94
|
+
return await self._request(
|
|
95
|
+
HTTPMethod.POST, ScraperRoute.SCRAPING_TASKS_CREATE, ScrapingTask, body=request
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
async def update(self, task_id: str, request: UpdateScrapingTaskParams) -> ScrapingTask:
|
|
99
|
+
return await self._request(
|
|
100
|
+
HTTPMethod.PATCH,
|
|
101
|
+
ScraperRoute.SCRAPING_TASKS_UPDATE.format(task_id=_path_id(task_id)),
|
|
102
|
+
ScrapingTask,
|
|
103
|
+
body=request,
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
async def archive(self, task_id: str) -> ScrapingTask:
|
|
107
|
+
return await self._request(
|
|
108
|
+
HTTPMethod.DELETE,
|
|
109
|
+
ScraperRoute.SCRAPING_TASKS_ARCHIVE.format(task_id=_path_id(task_id)),
|
|
110
|
+
ScrapingTask,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
async def list_revisions(self, task_id: str) -> builtins.list[ScrapingTaskRevision]:
|
|
114
|
+
return await self._request(
|
|
115
|
+
HTTPMethod.GET,
|
|
116
|
+
ScraperRoute.SCRAPING_TASK_REVISIONS_LIST.format(task_id=_path_id(task_id)),
|
|
117
|
+
list[ScrapingTaskRevision],
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
async def create_revision(
|
|
121
|
+
self, task_id: str, request: CreateScrapingTaskRevisionParams
|
|
122
|
+
) -> ScrapingTaskRevision:
|
|
123
|
+
return await self._request(
|
|
124
|
+
HTTPMethod.POST,
|
|
125
|
+
ScraperRoute.SCRAPING_TASK_REVISIONS_CREATE.format(task_id=_path_id(task_id)),
|
|
126
|
+
ScrapingTaskRevision,
|
|
127
|
+
body=request,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
async def run(self, task_id: str, request: RunScrapingTaskParams | None = None) -> ScraperRun:
|
|
131
|
+
"""Run a task with optional overrides and an idempotency key."""
|
|
132
|
+
return await self._submit_run(
|
|
133
|
+
ScraperRoute.SCRAPING_TASKS_RUN.format(task_id=_path_id(task_id)), request or {}
|
|
134
|
+
)
|