-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path_dataset_client.py
More file actions
304 lines (250 loc) · 11.2 KB
/
_dataset_client.py
File metadata and controls
304 lines (250 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from __future__ import annotations
import asyncio
from logging import getLogger
from typing import TYPE_CHECKING, Any
from typing_extensions import override
from apify_client import ApifyClientAsync
from crawlee._utils.byte_size import ByteSize
from crawlee._utils.file import json_dumps
from crawlee.storage_clients._base import DatasetClient
from crawlee.storage_clients.models import DatasetItemsListPage, DatasetMetadata
if TYPE_CHECKING:
from collections.abc import AsyncIterator
from apify_client.clients import DatasetClientAsync
from crawlee._types import JsonSerializable
from apify import Configuration
logger = getLogger(__name__)
class ApifyDatasetClient(DatasetClient):
"""An Apify platform implementation of the dataset client."""
_MAX_PAYLOAD_SIZE = ByteSize.from_mb(9)
"""Maximum size for a single payload."""
_SAFETY_BUFFER_COEFFICIENT = 0.01 / 100 # 0.01%
"""Percentage buffer to reduce payload limit slightly for safety."""
_EFFECTIVE_LIMIT_SIZE = _MAX_PAYLOAD_SIZE - (_MAX_PAYLOAD_SIZE * _SAFETY_BUFFER_COEFFICIENT)
"""Calculated payload limit considering safety buffer."""
def __init__(
self,
*,
api_client: DatasetClientAsync,
api_public_base_url: str,
lock: asyncio.Lock,
) -> None:
"""Initialize a new instance.
Preferably use the `ApifyDatasetClient.open` class method to create a new instance.
"""
self._api_client = api_client
"""The Apify dataset client for API operations."""
self._api_public_base_url = api_public_base_url
"""The public base URL for accessing the key-value store records."""
self._lock = lock
"""A lock to ensure that only one operation is performed at a time."""
@override
async def get_metadata(self) -> DatasetMetadata:
metadata = await self._api_client.get()
return DatasetMetadata.model_validate(metadata)
@classmethod
async def open(
cls,
*,
id: str | None,
name: str | None,
configuration: Configuration,
) -> ApifyDatasetClient:
"""Open an Apify dataset client.
This method creates and initializes a new instance of the Apify dataset client.
It handles authentication, storage lookup/creation, and metadata retrieval.
Args:
id: The ID of an existing dataset to open. If provided, the client will connect to this specific storage.
Cannot be used together with `name`.
name: The name of a dataset to get or create. If a storage with this name exists, it will be opened;
otherwise, a new one will be created. Cannot be used together with `id`.
configuration: The configuration object containing API credentials and settings. Must include a valid
`token` and `api_base_url`. May also contain a `default_dataset_id` for fallback when neither
`id` nor `name` is provided.
Returns:
An instance for the opened or created storage client.
Raises:
ValueError: If the configuration is missing required fields (token, api_base_url), if both `id` and `name`
are provided, or if neither `id` nor `name` is provided and no default storage ID is available in
the configuration.
"""
token = configuration.token
if not token:
raise ValueError(f'Apify storage client requires a valid token in Configuration (token={token}).')
api_url = configuration.api_base_url
if not api_url:
raise ValueError(f'Apify storage client requires a valid API URL in Configuration (api_url={api_url}).')
api_public_base_url = configuration.api_public_base_url
if not api_public_base_url:
raise ValueError(
'Apify storage client requires a valid API public base URL in Configuration '
f'(api_public_base_url={api_public_base_url}).'
)
# Create Apify client with the provided token and API URL.
apify_client_async = ApifyClientAsync(
token=token,
api_url=api_url,
max_retries=8,
min_delay_between_retries_millis=500,
timeout_secs=360,
)
apify_datasets_client = apify_client_async.datasets()
# If both id and name are provided, raise an error.
if id and name:
raise ValueError('Only one of "id" or "name" can be specified, not both.')
# If id is provided, get the storage by ID.
if id and name is None:
apify_dataset_client = apify_client_async.dataset(dataset_id=id)
# If name is provided, get or create the storage by name.
if name and id is None:
id = DatasetMetadata.model_validate(
await apify_datasets_client.get_or_create(name=name),
).id
apify_dataset_client = apify_client_async.dataset(dataset_id=id)
# If both id and name are None, try to get the default storage ID from environment variables.
# The default storage ID environment variable is set by the Apify platform. It also contains
# a new storage ID after Actor's reboot or migration.
if id is None and name is None:
id = configuration.default_dataset_id
apify_dataset_client = apify_client_async.dataset(dataset_id=id)
# Fetch its metadata.
metadata = await apify_dataset_client.get()
# If metadata is None, it means the storage does not exist, so we create it.
if metadata is None:
id = DatasetMetadata.model_validate(
await apify_datasets_client.get_or_create(),
).id
apify_dataset_client = apify_client_async.dataset(dataset_id=id)
# Verify that the storage exists by fetching its metadata again.
metadata = await apify_dataset_client.get()
if metadata is None:
raise ValueError(f'Opening dataset with id={id} and name={name} failed.')
return cls(
api_client=apify_dataset_client,
api_public_base_url=api_public_base_url,
lock=asyncio.Lock(),
)
@override
async def purge(self) -> None:
raise NotImplementedError(
'Purging datasets is not supported in the Apify platform. '
'Use the `drop` method to delete the dataset instead.'
)
@override
async def drop(self) -> None:
async with self._lock:
await self._api_client.delete()
@override
async def push_data(self, data: list[Any] | dict[str, Any]) -> None:
async def payloads_generator() -> AsyncIterator[str]:
for index, item in enumerate(data):
yield await self._check_and_serialize(item, index)
async with self._lock:
# Handle lists
if isinstance(data, list):
# Invoke client in series to preserve the order of data
async for items in self._chunk_by_size(payloads_generator()):
await self._api_client.push_items(items=items)
# Handle singular items
else:
items = await self._check_and_serialize(data)
await self._api_client.push_items(items=items)
@override
async def get_data(
self,
*,
offset: int = 0,
limit: int | None = 999_999_999_999,
clean: bool = False,
desc: bool = False,
fields: list[str] | None = None,
omit: list[str] | None = None,
unwind: list[str] | None = None,
skip_empty: bool = False,
skip_hidden: bool = False,
flatten: list[str] | None = None,
view: str | None = None,
) -> DatasetItemsListPage:
response = await self._api_client.list_items(
offset=offset,
limit=limit,
clean=clean,
desc=desc,
fields=fields,
omit=omit,
unwind=unwind,
skip_empty=skip_empty,
skip_hidden=skip_hidden,
flatten=flatten,
view=view,
)
return DatasetItemsListPage.model_validate(vars(response))
@override
async def iterate_items(
self,
*,
offset: int = 0,
limit: int | None = None,
clean: bool = False,
desc: bool = False,
fields: list[str] | None = None,
omit: list[str] | None = None,
unwind: list[str] | None = None,
skip_empty: bool = False,
skip_hidden: bool = False,
) -> AsyncIterator[dict]:
async for item in self._api_client.iterate_items(
offset=offset,
limit=limit,
clean=clean,
desc=desc,
fields=fields,
omit=omit,
unwind=unwind,
skip_empty=skip_empty,
skip_hidden=skip_hidden,
):
yield item
@classmethod
async def _check_and_serialize(cls, item: JsonSerializable, index: int | None = None) -> str:
"""Serialize a given item to JSON, checks its serializability and size against a limit.
Args:
item: The item to serialize.
index: Index of the item, used for error context.
Returns:
Serialized JSON string.
Raises:
ValueError: If item is not JSON serializable or exceeds size limit.
"""
s = ' ' if index is None else f' at index {index} '
try:
payload = await json_dumps(item)
except Exception as exc:
raise ValueError(f'Data item{s}is not serializable to JSON.') from exc
payload_size = ByteSize(len(payload.encode('utf-8')))
if payload_size > cls._EFFECTIVE_LIMIT_SIZE:
raise ValueError(f'Data item{s}is too large (size: {payload_size}, limit: {cls._EFFECTIVE_LIMIT_SIZE})')
return payload
async def _chunk_by_size(self, items: AsyncIterator[str]) -> AsyncIterator[str]:
"""Yield chunks of JSON arrays composed of input strings, respecting a size limit.
Groups an iterable of JSON string payloads into larger JSON arrays, ensuring the total size
of each array does not exceed `EFFECTIVE_LIMIT_SIZE`. Each output is a JSON array string that
contains as many payloads as possible without breaching the size threshold, maintaining the
order of the original payloads. Assumes individual items are below the size limit.
Args:
items: Iterable of JSON string payloads.
Yields:
Strings representing JSON arrays of payloads, each staying within the size limit.
"""
last_chunk_size = ByteSize(2) # Add 2 bytes for [] wrapper.
current_chunk = []
async for payload in items:
payload_size = ByteSize(len(payload.encode('utf-8')))
if last_chunk_size + payload_size <= self._EFFECTIVE_LIMIT_SIZE:
current_chunk.append(payload)
last_chunk_size += payload_size + ByteSize(1) # Add 1 byte for ',' separator.
else:
yield f'[{",".join(current_chunk)}]'
current_chunk = [payload]
last_chunk_size = payload_size + ByteSize(2) # Add 2 bytes for [] wrapper.
yield f'[{",".join(current_chunk)}]'