scrapely-client-python 3.1.3__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.
Potentially problematic release.
This version of scrapely-client-python might be problematic. Click here for more details.
- scrapely_client/__init__.py +30 -0
- scrapely_client/_charging.py +186 -0
- scrapely_client/_client_registry.py +136 -0
- scrapely_client/_consts.py +96 -0
- scrapely_client/_docs.py +37 -0
- scrapely_client/_literals.py +539 -0
- scrapely_client/_logging.py +180 -0
- scrapely_client/_models.py +4172 -0
- scrapely_client/_pagination.py +220 -0
- scrapely_client/_resource_clients/__init__.py +84 -0
- scrapely_client/_resource_clients/_resource_client.py +567 -0
- scrapely_client/_resource_clients/actor.py +1042 -0
- scrapely_client/_resource_clients/actor_collection.py +377 -0
- scrapely_client/_resource_clients/actor_env_var.py +154 -0
- scrapely_client/_resource_clients/actor_env_var_collection.py +165 -0
- scrapely_client/_resource_clients/actor_version.py +254 -0
- scrapely_client/_resource_clients/actor_version_collection.py +232 -0
- scrapely_client/_resource_clients/build.py +244 -0
- scrapely_client/_resource_clients/build_collection.py +164 -0
- scrapely_client/_resource_clients/dataset.py +1151 -0
- scrapely_client/_resource_clients/dataset_collection.py +244 -0
- scrapely_client/_resource_clients/key_value_store.py +906 -0
- scrapely_client/_resource_clients/key_value_store_collection.py +243 -0
- scrapely_client/_resource_clients/log.py +225 -0
- scrapely_client/_resource_clients/request_queue.py +1167 -0
- scrapely_client/_resource_clients/request_queue_collection.py +240 -0
- scrapely_client/_resource_clients/run.py +875 -0
- scrapely_client/_resource_clients/run_collection.py +226 -0
- scrapely_client/_resource_clients/schedule.py +243 -0
- scrapely_client/_resource_clients/schedule_collection.py +260 -0
- scrapely_client/_resource_clients/store_collection.py +242 -0
- scrapely_client/_resource_clients/task.py +793 -0
- scrapely_client/_resource_clients/task_collection.py +322 -0
- scrapely_client/_resource_clients/user.py +262 -0
- scrapely_client/_resource_clients/webhook.py +292 -0
- scrapely_client/_resource_clients/webhook_collection.py +281 -0
- scrapely_client/_resource_clients/webhook_dispatch.py +84 -0
- scrapely_client/_resource_clients/webhook_dispatch_collection.py +158 -0
- scrapely_client/_scrapely_client.py +837 -0
- scrapely_client/_statistics.py +29 -0
- scrapely_client/_status_message_watcher.py +197 -0
- scrapely_client/_streamed_log.py +250 -0
- scrapely_client/_typeddicts.py +408 -0
- scrapely_client/_utils/__init__.py +0 -0
- scrapely_client/_utils/crypto.py +87 -0
- scrapely_client/_utils/encoding.py +133 -0
- scrapely_client/_utils/errors.py +33 -0
- scrapely_client/_utils/http.py +137 -0
- scrapely_client/_utils/time.py +32 -0
- scrapely_client/_utils/try_import.py +61 -0
- scrapely_client/errors.py +190 -0
- scrapely_client/http_clients/__init__.py +10 -0
- scrapely_client/http_clients/_base.py +430 -0
- scrapely_client/http_clients/_impit.py +598 -0
- scrapely_client/http_compressors/__init__.py +18 -0
- scrapely_client/http_compressors/_base.py +25 -0
- scrapely_client/http_compressors/_brotli.py +38 -0
- scrapely_client/http_compressors/_gzip.py +38 -0
- scrapely_client/http_compressors/_resolve.py +38 -0
- scrapely_client/py.typed +0 -0
- scrapely_client/types.py +52 -0
- scrapely_client_python-3.1.3.dist-info/METADATA +269 -0
- scrapely_client_python-3.1.3.dist-info/RECORD +65 -0
- scrapely_client_python-3.1.3.dist-info/WHEEL +4 -0
- scrapely_client_python-3.1.3.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,1151 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from contextlib import asynccontextmanager, contextmanager
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import TYPE_CHECKING, Any
|
|
6
|
+
|
|
7
|
+
from scrapely_client._docs import docs_group
|
|
8
|
+
from scrapely_client._models import Dataset, DatasetResponse, DatasetStatistics, DatasetStatisticsResponse
|
|
9
|
+
from scrapely_client._pagination import DEFAULT_CHUNK_SIZE, get_items_iterator, get_items_iterator_async
|
|
10
|
+
from scrapely_client._resource_clients._resource_client import ResourceClient, ResourceClientAsync
|
|
11
|
+
from scrapely_client._utils.crypto import create_storage_content_signature
|
|
12
|
+
from scrapely_client._utils.http import response_to_dict, response_to_list
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from collections.abc import AsyncIterator, Iterator
|
|
16
|
+
from datetime import timedelta
|
|
17
|
+
|
|
18
|
+
from scrapely_client._literals import GeneralAccess
|
|
19
|
+
from scrapely_client.http_clients import HttpResponse
|
|
20
|
+
from scrapely_client.types import JsonSerializable, Timeout
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@docs_group('Other')
|
|
24
|
+
@dataclass
|
|
25
|
+
class DatasetItemsPage:
|
|
26
|
+
"""A page of dataset items returned by the `list_items` method.
|
|
27
|
+
|
|
28
|
+
Dataset items are arbitrary JSON objects stored in the dataset, so they cannot be
|
|
29
|
+
represented by a specific Pydantic model. This class provides pagination metadata
|
|
30
|
+
along with the raw items.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
items: list[dict[str, Any]]
|
|
34
|
+
"""List of dataset items. Each item is a JSON object (dictionary)."""
|
|
35
|
+
|
|
36
|
+
total: int
|
|
37
|
+
"""Total number of items in the dataset."""
|
|
38
|
+
|
|
39
|
+
offset: int
|
|
40
|
+
"""The offset of the first item in this page."""
|
|
41
|
+
|
|
42
|
+
count: int
|
|
43
|
+
"""Number of items in this page."""
|
|
44
|
+
|
|
45
|
+
limit: int
|
|
46
|
+
"""The limit that was used for this request."""
|
|
47
|
+
|
|
48
|
+
desc: bool
|
|
49
|
+
"""Whether the items are sorted in descending order."""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@docs_group('Resource clients')
|
|
53
|
+
class DatasetClient(ResourceClient):
|
|
54
|
+
"""Sub-client for managing a specific dataset.
|
|
55
|
+
|
|
56
|
+
Provides methods to manage a specific dataset, e.g. get it, update it, or download its items. Obtain an instance
|
|
57
|
+
via an appropriate method on the `ApifyClient` class.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
def __init__(
|
|
61
|
+
self,
|
|
62
|
+
*,
|
|
63
|
+
resource_id: str | None = None,
|
|
64
|
+
resource_path: str = 'datasets',
|
|
65
|
+
**kwargs: Any,
|
|
66
|
+
) -> None:
|
|
67
|
+
super().__init__(
|
|
68
|
+
resource_id=resource_id,
|
|
69
|
+
resource_path=resource_path,
|
|
70
|
+
**kwargs,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
def get(self, *, timeout: Timeout = 'short') -> Dataset | None:
|
|
74
|
+
"""Retrieve the dataset.
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
timeout: Timeout for the API HTTP request.
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
The retrieved dataset, or None, if it does not exist.
|
|
82
|
+
"""
|
|
83
|
+
result = self._get(timeout=timeout)
|
|
84
|
+
if result is None:
|
|
85
|
+
return None
|
|
86
|
+
return DatasetResponse.model_validate(result).data
|
|
87
|
+
|
|
88
|
+
def update(
|
|
89
|
+
self,
|
|
90
|
+
*,
|
|
91
|
+
name: str | None = None,
|
|
92
|
+
general_access: GeneralAccess | None = None,
|
|
93
|
+
timeout: Timeout = 'short',
|
|
94
|
+
) -> Dataset:
|
|
95
|
+
"""Update the dataset with specified fields.
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
name: The new name for the dataset.
|
|
100
|
+
general_access: Determines how others can access the dataset.
|
|
101
|
+
timeout: Timeout for the API HTTP request.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
The updated dataset.
|
|
105
|
+
"""
|
|
106
|
+
result = self._update(
|
|
107
|
+
timeout=timeout,
|
|
108
|
+
name=name,
|
|
109
|
+
generalAccess=general_access,
|
|
110
|
+
)
|
|
111
|
+
return DatasetResponse.model_validate(result).data
|
|
112
|
+
|
|
113
|
+
def delete(self, *, timeout: Timeout = 'short') -> None:
|
|
114
|
+
"""Delete the dataset.
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
timeout: Timeout for the API HTTP request.
|
|
119
|
+
"""
|
|
120
|
+
self._delete(timeout=timeout)
|
|
121
|
+
|
|
122
|
+
def list_items(
|
|
123
|
+
self,
|
|
124
|
+
*,
|
|
125
|
+
offset: int | None = None,
|
|
126
|
+
limit: int | None = None,
|
|
127
|
+
clean: bool | None = None,
|
|
128
|
+
desc: bool | None = None,
|
|
129
|
+
fields: list[str] | None = None,
|
|
130
|
+
omit: list[str] | None = None,
|
|
131
|
+
unwind: list[str] | None = None,
|
|
132
|
+
skip_empty: bool | None = None,
|
|
133
|
+
skip_hidden: bool | None = None,
|
|
134
|
+
flatten: list[str] | None = None,
|
|
135
|
+
view: str | None = None,
|
|
136
|
+
signature: str | None = None,
|
|
137
|
+
timeout: Timeout = 'long',
|
|
138
|
+
) -> DatasetItemsPage:
|
|
139
|
+
"""List the items of the dataset.
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
offset: Number of items that should be skipped at the start. The default value is 0.
|
|
144
|
+
limit: Maximum number of items to return. By default there is no limit.
|
|
145
|
+
desc: By default, results are returned in the same order as they were stored. To reverse the order,
|
|
146
|
+
set this parameter to True.
|
|
147
|
+
clean: If True, returns only non-empty items and skips hidden fields (i.e. fields starting with
|
|
148
|
+
the # character). The clean parameter is just a shortcut for skip_hidden=True and skip_empty=True
|
|
149
|
+
parameters. Note that since some objects might be skipped from the output, that the result might
|
|
150
|
+
contain less items than the limit value.
|
|
151
|
+
fields: A list of fields which should be picked from the items, only these fields will remain
|
|
152
|
+
in the resulting record objects. Note that the fields in the outputted items are sorted the same
|
|
153
|
+
way as they are specified in the fields parameter. You can use this feature to effectively fix
|
|
154
|
+
the output format.
|
|
155
|
+
omit: A list of fields which should be omitted from the items.
|
|
156
|
+
unwind: A list of fields which should be unwound, in order which they should be processed. Each field
|
|
157
|
+
should be either an array or an object. If the field is an array then every element of the array
|
|
158
|
+
will become a separate record and merged with parent object. If the unwound field is an object then
|
|
159
|
+
it is merged with the parent object. If the unwound field is missing or its value is neither an array
|
|
160
|
+
nor an object and therefore cannot be merged with a parent object, then the item gets preserved
|
|
161
|
+
as it is. Note that the unwound items ignore the desc parameter.
|
|
162
|
+
skip_empty: If True, then empty items are skipped from the output. Note that if used, the results might
|
|
163
|
+
contain less items than the limit value.
|
|
164
|
+
skip_hidden: If True, then hidden fields are skipped from the output, i.e. fields starting with
|
|
165
|
+
the # character.
|
|
166
|
+
flatten: A list of fields that should be flattened.
|
|
167
|
+
view: Name of the dataset view to be used.
|
|
168
|
+
signature: Signature used to access the items.
|
|
169
|
+
timeout: Timeout for the API HTTP request.
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
A page of the list of dataset items according to the specified filters.
|
|
173
|
+
"""
|
|
174
|
+
request_params = self._build_params(
|
|
175
|
+
offset=offset,
|
|
176
|
+
limit=limit,
|
|
177
|
+
desc=desc,
|
|
178
|
+
clean=clean,
|
|
179
|
+
fields=fields,
|
|
180
|
+
omit=omit,
|
|
181
|
+
unwind=unwind,
|
|
182
|
+
skipEmpty=skip_empty,
|
|
183
|
+
skipHidden=skip_hidden,
|
|
184
|
+
flatten=flatten,
|
|
185
|
+
view=view,
|
|
186
|
+
signature=signature,
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
response = self._http_client.call(
|
|
190
|
+
url=self._build_url('items'),
|
|
191
|
+
method='GET',
|
|
192
|
+
params=request_params,
|
|
193
|
+
timeout=timeout,
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
# When using signature, API returns items as list directly
|
|
197
|
+
items = response_to_list(response)
|
|
198
|
+
|
|
199
|
+
return DatasetItemsPage(
|
|
200
|
+
items=items,
|
|
201
|
+
total=int(response.headers['x-apify-pagination-total']),
|
|
202
|
+
offset=int(response.headers['x-apify-pagination-offset']),
|
|
203
|
+
# x-apify-pagination-count returns count of processed items, not count of returned items
|
|
204
|
+
# This makes difference when items were filtered using hidden/empty
|
|
205
|
+
count=max(int(response.headers['x-apify-pagination-count']), len(items)),
|
|
206
|
+
# API returns 999999999999 when no limit is used
|
|
207
|
+
limit=int(response.headers['x-apify-pagination-limit']),
|
|
208
|
+
desc=response.headers['x-apify-pagination-desc'].lower() == 'true',
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
def iterate_items(
|
|
212
|
+
self,
|
|
213
|
+
*,
|
|
214
|
+
offset: int | None = None,
|
|
215
|
+
limit: int | None = None,
|
|
216
|
+
clean: bool | None = None,
|
|
217
|
+
desc: bool | None = None,
|
|
218
|
+
fields: list[str] | None = None,
|
|
219
|
+
omit: list[str] | None = None,
|
|
220
|
+
unwind: list[str] | None = None,
|
|
221
|
+
skip_empty: bool | None = None,
|
|
222
|
+
skip_hidden: bool | None = None,
|
|
223
|
+
signature: str | None = None,
|
|
224
|
+
chunk_size: int | None = None,
|
|
225
|
+
timeout: Timeout = 'long',
|
|
226
|
+
) -> Iterator[dict]:
|
|
227
|
+
"""Iterate over the items in the dataset.
|
|
228
|
+
|
|
229
|
+
Simple `list_items` does only one API call, possibly not listing all items matching the criteria. This method
|
|
230
|
+
returns an iterator that is capable of making multiple API calls to retrieve all items matching the criteria.
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
Args:
|
|
234
|
+
offset: Number of items that should be skipped at the start. The default value is 0.
|
|
235
|
+
limit: Maximum number of items to return. By default there is no limit.
|
|
236
|
+
desc: By default, results are returned in the same order as they were stored. To reverse the order,
|
|
237
|
+
set this parameter to True.
|
|
238
|
+
clean: If True, returns only non-empty items and skips hidden fields (i.e. fields starting with
|
|
239
|
+
the # character). The clean parameter is just a shortcut for skip_hidden=True and skip_empty=True
|
|
240
|
+
parameters. Note that since some objects might be skipped from the output, that the result might
|
|
241
|
+
contain less items than the limit value.
|
|
242
|
+
fields: A list of fields which should be picked from the items, only these fields will remain in
|
|
243
|
+
the resulting record objects. Note that the fields in the outputted items are sorted the same way
|
|
244
|
+
as they are specified in the fields parameter. You can use this feature to effectively fix
|
|
245
|
+
the output format.
|
|
246
|
+
omit: A list of fields which should be omitted from the items.
|
|
247
|
+
unwind: A list of fields which should be unwound, in order which they should be processed. Each field
|
|
248
|
+
should be either an array or an object. If the field is an array then every element of the array
|
|
249
|
+
will become a separate record and merged with parent object. If the unwound field is an object then
|
|
250
|
+
it is merged with the parent object. If the unwound field is missing or its value is neither an array
|
|
251
|
+
nor an object and therefore cannot be merged with a parent object, then the item gets preserved
|
|
252
|
+
as it is. Note that the unwound items ignore the desc parameter.
|
|
253
|
+
skip_empty: If True, then empty items are skipped from the output. Note that if used, the results might
|
|
254
|
+
contain less items than the limit value.
|
|
255
|
+
skip_hidden: If True, then hidden fields are skipped from the output, i.e. fields starting with
|
|
256
|
+
the # character.
|
|
257
|
+
signature: Signature used to access the items.
|
|
258
|
+
chunk_size: Maximum number of items requested per API call when iterating across pages.
|
|
259
|
+
timeout: Timeout for the API HTTP request.
|
|
260
|
+
|
|
261
|
+
Yields:
|
|
262
|
+
An item from the dataset.
|
|
263
|
+
"""
|
|
264
|
+
|
|
265
|
+
def _callback(*, limit: int | None = None, offset: int | None = None) -> DatasetItemsPage:
|
|
266
|
+
return self.list_items(
|
|
267
|
+
offset=offset,
|
|
268
|
+
limit=limit,
|
|
269
|
+
clean=clean,
|
|
270
|
+
desc=desc,
|
|
271
|
+
fields=fields,
|
|
272
|
+
omit=omit,
|
|
273
|
+
unwind=unwind,
|
|
274
|
+
skip_empty=skip_empty,
|
|
275
|
+
skip_hidden=skip_hidden,
|
|
276
|
+
signature=signature,
|
|
277
|
+
timeout=timeout,
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
return get_items_iterator(_callback, limit=limit, offset=offset, chunk_size=chunk_size or DEFAULT_CHUNK_SIZE)
|
|
281
|
+
|
|
282
|
+
def get_items_as_bytes(
|
|
283
|
+
self,
|
|
284
|
+
*,
|
|
285
|
+
item_format: str = 'json',
|
|
286
|
+
offset: int | None = None,
|
|
287
|
+
limit: int | None = None,
|
|
288
|
+
desc: bool | None = None,
|
|
289
|
+
clean: bool | None = None,
|
|
290
|
+
bom: bool | None = None,
|
|
291
|
+
delimiter: str | None = None,
|
|
292
|
+
fields: list[str] | None = None,
|
|
293
|
+
omit: list[str] | None = None,
|
|
294
|
+
unwind: list[str] | None = None,
|
|
295
|
+
skip_empty: bool | None = None,
|
|
296
|
+
skip_header_row: bool | None = None,
|
|
297
|
+
skip_hidden: bool | None = None,
|
|
298
|
+
xml_root: str | None = None,
|
|
299
|
+
xml_row: str | None = None,
|
|
300
|
+
flatten: list[str] | None = None,
|
|
301
|
+
signature: str | None = None,
|
|
302
|
+
timeout: Timeout = 'long',
|
|
303
|
+
) -> bytes:
|
|
304
|
+
"""Get the items in the dataset as raw bytes.
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
Args:
|
|
308
|
+
item_format: Format of the results, possible values are: json, jsonl, csv, html, xlsx, xml and rss.
|
|
309
|
+
The default value is json.
|
|
310
|
+
offset: Number of items that should be skipped at the start. The default value is 0.
|
|
311
|
+
limit: Maximum number of items to return. By default there is no limit.
|
|
312
|
+
desc: By default, results are returned in the same order as they were stored. To reverse the order,
|
|
313
|
+
set this parameter to True.
|
|
314
|
+
clean: If True, returns only non-empty items and skips hidden fields (i.e. fields starting with
|
|
315
|
+
the # character). The clean parameter is just a shortcut for skip_hidden=True and skip_empty=True
|
|
316
|
+
parameters. Note that since some objects might be skipped from the output, that the result might
|
|
317
|
+
contain less items than the limit value.
|
|
318
|
+
bom: All text responses are encoded in UTF-8 encoding. By default, csv files are prefixed with
|
|
319
|
+
the UTF-8 Byte Order Mark (BOM), while json, jsonl, xml, html and rss files are not. If you want
|
|
320
|
+
to override this default behavior, specify bom=True query parameter to include the BOM or bom=False
|
|
321
|
+
to skip it.
|
|
322
|
+
delimiter: A delimiter character for CSV files. The default delimiter is a simple comma (,).
|
|
323
|
+
fields: A list of fields which should be picked from the items, only these fields will remain
|
|
324
|
+
in the resulting record objects. Note that the fields in the outputted items are sorted the same
|
|
325
|
+
way as they are specified in the fields parameter. You can use this feature to effectively fix
|
|
326
|
+
the output format.
|
|
327
|
+
You can use this feature to effectively fix the output format.
|
|
328
|
+
omit: A list of fields which should be omitted from the items.
|
|
329
|
+
unwind: A list of fields which should be unwound, in order which they should be processed. Each field
|
|
330
|
+
should be either an array or an object. If the field is an array then every element of the array
|
|
331
|
+
will become a separate record and merged with parent object. If the unwound field is an object then
|
|
332
|
+
it is merged with the parent object. If the unwound field is missing or its value is neither an array
|
|
333
|
+
nor an object and therefore cannot be merged with a parent object, then the item gets preserved
|
|
334
|
+
as it is. Note that the unwound items ignore the desc parameter.
|
|
335
|
+
skip_empty: If True, then empty items are skipped from the output. Note that if used, the results might
|
|
336
|
+
contain less items than the limit value.
|
|
337
|
+
skip_header_row: If True, then header row in the csv format is skipped.
|
|
338
|
+
skip_hidden: If True, then hidden fields are skipped from the output, i.e. fields starting with
|
|
339
|
+
the # character.
|
|
340
|
+
xml_root: Overrides default root element name of xml output. By default the root element is items.
|
|
341
|
+
xml_row: Overrides default element name that wraps each page or page function result object in xml output.
|
|
342
|
+
By default the element name is item.
|
|
343
|
+
flatten: A list of fields that should be flattened.
|
|
344
|
+
signature: Signature used to access the items.
|
|
345
|
+
timeout: Timeout for the API HTTP request.
|
|
346
|
+
|
|
347
|
+
Returns:
|
|
348
|
+
The dataset items as raw bytes.
|
|
349
|
+
"""
|
|
350
|
+
request_params = self._build_params(
|
|
351
|
+
format=item_format,
|
|
352
|
+
offset=offset,
|
|
353
|
+
limit=limit,
|
|
354
|
+
desc=desc,
|
|
355
|
+
clean=clean,
|
|
356
|
+
bom=bom,
|
|
357
|
+
delimiter=delimiter,
|
|
358
|
+
fields=fields,
|
|
359
|
+
omit=omit,
|
|
360
|
+
unwind=unwind,
|
|
361
|
+
skipEmpty=skip_empty,
|
|
362
|
+
skipHeaderRow=skip_header_row,
|
|
363
|
+
skipHidden=skip_hidden,
|
|
364
|
+
xmlRoot=xml_root,
|
|
365
|
+
xmlRow=xml_row,
|
|
366
|
+
flatten=flatten,
|
|
367
|
+
signature=signature,
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
response = self._http_client.call(
|
|
371
|
+
url=self._build_url('items'),
|
|
372
|
+
method='GET',
|
|
373
|
+
params=request_params,
|
|
374
|
+
timeout=timeout,
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
return response.content
|
|
378
|
+
|
|
379
|
+
@contextmanager
|
|
380
|
+
def stream_items(
|
|
381
|
+
self,
|
|
382
|
+
*,
|
|
383
|
+
item_format: str = 'json',
|
|
384
|
+
offset: int | None = None,
|
|
385
|
+
limit: int | None = None,
|
|
386
|
+
desc: bool | None = None,
|
|
387
|
+
clean: bool | None = None,
|
|
388
|
+
bom: bool | None = None,
|
|
389
|
+
delimiter: str | None = None,
|
|
390
|
+
fields: list[str] | None = None,
|
|
391
|
+
omit: list[str] | None = None,
|
|
392
|
+
unwind: list[str] | None = None,
|
|
393
|
+
skip_empty: bool | None = None,
|
|
394
|
+
skip_header_row: bool | None = None,
|
|
395
|
+
skip_hidden: bool | None = None,
|
|
396
|
+
xml_root: str | None = None,
|
|
397
|
+
xml_row: str | None = None,
|
|
398
|
+
signature: str | None = None,
|
|
399
|
+
timeout: Timeout = 'long',
|
|
400
|
+
) -> Iterator[HttpResponse]:
|
|
401
|
+
"""Retrieve the items in the dataset as a stream.
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
Args:
|
|
405
|
+
item_format: Format of the results, possible values are: json, jsonl, csv, html, xlsx, xml and rss.
|
|
406
|
+
The default value is json.
|
|
407
|
+
offset: Number of items that should be skipped at the start. The default value is 0.
|
|
408
|
+
limit: Maximum number of items to return. By default there is no limit.
|
|
409
|
+
desc: By default, results are returned in the same order as they were stored. To reverse the order,
|
|
410
|
+
set this parameter to True.
|
|
411
|
+
clean: If True, returns only non-empty items and skips hidden fields (i.e. fields starting with
|
|
412
|
+
the # character). The clean parameter is just a shortcut for skip_hidden=True and skip_empty=True
|
|
413
|
+
parameters. Note that since some objects might be skipped from the output, that the result might
|
|
414
|
+
contain less items than the limit value.
|
|
415
|
+
bom: All text responses are encoded in UTF-8 encoding. By default, csv files are prefixed with
|
|
416
|
+
the UTF-8 Byte Order Mark (BOM), while json, jsonl, xml, html and rss files are not. If you want
|
|
417
|
+
to override this default behavior, specify bom=True query parameter to include the BOM or bom=False
|
|
418
|
+
to skip it.
|
|
419
|
+
delimiter: A delimiter character for CSV files. The default delimiter is a simple comma (,).
|
|
420
|
+
fields: A list of fields which should be picked from the items, only these fields will remain
|
|
421
|
+
in the resulting record objects. Note that the fields in the outputted items are sorted the same
|
|
422
|
+
way as they are specified in the fields parameter. You can use this feature to effectively fix
|
|
423
|
+
the output format.
|
|
424
|
+
You can use this feature to effectively fix the output format.
|
|
425
|
+
omit: A list of fields which should be omitted from the items.
|
|
426
|
+
unwind: A list of fields which should be unwound, in order which they should be processed. Each field
|
|
427
|
+
should be either an array or an object. If the field is an array then every element of the array
|
|
428
|
+
will become a separate record and merged with parent object. If the unwound field is an object then
|
|
429
|
+
it is merged with the parent object. If the unwound field is missing or its value is neither an array
|
|
430
|
+
nor an object and therefore cannot be merged with a parent object, then the item gets preserved
|
|
431
|
+
as it is. Note that the unwound items ignore the desc parameter.
|
|
432
|
+
skip_empty: If True, then empty items are skipped from the output. Note that if used, the results might
|
|
433
|
+
contain less items than the limit value.
|
|
434
|
+
skip_header_row: If True, then header row in the csv format is skipped.
|
|
435
|
+
skip_hidden: If True, then hidden fields are skipped from the output, i.e. fields starting with
|
|
436
|
+
the # character.
|
|
437
|
+
xml_root: Overrides default root element name of xml output. By default the root element is items.
|
|
438
|
+
xml_row: Overrides default element name that wraps each page or page function result object in xml output.
|
|
439
|
+
By default the element name is item.
|
|
440
|
+
signature: Signature used to access the items.
|
|
441
|
+
timeout: Timeout for the API HTTP request.
|
|
442
|
+
|
|
443
|
+
Returns:
|
|
444
|
+
The dataset items as a context-managed streaming `Response`.
|
|
445
|
+
"""
|
|
446
|
+
response = None
|
|
447
|
+
try:
|
|
448
|
+
request_params = self._build_params(
|
|
449
|
+
format=item_format,
|
|
450
|
+
offset=offset,
|
|
451
|
+
limit=limit,
|
|
452
|
+
desc=desc,
|
|
453
|
+
clean=clean,
|
|
454
|
+
bom=bom,
|
|
455
|
+
delimiter=delimiter,
|
|
456
|
+
fields=fields,
|
|
457
|
+
omit=omit,
|
|
458
|
+
unwind=unwind,
|
|
459
|
+
skipEmpty=skip_empty,
|
|
460
|
+
skipHeaderRow=skip_header_row,
|
|
461
|
+
skipHidden=skip_hidden,
|
|
462
|
+
xmlRoot=xml_root,
|
|
463
|
+
xmlRow=xml_row,
|
|
464
|
+
signature=signature,
|
|
465
|
+
)
|
|
466
|
+
|
|
467
|
+
response = self._http_client.call(
|
|
468
|
+
url=self._build_url('items'),
|
|
469
|
+
method='GET',
|
|
470
|
+
params=request_params,
|
|
471
|
+
stream=True,
|
|
472
|
+
timeout=timeout,
|
|
473
|
+
)
|
|
474
|
+
yield response
|
|
475
|
+
finally:
|
|
476
|
+
if response:
|
|
477
|
+
response.close()
|
|
478
|
+
|
|
479
|
+
def push_items(self, items: JsonSerializable, *, timeout: Timeout = 'medium') -> None:
|
|
480
|
+
"""Push items to the dataset.
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
Args:
|
|
484
|
+
items: The items which to push in the dataset. Either a stringified JSON, a dictionary, or a list
|
|
485
|
+
of strings or dictionaries.
|
|
486
|
+
timeout: Timeout for the API HTTP request.
|
|
487
|
+
"""
|
|
488
|
+
data = None
|
|
489
|
+
json = None
|
|
490
|
+
|
|
491
|
+
if isinstance(items, str):
|
|
492
|
+
data = items
|
|
493
|
+
else:
|
|
494
|
+
json = items
|
|
495
|
+
|
|
496
|
+
self._http_client.call(
|
|
497
|
+
url=self._build_url('items'),
|
|
498
|
+
method='POST',
|
|
499
|
+
headers={'content-type': 'application/json; charset=utf-8'},
|
|
500
|
+
params=self._build_params(),
|
|
501
|
+
data=data,
|
|
502
|
+
json=json,
|
|
503
|
+
timeout=timeout,
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
def get_statistics(self, *, timeout: Timeout = 'short') -> DatasetStatistics:
|
|
507
|
+
"""Get the dataset statistics.
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
Args:
|
|
511
|
+
timeout: Timeout for the API HTTP request.
|
|
512
|
+
|
|
513
|
+
Returns:
|
|
514
|
+
The dataset statistics.
|
|
515
|
+
|
|
516
|
+
Raises:
|
|
517
|
+
NotFoundError: If the dataset does not exist.
|
|
518
|
+
"""
|
|
519
|
+
response = self._http_client.call(
|
|
520
|
+
url=self._build_url('statistics'),
|
|
521
|
+
method='GET',
|
|
522
|
+
params=self._build_params(),
|
|
523
|
+
timeout=timeout,
|
|
524
|
+
)
|
|
525
|
+
result = response_to_dict(response)
|
|
526
|
+
return DatasetStatisticsResponse.model_validate(result).data
|
|
527
|
+
|
|
528
|
+
def create_items_public_url(
|
|
529
|
+
self,
|
|
530
|
+
*,
|
|
531
|
+
offset: int | None = None,
|
|
532
|
+
limit: int | None = None,
|
|
533
|
+
clean: bool | None = None,
|
|
534
|
+
desc: bool | None = None,
|
|
535
|
+
fields: list[str] | None = None,
|
|
536
|
+
omit: list[str] | None = None,
|
|
537
|
+
unwind: list[str] | None = None,
|
|
538
|
+
skip_empty: bool | None = None,
|
|
539
|
+
skip_hidden: bool | None = None,
|
|
540
|
+
flatten: list[str] | None = None,
|
|
541
|
+
view: str | None = None,
|
|
542
|
+
expires_in: timedelta | None = None,
|
|
543
|
+
timeout: Timeout = 'long',
|
|
544
|
+
) -> str:
|
|
545
|
+
"""Generate a URL that can be used to access dataset items.
|
|
546
|
+
|
|
547
|
+
If the client has permission to access the dataset's URL signing key,
|
|
548
|
+
the URL will include a signature to verify its authenticity.
|
|
549
|
+
|
|
550
|
+
You can optionally control how long the signed URL should be valid using the `expires_in` option.
|
|
551
|
+
This value sets the expiration duration from the time the URL is generated.
|
|
552
|
+
If not provided, the URL will not expire.
|
|
553
|
+
|
|
554
|
+
Any other options (like `limit` or `offset`) will be included as query parameters in the URL.
|
|
555
|
+
|
|
556
|
+
Args:
|
|
557
|
+
offset: Number of items that should be skipped at the start. The default value is 0.
|
|
558
|
+
limit: Maximum number of items to return. By default there is no limit.
|
|
559
|
+
clean: If True, returns only non-empty items and skips hidden fields.
|
|
560
|
+
desc: By default, results are returned in the same order as they were stored. To reverse the order,
|
|
561
|
+
set this parameter to True.
|
|
562
|
+
fields: A list of fields which should be picked from the items.
|
|
563
|
+
omit: A list of fields which should be omitted from the items.
|
|
564
|
+
unwind: A list of fields which should be unwound, in order which they should be processed.
|
|
565
|
+
skip_empty: If True, then empty items are skipped from the output.
|
|
566
|
+
skip_hidden: If True, then hidden fields are skipped from the output.
|
|
567
|
+
flatten: A list of fields that should be flattened.
|
|
568
|
+
view: Name of the dataset view to be used.
|
|
569
|
+
expires_in: How long the signed URL should be valid.
|
|
570
|
+
timeout: Timeout for the API HTTP request.
|
|
571
|
+
|
|
572
|
+
Returns:
|
|
573
|
+
The public dataset items URL.
|
|
574
|
+
"""
|
|
575
|
+
dataset = self.get(timeout=timeout)
|
|
576
|
+
|
|
577
|
+
request_params = self._build_params(
|
|
578
|
+
offset=offset,
|
|
579
|
+
limit=limit,
|
|
580
|
+
desc=desc,
|
|
581
|
+
clean=clean,
|
|
582
|
+
fields=fields,
|
|
583
|
+
omit=omit,
|
|
584
|
+
unwind=unwind,
|
|
585
|
+
skipEmpty=skip_empty,
|
|
586
|
+
skipHidden=skip_hidden,
|
|
587
|
+
flatten=flatten,
|
|
588
|
+
view=view,
|
|
589
|
+
)
|
|
590
|
+
|
|
591
|
+
if dataset and dataset.url_signing_secret_key:
|
|
592
|
+
signature = create_storage_content_signature(
|
|
593
|
+
dataset.id,
|
|
594
|
+
dataset.url_signing_secret_key,
|
|
595
|
+
expires_in=expires_in,
|
|
596
|
+
)
|
|
597
|
+
request_params['signature'] = signature
|
|
598
|
+
|
|
599
|
+
return self._build_public_url('items', request_params)
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
@docs_group('Resource clients')
|
|
603
|
+
class DatasetClientAsync(ResourceClientAsync):
|
|
604
|
+
"""Sub-client for managing a specific dataset.
|
|
605
|
+
|
|
606
|
+
Provides methods to manage a specific dataset, e.g. get it, update it, or download its items. Obtain an instance
|
|
607
|
+
via an appropriate method on the `ApifyClientAsync` class.
|
|
608
|
+
"""
|
|
609
|
+
|
|
610
|
+
def __init__(
|
|
611
|
+
self,
|
|
612
|
+
*,
|
|
613
|
+
resource_id: str | None = None,
|
|
614
|
+
resource_path: str = 'datasets',
|
|
615
|
+
**kwargs: Any,
|
|
616
|
+
) -> None:
|
|
617
|
+
super().__init__(
|
|
618
|
+
resource_id=resource_id,
|
|
619
|
+
resource_path=resource_path,
|
|
620
|
+
**kwargs,
|
|
621
|
+
)
|
|
622
|
+
|
|
623
|
+
async def get(self, *, timeout: Timeout = 'short') -> Dataset | None:
|
|
624
|
+
"""Retrieve the dataset.
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
Args:
|
|
628
|
+
timeout: Timeout for the API HTTP request.
|
|
629
|
+
|
|
630
|
+
Returns:
|
|
631
|
+
The retrieved dataset, or None, if it does not exist.
|
|
632
|
+
"""
|
|
633
|
+
result = await self._get(timeout=timeout)
|
|
634
|
+
if result is None:
|
|
635
|
+
return None
|
|
636
|
+
return DatasetResponse.model_validate(result).data
|
|
637
|
+
|
|
638
|
+
async def update(
|
|
639
|
+
self,
|
|
640
|
+
*,
|
|
641
|
+
name: str | None = None,
|
|
642
|
+
general_access: GeneralAccess | None = None,
|
|
643
|
+
timeout: Timeout = 'short',
|
|
644
|
+
) -> Dataset:
|
|
645
|
+
"""Update the dataset with specified fields.
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
Args:
|
|
649
|
+
name: The new name for the dataset.
|
|
650
|
+
general_access: Determines how others can access the dataset.
|
|
651
|
+
timeout: Timeout for the API HTTP request.
|
|
652
|
+
|
|
653
|
+
Returns:
|
|
654
|
+
The updated dataset.
|
|
655
|
+
"""
|
|
656
|
+
result = await self._update(
|
|
657
|
+
timeout=timeout,
|
|
658
|
+
name=name,
|
|
659
|
+
generalAccess=general_access,
|
|
660
|
+
)
|
|
661
|
+
return DatasetResponse.model_validate(result).data
|
|
662
|
+
|
|
663
|
+
async def delete(self, *, timeout: Timeout = 'short') -> None:
|
|
664
|
+
"""Delete the dataset.
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
Args:
|
|
668
|
+
timeout: Timeout for the API HTTP request.
|
|
669
|
+
"""
|
|
670
|
+
await self._delete(timeout=timeout)
|
|
671
|
+
|
|
672
|
+
async def list_items(
|
|
673
|
+
self,
|
|
674
|
+
*,
|
|
675
|
+
offset: int | None = None,
|
|
676
|
+
limit: int | None = None,
|
|
677
|
+
clean: bool | None = None,
|
|
678
|
+
desc: bool | None = None,
|
|
679
|
+
fields: list[str] | None = None,
|
|
680
|
+
omit: list[str] | None = None,
|
|
681
|
+
unwind: list[str] | None = None,
|
|
682
|
+
skip_empty: bool | None = None,
|
|
683
|
+
skip_hidden: bool | None = None,
|
|
684
|
+
flatten: list[str] | None = None,
|
|
685
|
+
view: str | None = None,
|
|
686
|
+
signature: str | None = None,
|
|
687
|
+
timeout: Timeout = 'long',
|
|
688
|
+
) -> DatasetItemsPage:
|
|
689
|
+
"""List the items of the dataset.
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
Args:
|
|
693
|
+
offset: Number of items that should be skipped at the start. The default value is 0.
|
|
694
|
+
limit: Maximum number of items to return. By default there is no limit.
|
|
695
|
+
desc: By default, results are returned in the same order as they were stored. To reverse the order,
|
|
696
|
+
set this parameter to True.
|
|
697
|
+
clean: If True, returns only non-empty items and skips hidden fields (i.e. fields starting with
|
|
698
|
+
the # character). The clean parameter is just a shortcut for skip_hidden=True and skip_empty=True
|
|
699
|
+
parameters. Note that since some objects might be skipped from the output, that the result might
|
|
700
|
+
contain less items than the limit value.
|
|
701
|
+
fields: A list of fields which should be picked from the items, only these fields will remain
|
|
702
|
+
in the resulting record objects. Note that the fields in the outputted items are sorted the same
|
|
703
|
+
way as they are specified in the fields parameter. You can use this feature to effectively fix
|
|
704
|
+
the output format.
|
|
705
|
+
omit: A list of fields which should be omitted from the items.
|
|
706
|
+
unwind: A list of fields which should be unwound, in order which they should be processed. Each field
|
|
707
|
+
should be either an array or an object. If the field is an array then every element of the array
|
|
708
|
+
will become a separate record and merged with parent object. If the unwound field is an object then
|
|
709
|
+
it is merged with the parent object. If the unwound field is missing or its value is neither an array
|
|
710
|
+
nor an object and therefore cannot be merged with a parent object, then the item gets preserved
|
|
711
|
+
as it is. Note that the unwound items ignore the desc parameter.
|
|
712
|
+
skip_empty: If True, then empty items are skipped from the output. Note that if used, the results might
|
|
713
|
+
contain less items than the limit value.
|
|
714
|
+
skip_hidden: If True, then hidden fields are skipped from the output, i.e. fields starting with
|
|
715
|
+
the # character.
|
|
716
|
+
flatten: A list of fields that should be flattened.
|
|
717
|
+
view: Name of the dataset view to be used.
|
|
718
|
+
signature: Signature used to access the items.
|
|
719
|
+
timeout: Timeout for the API HTTP request.
|
|
720
|
+
|
|
721
|
+
Returns:
|
|
722
|
+
A page of the list of dataset items according to the specified filters.
|
|
723
|
+
"""
|
|
724
|
+
request_params = self._build_params(
|
|
725
|
+
offset=offset,
|
|
726
|
+
limit=limit,
|
|
727
|
+
desc=desc,
|
|
728
|
+
clean=clean,
|
|
729
|
+
fields=fields,
|
|
730
|
+
omit=omit,
|
|
731
|
+
unwind=unwind,
|
|
732
|
+
skipEmpty=skip_empty,
|
|
733
|
+
skipHidden=skip_hidden,
|
|
734
|
+
flatten=flatten,
|
|
735
|
+
view=view,
|
|
736
|
+
signature=signature,
|
|
737
|
+
)
|
|
738
|
+
|
|
739
|
+
response = await self._http_client.call(
|
|
740
|
+
url=self._build_url('items'),
|
|
741
|
+
method='GET',
|
|
742
|
+
params=request_params,
|
|
743
|
+
timeout=timeout,
|
|
744
|
+
)
|
|
745
|
+
|
|
746
|
+
# When using signature, API returns items as list directly
|
|
747
|
+
items = response_to_list(response)
|
|
748
|
+
|
|
749
|
+
return DatasetItemsPage(
|
|
750
|
+
items=items,
|
|
751
|
+
total=int(response.headers['x-apify-pagination-total']),
|
|
752
|
+
offset=int(response.headers['x-apify-pagination-offset']),
|
|
753
|
+
# x-apify-pagination-count returns count of processed items, not count of returned items
|
|
754
|
+
# This makes difference when items were filtered using hidden/empty
|
|
755
|
+
count=max(int(response.headers['x-apify-pagination-count']), len(items)),
|
|
756
|
+
# API returns 999999999999 when no limit is used
|
|
757
|
+
limit=int(response.headers['x-apify-pagination-limit']),
|
|
758
|
+
desc=response.headers['x-apify-pagination-desc'].lower() == 'true',
|
|
759
|
+
)
|
|
760
|
+
|
|
761
|
+
def iterate_items(
|
|
762
|
+
self,
|
|
763
|
+
*,
|
|
764
|
+
offset: int | None = None,
|
|
765
|
+
limit: int | None = None,
|
|
766
|
+
clean: bool | None = None,
|
|
767
|
+
desc: bool | None = None,
|
|
768
|
+
fields: list[str] | None = None,
|
|
769
|
+
omit: list[str] | None = None,
|
|
770
|
+
unwind: list[str] | None = None,
|
|
771
|
+
skip_empty: bool | None = None,
|
|
772
|
+
skip_hidden: bool | None = None,
|
|
773
|
+
signature: str | None = None,
|
|
774
|
+
chunk_size: int | None = None,
|
|
775
|
+
timeout: Timeout = 'long',
|
|
776
|
+
) -> AsyncIterator[dict]:
|
|
777
|
+
"""Iterate over the items in the dataset.
|
|
778
|
+
|
|
779
|
+
Simple `list_items` does only one API call, possibly not listing all items matching the criteria. This method
|
|
780
|
+
returns an iterator that is capable of making multiple API calls to retrieve all items matching the criteria.
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
Args:
|
|
784
|
+
offset: Number of items that should be skipped at the start. The default value is 0.
|
|
785
|
+
limit: Maximum number of items to return. By default there is no limit.
|
|
786
|
+
desc: By default, results are returned in the same order as they were stored. To reverse the order,
|
|
787
|
+
set this parameter to True.
|
|
788
|
+
clean: If True, returns only non-empty items and skips hidden fields (i.e. fields starting with
|
|
789
|
+
the # character). The clean parameter is just a shortcut for skip_hidden=True and skip_empty=True
|
|
790
|
+
parameters. Note that since some objects might be skipped from the output, that the result might
|
|
791
|
+
contain less items than the limit value.
|
|
792
|
+
fields: A list of fields which should be picked from the items, only these fields will remain in
|
|
793
|
+
the resulting record objects. Note that the fields in the outputted items are sorted the same way
|
|
794
|
+
as they are specified in the fields parameter. You can use this feature to effectively fix
|
|
795
|
+
the output format.
|
|
796
|
+
omit: A list of fields which should be omitted from the items.
|
|
797
|
+
unwind: A list of fields which should be unwound, in order which they should be processed. Each field
|
|
798
|
+
should be either an array or an object. If the field is an array then every element of the array
|
|
799
|
+
will become a separate record and merged with parent object. If the unwound field is an object then
|
|
800
|
+
it is merged with the parent object. If the unwound field is missing or its value is neither an array
|
|
801
|
+
nor an object and therefore cannot be merged with a parent object, then the item gets preserved
|
|
802
|
+
as it is. Note that the unwound items ignore the desc parameter.
|
|
803
|
+
skip_empty: If True, then empty items are skipped from the output. Note that if used, the results might
|
|
804
|
+
contain less items than the limit value.
|
|
805
|
+
skip_hidden: If True, then hidden fields are skipped from the output, i.e. fields starting with
|
|
806
|
+
the # character.
|
|
807
|
+
signature: Signature used to access the items.
|
|
808
|
+
chunk_size: Maximum number of items requested per API call when iterating across pages.
|
|
809
|
+
timeout: Timeout for the API HTTP request.
|
|
810
|
+
|
|
811
|
+
Yields:
|
|
812
|
+
An item from the dataset.
|
|
813
|
+
"""
|
|
814
|
+
|
|
815
|
+
async def _callback(*, limit: int | None = None, offset: int | None = None) -> DatasetItemsPage:
|
|
816
|
+
return await self.list_items(
|
|
817
|
+
offset=offset,
|
|
818
|
+
limit=limit,
|
|
819
|
+
clean=clean,
|
|
820
|
+
desc=desc,
|
|
821
|
+
fields=fields,
|
|
822
|
+
omit=omit,
|
|
823
|
+
unwind=unwind,
|
|
824
|
+
skip_empty=skip_empty,
|
|
825
|
+
skip_hidden=skip_hidden,
|
|
826
|
+
signature=signature,
|
|
827
|
+
timeout=timeout,
|
|
828
|
+
)
|
|
829
|
+
|
|
830
|
+
return get_items_iterator_async(
|
|
831
|
+
_callback, limit=limit, offset=offset, chunk_size=chunk_size or DEFAULT_CHUNK_SIZE
|
|
832
|
+
)
|
|
833
|
+
|
|
834
|
+
async def get_items_as_bytes(
|
|
835
|
+
self,
|
|
836
|
+
*,
|
|
837
|
+
item_format: str = 'json',
|
|
838
|
+
offset: int | None = None,
|
|
839
|
+
limit: int | None = None,
|
|
840
|
+
desc: bool | None = None,
|
|
841
|
+
clean: bool | None = None,
|
|
842
|
+
bom: bool | None = None,
|
|
843
|
+
delimiter: str | None = None,
|
|
844
|
+
fields: list[str] | None = None,
|
|
845
|
+
omit: list[str] | None = None,
|
|
846
|
+
unwind: list[str] | None = None,
|
|
847
|
+
skip_empty: bool | None = None,
|
|
848
|
+
skip_header_row: bool | None = None,
|
|
849
|
+
skip_hidden: bool | None = None,
|
|
850
|
+
xml_root: str | None = None,
|
|
851
|
+
xml_row: str | None = None,
|
|
852
|
+
flatten: list[str] | None = None,
|
|
853
|
+
signature: str | None = None,
|
|
854
|
+
timeout: Timeout = 'long',
|
|
855
|
+
) -> bytes:
|
|
856
|
+
"""Get the items in the dataset as raw bytes.
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
Args:
|
|
860
|
+
item_format: Format of the results, possible values are: json, jsonl, csv, html, xlsx, xml and rss.
|
|
861
|
+
The default value is json.
|
|
862
|
+
offset: Number of items that should be skipped at the start. The default value is 0.
|
|
863
|
+
limit: Maximum number of items to return. By default there is no limit.
|
|
864
|
+
desc: By default, results are returned in the same order as they were stored. To reverse the order,
|
|
865
|
+
set this parameter to True.
|
|
866
|
+
clean: If True, returns only non-empty items and skips hidden fields (i.e. fields starting with
|
|
867
|
+
the # character). The clean parameter is just a shortcut for skip_hidden=True and skip_empty=True
|
|
868
|
+
parameters. Note that since some objects might be skipped from the output, that the result might
|
|
869
|
+
contain less items than the limit value.
|
|
870
|
+
bom: All text responses are encoded in UTF-8 encoding. By default, csv files are prefixed with
|
|
871
|
+
the UTF-8 Byte Order Mark (BOM), while json, jsonl, xml, html and rss files are not. If you want
|
|
872
|
+
to override this default behavior, specify bom=True query parameter to include the BOM or bom=False
|
|
873
|
+
to skip it.
|
|
874
|
+
delimiter: A delimiter character for CSV files. The default delimiter is a simple comma (,).
|
|
875
|
+
fields: A list of fields which should be picked from the items, only these fields will remain
|
|
876
|
+
in the resulting record objects. Note that the fields in the outputted items are sorted the same
|
|
877
|
+
way as they are specified in the fields parameter. You can use this feature to effectively fix
|
|
878
|
+
the output format.
|
|
879
|
+
You can use this feature to effectively fix the output format.
|
|
880
|
+
omit: A list of fields which should be omitted from the items.
|
|
881
|
+
unwind: A list of fields which should be unwound, in order which they should be processed. Each field
|
|
882
|
+
should be either an array or an object. If the field is an array then every element of the array
|
|
883
|
+
will become a separate record and merged with parent object. If the unwound field is an object then
|
|
884
|
+
it is merged with the parent object. If the unwound field is missing or its value is neither an array
|
|
885
|
+
nor an object and therefore cannot be merged with a parent object, then the item gets preserved
|
|
886
|
+
as it is. Note that the unwound items ignore the desc parameter.
|
|
887
|
+
skip_empty: If True, then empty items are skipped from the output. Note that if used, the results might
|
|
888
|
+
contain less items than the limit value.
|
|
889
|
+
skip_header_row: If True, then header row in the csv format is skipped.
|
|
890
|
+
skip_hidden: If True, then hidden fields are skipped from the output, i.e. fields starting with
|
|
891
|
+
the # character.
|
|
892
|
+
xml_root: Overrides default root element name of xml output. By default the root element is items.
|
|
893
|
+
xml_row: Overrides default element name that wraps each page or page function result object in xml output.
|
|
894
|
+
By default the element name is item.
|
|
895
|
+
flatten: A list of fields that should be flattened.
|
|
896
|
+
signature: Signature used to access the items.
|
|
897
|
+
timeout: Timeout for the API HTTP request.
|
|
898
|
+
|
|
899
|
+
Returns:
|
|
900
|
+
The dataset items as raw bytes.
|
|
901
|
+
"""
|
|
902
|
+
request_params = self._build_params(
|
|
903
|
+
format=item_format,
|
|
904
|
+
offset=offset,
|
|
905
|
+
limit=limit,
|
|
906
|
+
desc=desc,
|
|
907
|
+
clean=clean,
|
|
908
|
+
bom=bom,
|
|
909
|
+
delimiter=delimiter,
|
|
910
|
+
fields=fields,
|
|
911
|
+
omit=omit,
|
|
912
|
+
unwind=unwind,
|
|
913
|
+
skipEmpty=skip_empty,
|
|
914
|
+
skipHeaderRow=skip_header_row,
|
|
915
|
+
skipHidden=skip_hidden,
|
|
916
|
+
xmlRoot=xml_root,
|
|
917
|
+
xmlRow=xml_row,
|
|
918
|
+
flatten=flatten,
|
|
919
|
+
signature=signature,
|
|
920
|
+
)
|
|
921
|
+
|
|
922
|
+
response = await self._http_client.call(
|
|
923
|
+
url=self._build_url('items'),
|
|
924
|
+
method='GET',
|
|
925
|
+
params=request_params,
|
|
926
|
+
timeout=timeout,
|
|
927
|
+
)
|
|
928
|
+
|
|
929
|
+
return response.content
|
|
930
|
+
|
|
931
|
+
@asynccontextmanager
|
|
932
|
+
async def stream_items(
|
|
933
|
+
self,
|
|
934
|
+
*,
|
|
935
|
+
item_format: str = 'json',
|
|
936
|
+
offset: int | None = None,
|
|
937
|
+
limit: int | None = None,
|
|
938
|
+
desc: bool | None = None,
|
|
939
|
+
clean: bool | None = None,
|
|
940
|
+
bom: bool | None = None,
|
|
941
|
+
delimiter: str | None = None,
|
|
942
|
+
fields: list[str] | None = None,
|
|
943
|
+
omit: list[str] | None = None,
|
|
944
|
+
unwind: list[str] | None = None,
|
|
945
|
+
skip_empty: bool | None = None,
|
|
946
|
+
skip_header_row: bool | None = None,
|
|
947
|
+
skip_hidden: bool | None = None,
|
|
948
|
+
xml_root: str | None = None,
|
|
949
|
+
xml_row: str | None = None,
|
|
950
|
+
signature: str | None = None,
|
|
951
|
+
timeout: Timeout = 'long',
|
|
952
|
+
) -> AsyncIterator[HttpResponse]:
|
|
953
|
+
"""Retrieve the items in the dataset as a stream.
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
Args:
|
|
957
|
+
item_format: Format of the results, possible values are: json, jsonl, csv, html, xlsx, xml and rss.
|
|
958
|
+
The default value is json.
|
|
959
|
+
offset: Number of items that should be skipped at the start. The default value is 0.
|
|
960
|
+
limit: Maximum number of items to return. By default there is no limit.
|
|
961
|
+
desc: By default, results are returned in the same order as they were stored. To reverse the order,
|
|
962
|
+
set this parameter to True.
|
|
963
|
+
clean: If True, returns only non-empty items and skips hidden fields (i.e. fields starting with
|
|
964
|
+
the # character). The clean parameter is just a shortcut for skip_hidden=True and skip_empty=True
|
|
965
|
+
parameters. Note that since some objects might be skipped from the output, that the result might
|
|
966
|
+
contain less items than the limit value.
|
|
967
|
+
bom: All text responses are encoded in UTF-8 encoding. By default, csv files are prefixed with
|
|
968
|
+
the UTF-8 Byte Order Mark (BOM), while json, jsonl, xml, html and rss files are not. If you want
|
|
969
|
+
to override this default behavior, specify bom=True query parameter to include the BOM or bom=False
|
|
970
|
+
to skip it.
|
|
971
|
+
delimiter: A delimiter character for CSV files. The default delimiter is a simple comma (,).
|
|
972
|
+
fields: A list of fields which should be picked from the items, only these fields will remain
|
|
973
|
+
in the resulting record objects. Note that the fields in the outputted items are sorted the same
|
|
974
|
+
way as they are specified in the fields parameter. You can use this feature to effectively fix
|
|
975
|
+
the output format.
|
|
976
|
+
You can use this feature to effectively fix the output format.
|
|
977
|
+
omit: A list of fields which should be omitted from the items.
|
|
978
|
+
unwind: A list of fields which should be unwound, in order which they should be processed. Each field
|
|
979
|
+
should be either an array or an object. If the field is an array then every element of the array
|
|
980
|
+
will become a separate record and merged with parent object. If the unwound field is an object then
|
|
981
|
+
it is merged with the parent object. If the unwound field is missing or its value is neither an array
|
|
982
|
+
nor an object and therefore cannot be merged with a parent object, then the item gets preserved
|
|
983
|
+
as it is. Note that the unwound items ignore the desc parameter.
|
|
984
|
+
skip_empty: If True, then empty items are skipped from the output. Note that if used, the results might
|
|
985
|
+
contain less items than the limit value.
|
|
986
|
+
skip_header_row: If True, then header row in the csv format is skipped.
|
|
987
|
+
skip_hidden: If True, then hidden fields are skipped from the output, i.e. fields starting with
|
|
988
|
+
the # character.
|
|
989
|
+
xml_root: Overrides default root element name of xml output. By default the root element is items.
|
|
990
|
+
xml_row: Overrides default element name that wraps each page or page function result object in xml output.
|
|
991
|
+
By default the element name is item.
|
|
992
|
+
signature: Signature used to access the items.
|
|
993
|
+
timeout: Timeout for the API HTTP request.
|
|
994
|
+
|
|
995
|
+
Returns:
|
|
996
|
+
The dataset items as a context-managed streaming `Response`.
|
|
997
|
+
"""
|
|
998
|
+
response = None
|
|
999
|
+
try:
|
|
1000
|
+
request_params = self._build_params(
|
|
1001
|
+
format=item_format,
|
|
1002
|
+
offset=offset,
|
|
1003
|
+
limit=limit,
|
|
1004
|
+
desc=desc,
|
|
1005
|
+
clean=clean,
|
|
1006
|
+
bom=bom,
|
|
1007
|
+
delimiter=delimiter,
|
|
1008
|
+
fields=fields,
|
|
1009
|
+
omit=omit,
|
|
1010
|
+
unwind=unwind,
|
|
1011
|
+
skipEmpty=skip_empty,
|
|
1012
|
+
skipHeaderRow=skip_header_row,
|
|
1013
|
+
skipHidden=skip_hidden,
|
|
1014
|
+
xmlRoot=xml_root,
|
|
1015
|
+
xmlRow=xml_row,
|
|
1016
|
+
signature=signature,
|
|
1017
|
+
)
|
|
1018
|
+
|
|
1019
|
+
response = await self._http_client.call(
|
|
1020
|
+
url=self._build_url('items'),
|
|
1021
|
+
method='GET',
|
|
1022
|
+
params=request_params,
|
|
1023
|
+
stream=True,
|
|
1024
|
+
timeout=timeout,
|
|
1025
|
+
)
|
|
1026
|
+
yield response
|
|
1027
|
+
finally:
|
|
1028
|
+
if response:
|
|
1029
|
+
await response.aclose()
|
|
1030
|
+
|
|
1031
|
+
async def push_items(self, items: JsonSerializable, *, timeout: Timeout = 'medium') -> None:
|
|
1032
|
+
"""Push items to the dataset.
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
Args:
|
|
1036
|
+
items: The items which to push in the dataset. Either a stringified JSON, a dictionary, or a list
|
|
1037
|
+
of strings or dictionaries.
|
|
1038
|
+
timeout: Timeout for the API HTTP request.
|
|
1039
|
+
"""
|
|
1040
|
+
data = None
|
|
1041
|
+
json = None
|
|
1042
|
+
|
|
1043
|
+
if isinstance(items, str):
|
|
1044
|
+
data = items
|
|
1045
|
+
else:
|
|
1046
|
+
json = items
|
|
1047
|
+
|
|
1048
|
+
await self._http_client.call(
|
|
1049
|
+
url=self._build_url('items'),
|
|
1050
|
+
method='POST',
|
|
1051
|
+
headers={'content-type': 'application/json; charset=utf-8'},
|
|
1052
|
+
params=self._build_params(),
|
|
1053
|
+
data=data,
|
|
1054
|
+
json=json,
|
|
1055
|
+
timeout=timeout,
|
|
1056
|
+
)
|
|
1057
|
+
|
|
1058
|
+
async def get_statistics(self, *, timeout: Timeout = 'short') -> DatasetStatistics:
|
|
1059
|
+
"""Get the dataset statistics.
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
Args:
|
|
1063
|
+
timeout: Timeout for the API HTTP request.
|
|
1064
|
+
|
|
1065
|
+
Returns:
|
|
1066
|
+
The dataset statistics.
|
|
1067
|
+
|
|
1068
|
+
Raises:
|
|
1069
|
+
NotFoundError: If the dataset does not exist.
|
|
1070
|
+
"""
|
|
1071
|
+
response = await self._http_client.call(
|
|
1072
|
+
url=self._build_url('statistics'),
|
|
1073
|
+
method='GET',
|
|
1074
|
+
params=self._build_params(),
|
|
1075
|
+
timeout=timeout,
|
|
1076
|
+
)
|
|
1077
|
+
result = response_to_dict(response)
|
|
1078
|
+
return DatasetStatisticsResponse.model_validate(result).data
|
|
1079
|
+
|
|
1080
|
+
async def create_items_public_url(
|
|
1081
|
+
self,
|
|
1082
|
+
*,
|
|
1083
|
+
offset: int | None = None,
|
|
1084
|
+
limit: int | None = None,
|
|
1085
|
+
clean: bool | None = None,
|
|
1086
|
+
desc: bool | None = None,
|
|
1087
|
+
fields: list[str] | None = None,
|
|
1088
|
+
omit: list[str] | None = None,
|
|
1089
|
+
unwind: list[str] | None = None,
|
|
1090
|
+
skip_empty: bool | None = None,
|
|
1091
|
+
skip_hidden: bool | None = None,
|
|
1092
|
+
flatten: list[str] | None = None,
|
|
1093
|
+
view: str | None = None,
|
|
1094
|
+
expires_in: timedelta | None = None,
|
|
1095
|
+
timeout: Timeout = 'long',
|
|
1096
|
+
) -> str:
|
|
1097
|
+
"""Generate a URL that can be used to access dataset items.
|
|
1098
|
+
|
|
1099
|
+
If the client has permission to access the dataset's URL signing key,
|
|
1100
|
+
the URL will include a signature to verify its authenticity.
|
|
1101
|
+
|
|
1102
|
+
You can optionally control how long the signed URL should be valid using the `expires_in` option.
|
|
1103
|
+
This value sets the expiration duration from the time the URL is generated.
|
|
1104
|
+
If not provided, the URL will not expire.
|
|
1105
|
+
|
|
1106
|
+
Any other options (like `limit` or `offset`) will be included as query parameters in the URL.
|
|
1107
|
+
|
|
1108
|
+
Args:
|
|
1109
|
+
offset: Number of items that should be skipped at the start. The default value is 0.
|
|
1110
|
+
limit: Maximum number of items to return. By default there is no limit.
|
|
1111
|
+
clean: If True, returns only non-empty items and skips hidden fields.
|
|
1112
|
+
desc: By default, results are returned in the same order as they were stored. To reverse the order,
|
|
1113
|
+
set this parameter to True.
|
|
1114
|
+
fields: A list of fields which should be picked from the items.
|
|
1115
|
+
omit: A list of fields which should be omitted from the items.
|
|
1116
|
+
unwind: A list of fields which should be unwound, in order which they should be processed.
|
|
1117
|
+
skip_empty: If True, then empty items are skipped from the output.
|
|
1118
|
+
skip_hidden: If True, then hidden fields are skipped from the output.
|
|
1119
|
+
flatten: A list of fields that should be flattened.
|
|
1120
|
+
view: Name of the dataset view to be used.
|
|
1121
|
+
expires_in: How long the signed URL should be valid.
|
|
1122
|
+
timeout: Timeout for the API HTTP request.
|
|
1123
|
+
|
|
1124
|
+
Returns:
|
|
1125
|
+
The public dataset items URL.
|
|
1126
|
+
"""
|
|
1127
|
+
dataset = await self.get(timeout=timeout)
|
|
1128
|
+
|
|
1129
|
+
request_params = self._build_params(
|
|
1130
|
+
offset=offset,
|
|
1131
|
+
limit=limit,
|
|
1132
|
+
desc=desc,
|
|
1133
|
+
clean=clean,
|
|
1134
|
+
fields=fields,
|
|
1135
|
+
omit=omit,
|
|
1136
|
+
unwind=unwind,
|
|
1137
|
+
skipEmpty=skip_empty,
|
|
1138
|
+
skipHidden=skip_hidden,
|
|
1139
|
+
flatten=flatten,
|
|
1140
|
+
view=view,
|
|
1141
|
+
)
|
|
1142
|
+
|
|
1143
|
+
if dataset and dataset.url_signing_secret_key:
|
|
1144
|
+
signature = create_storage_content_signature(
|
|
1145
|
+
dataset.id,
|
|
1146
|
+
dataset.url_signing_secret_key,
|
|
1147
|
+
expires_in=expires_in,
|
|
1148
|
+
)
|
|
1149
|
+
request_params['signature'] = signature
|
|
1150
|
+
|
|
1151
|
+
return self._build_public_url('items', request_params)
|