scrapely-python 0.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.
Files changed (43) hide show
  1. scrapely/__init__.py +246 -0
  2. scrapely/_browser_locations.py +5470 -0
  3. scrapely/actor.py +1836 -0
  4. scrapely/browser_configuration.py +146 -0
  5. scrapely/charging.py +1143 -0
  6. scrapely/charging_client.py +107 -0
  7. scrapely/configuration.py +375 -0
  8. scrapely/crypto/__init__.py +21 -0
  9. scrapely/crypto/_secrets.py +210 -0
  10. scrapely/errors.py +153 -0
  11. scrapely/events/__init__.py +57 -0
  12. scrapely/events/_types.py +194 -0
  13. scrapely/events/event_manager.py +292 -0
  14. scrapely/log.py +39 -0
  15. scrapely/patch_browser_pool.py +861 -0
  16. scrapely/pricing_types.py +172 -0
  17. scrapely/proxy_configuration.py +741 -0
  18. scrapely/py.typed +2 -0
  19. scrapely/request_loaders/__init__.py +5 -0
  20. scrapely/request_loaders/_scrapely_request_list.py +169 -0
  21. scrapely/storage/__init__.py +48 -0
  22. scrapely/storage/_file_system/__init__.py +9 -0
  23. scrapely/storage/_file_system/_key_value_store_client.py +155 -0
  24. scrapely/storage/_file_system/_storage_client.py +50 -0
  25. scrapely/storage/_metadata.py +138 -0
  26. scrapely/storage/api_client.py +350 -0
  27. scrapely/storage/client.py +618 -0
  28. scrapely/storage/patch_storage.py +257 -0
  29. scrapely/storage/scrapely_dataset_client.py +305 -0
  30. scrapely/storage/scrapely_kvs_client.py +220 -0
  31. scrapely/storage/scrapely_rq_client.py +496 -0
  32. scrapely/storage/scrapely_storage_client.py +192 -0
  33. scrapely/storage/smart_scrapely_storage_client.py +109 -0
  34. scrapely/storage/types.py +601 -0
  35. scrapely/storage/utils.py +38 -0
  36. scrapely/storage_clients/__init__.py +25 -0
  37. scrapely/storages/__init__.py +5 -0
  38. scrapely/types.py +269 -0
  39. scrapely/webhook.py +82 -0
  40. scrapely_python-0.1.0.dist-info/METADATA +83 -0
  41. scrapely_python-0.1.0.dist-info/RECORD +43 -0
  42. scrapely_python-0.1.0.dist-info/WHEEL +5 -0
  43. scrapely_python-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,109 @@
1
+ """Smart storage client that selects cloud or local based on environment."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING, Optional
6
+
7
+ from typing_extensions import override
8
+
9
+ from crawlee.storage_clients._base import DatasetClient, KeyValueStoreClient, RequestQueueClient, StorageClient
10
+
11
+ from scrapely.configuration import Configuration as ScrapelyConfiguration
12
+
13
+ from ._file_system import ScrapelyFileSystemStorageClient
14
+ from .scrapely_storage_client import ScrapelyStorageClient
15
+
16
+ if TYPE_CHECKING:
17
+ from collections.abc import Hashable
18
+
19
+ from crawlee.configuration import Configuration as CrawleeConfiguration
20
+
21
+
22
+ class SmartScrapelyStorageClient(StorageClient):
23
+ """Selects Scrapely cloud or local FS storage based on environment.
24
+
25
+ Mirrors Apify's ``SmartApifyStorageClient``. When running on the Scrapely platform
26
+ (``is_at_home``), uses ``cloud_storage_client``. Locally, uses ``local_storage_client``.
27
+ Pass ``force_cloud=True`` to use cloud storage from a local machine.
28
+ """
29
+
30
+ def __init__(
31
+ self,
32
+ *,
33
+ cloud_storage_client: Optional[StorageClient] = None,
34
+ local_storage_client: Optional[StorageClient] = None,
35
+ ) -> None:
36
+ self._cloud_storage_client = cloud_storage_client or ScrapelyStorageClient()
37
+ self._local_storage_client = local_storage_client or ScrapelyFileSystemStorageClient()
38
+
39
+ def __str__(self) -> str:
40
+ return (
41
+ f'{self.__class__.__name__}(cloud_storage_client={self._cloud_storage_client.__class__.__name__},'
42
+ f' local_storage_client={self._local_storage_client.__class__.__name__})'
43
+ )
44
+
45
+ @override
46
+ def get_storage_client_cache_key(self, configuration: CrawleeConfiguration) -> Hashable:
47
+ if ScrapelyConfiguration.get_global_config().is_at_home:
48
+ return self._cloud_storage_client.get_storage_client_cache_key(configuration)
49
+ return self._local_storage_client.get_storage_client_cache_key(configuration)
50
+
51
+ @override
52
+ async def create_dataset_client(
53
+ self,
54
+ *,
55
+ id: Optional[str] = None,
56
+ name: Optional[str] = None,
57
+ alias: Optional[str] = None,
58
+ configuration: Optional[CrawleeConfiguration] = None,
59
+ ) -> DatasetClient:
60
+ return await self.get_suitable_storage_client().create_dataset_client(
61
+ id=id, name=name, alias=alias, configuration=configuration,
62
+ )
63
+
64
+ @override
65
+ async def create_kvs_client(
66
+ self,
67
+ *,
68
+ id: Optional[str] = None,
69
+ name: Optional[str] = None,
70
+ alias: Optional[str] = None,
71
+ configuration: Optional[CrawleeConfiguration] = None,
72
+ ) -> KeyValueStoreClient:
73
+ return await self.get_suitable_storage_client().create_kvs_client(
74
+ id=id, name=name, alias=alias, configuration=configuration,
75
+ )
76
+
77
+ @override
78
+ async def create_rq_client(
79
+ self,
80
+ *,
81
+ id: Optional[str] = None,
82
+ name: Optional[str] = None,
83
+ alias: Optional[str] = None,
84
+ configuration: Optional[CrawleeConfiguration] = None,
85
+ ) -> RequestQueueClient:
86
+ return await self.get_suitable_storage_client().create_rq_client(
87
+ id=id, name=name, alias=alias, configuration=configuration,
88
+ )
89
+
90
+ def get_suitable_storage_client(self, *, force_cloud: bool = False) -> StorageClient:
91
+ """Return cloud client when at home or ``force_cloud``, else local FS."""
92
+ scrapely_config = ScrapelyConfiguration.get_global_config()
93
+ if scrapely_config.is_at_home:
94
+ return self._cloud_storage_client
95
+
96
+ if force_cloud:
97
+ cloud = self._cloud_storage_client
98
+ has_token = scrapely_config.token is not None
99
+ if isinstance(cloud, ScrapelyStorageClient):
100
+ has_token = has_token or cloud.has_credentials()
101
+ if not has_token:
102
+ raise RuntimeError(
103
+ 'In order to use Scrapely cloud storage from your computer, '
104
+ 'you need to provide a token using the SCRAPELY_TOKEN / APIFY_TOKEN '
105
+ 'environment variable.'
106
+ )
107
+ return self._cloud_storage_client
108
+
109
+ return self._local_storage_client