crawlee 0.6.13b31__py3-none-any.whl → 1.1.1b1__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 crawlee might be problematic. Click here for more details.

Files changed (82) hide show
  1. crawlee/_autoscaling/snapshotter.py +1 -1
  2. crawlee/_request.py +34 -22
  3. crawlee/_service_locator.py +44 -24
  4. crawlee/_types.py +86 -33
  5. crawlee/_utils/raise_if_too_many_kwargs.py +12 -0
  6. crawlee/_utils/recoverable_state.py +32 -8
  7. crawlee/_utils/recurring_task.py +15 -0
  8. crawlee/_utils/robots.py +17 -5
  9. crawlee/_utils/sitemap.py +1 -1
  10. crawlee/_utils/system.py +3 -3
  11. crawlee/_utils/urls.py +9 -2
  12. crawlee/browsers/_browser_pool.py +4 -1
  13. crawlee/browsers/_playwright_browser_controller.py +21 -15
  14. crawlee/browsers/_playwright_browser_plugin.py +17 -3
  15. crawlee/browsers/_types.py +1 -1
  16. crawlee/configuration.py +2 -0
  17. crawlee/crawlers/_abstract_http/_abstract_http_crawler.py +6 -2
  18. crawlee/crawlers/_abstract_http/_abstract_http_parser.py +1 -1
  19. crawlee/crawlers/_abstract_http/_http_crawling_context.py +1 -1
  20. crawlee/crawlers/_adaptive_playwright/_adaptive_playwright_crawler.py +39 -15
  21. crawlee/crawlers/_adaptive_playwright/_adaptive_playwright_crawler_statistics.py +1 -1
  22. crawlee/crawlers/_adaptive_playwright/_adaptive_playwright_crawling_context.py +2 -1
  23. crawlee/crawlers/_adaptive_playwright/_rendering_type_predictor.py +1 -1
  24. crawlee/crawlers/_basic/_basic_crawler.py +124 -37
  25. crawlee/crawlers/_playwright/_playwright_crawler.py +17 -5
  26. crawlee/events/_event_manager.py +3 -1
  27. crawlee/events/_types.py +6 -6
  28. crawlee/fingerprint_suite/_header_generator.py +2 -2
  29. crawlee/fingerprint_suite/_types.py +2 -2
  30. crawlee/otel/crawler_instrumentor.py +3 -3
  31. crawlee/project_template/{{cookiecutter.project_name}}/pyproject.toml +2 -2
  32. crawlee/project_template/{{cookiecutter.project_name}}/requirements.txt +3 -0
  33. crawlee/request_loaders/_request_list.py +1 -1
  34. crawlee/request_loaders/_sitemap_request_loader.py +23 -5
  35. crawlee/sessions/_models.py +2 -2
  36. crawlee/sessions/_session_pool.py +1 -1
  37. crawlee/statistics/_error_snapshotter.py +1 -1
  38. crawlee/statistics/_models.py +33 -2
  39. crawlee/statistics/_statistics.py +24 -33
  40. crawlee/storage_clients/__init__.py +16 -0
  41. crawlee/storage_clients/_base/_storage_client.py +13 -0
  42. crawlee/storage_clients/_file_system/_dataset_client.py +29 -27
  43. crawlee/storage_clients/_file_system/_key_value_store_client.py +29 -25
  44. crawlee/storage_clients/_file_system/_request_queue_client.py +53 -34
  45. crawlee/storage_clients/_file_system/_storage_client.py +16 -3
  46. crawlee/storage_clients/_file_system/_utils.py +0 -0
  47. crawlee/storage_clients/_memory/_dataset_client.py +16 -4
  48. crawlee/storage_clients/_memory/_key_value_store_client.py +16 -4
  49. crawlee/storage_clients/_memory/_request_queue_client.py +16 -4
  50. crawlee/storage_clients/_memory/_storage_client.py +6 -3
  51. crawlee/storage_clients/_redis/__init__.py +6 -0
  52. crawlee/storage_clients/_redis/_client_mixin.py +295 -0
  53. crawlee/storage_clients/_redis/_dataset_client.py +325 -0
  54. crawlee/storage_clients/_redis/_key_value_store_client.py +264 -0
  55. crawlee/storage_clients/_redis/_request_queue_client.py +586 -0
  56. crawlee/storage_clients/_redis/_storage_client.py +146 -0
  57. crawlee/storage_clients/_redis/_utils.py +23 -0
  58. crawlee/storage_clients/_redis/lua_scripts/atomic_bloom_add_requests.lua +36 -0
  59. crawlee/storage_clients/_redis/lua_scripts/atomic_fetch_request.lua +49 -0
  60. crawlee/storage_clients/_redis/lua_scripts/atomic_set_add_requests.lua +37 -0
  61. crawlee/storage_clients/_redis/lua_scripts/reclaim_stale_requests.lua +34 -0
  62. crawlee/storage_clients/_redis/py.typed +0 -0
  63. crawlee/storage_clients/_sql/__init__.py +6 -0
  64. crawlee/storage_clients/_sql/_client_mixin.py +385 -0
  65. crawlee/storage_clients/_sql/_dataset_client.py +310 -0
  66. crawlee/storage_clients/_sql/_db_models.py +268 -0
  67. crawlee/storage_clients/_sql/_key_value_store_client.py +300 -0
  68. crawlee/storage_clients/_sql/_request_queue_client.py +720 -0
  69. crawlee/storage_clients/_sql/_storage_client.py +291 -0
  70. crawlee/storage_clients/_sql/py.typed +0 -0
  71. crawlee/storage_clients/models.py +10 -10
  72. crawlee/storages/_base.py +5 -1
  73. crawlee/storages/_dataset.py +12 -2
  74. crawlee/storages/_key_value_store.py +17 -4
  75. crawlee/storages/_request_queue.py +10 -2
  76. crawlee/storages/_storage_instance_manager.py +133 -71
  77. crawlee/storages/_utils.py +11 -0
  78. {crawlee-0.6.13b31.dist-info → crawlee-1.1.1b1.dist-info}/METADATA +17 -6
  79. {crawlee-0.6.13b31.dist-info → crawlee-1.1.1b1.dist-info}/RECORD +82 -59
  80. {crawlee-0.6.13b31.dist-info → crawlee-1.1.1b1.dist-info}/WHEEL +0 -0
  81. {crawlee-0.6.13b31.dist-info → crawlee-1.1.1b1.dist-info}/entry_points.txt +0 -0
  82. {crawlee-0.6.13b31.dist-info → crawlee-1.1.1b1.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,146 @@
1
+ from __future__ import annotations
2
+
3
+ import warnings
4
+ from typing import Literal
5
+
6
+ from redis.asyncio import Redis
7
+ from typing_extensions import override
8
+
9
+ from crawlee._utils.docs import docs_group
10
+ from crawlee.configuration import Configuration
11
+ from crawlee.storage_clients._base import StorageClient
12
+
13
+ from ._dataset_client import RedisDatasetClient
14
+ from ._key_value_store_client import RedisKeyValueStoreClient
15
+ from ._request_queue_client import RedisRequestQueueClient
16
+
17
+
18
+ @docs_group('Storage clients')
19
+ class RedisStorageClient(StorageClient):
20
+ """Redis implementation of the storage client.
21
+
22
+ This storage client provides access to datasets, key-value stores, and request queues that persist data
23
+ to a Redis database v8.0+. Each storage type uses Redis-specific data structures and key patterns for
24
+ efficient storage and retrieval.
25
+
26
+ The client accepts either a Redis connection string or a pre-configured Redis client instance.
27
+ Exactly one of these parameters must be provided during initialization.
28
+
29
+ Storage types use the following Redis data structures:
30
+ - **Datasets**: Redis JSON arrays for item storage with metadata in JSON objects
31
+ - **Key-value stores**: Redis hashes for key-value pairs with separate metadata storage
32
+ - **Request queues**: Redis lists for FIFO queuing, hashes for request data and in-progress tracking,
33
+ and Bloom filters for request deduplication
34
+
35
+ Warning:
36
+ This is an experimental feature. The behavior and interface may change in future versions.
37
+ """
38
+
39
+ def __init__(
40
+ self,
41
+ *,
42
+ connection_string: str | None = None,
43
+ redis: Redis | None = None,
44
+ queue_dedup_strategy: Literal['default', 'bloom'] = 'default',
45
+ queue_bloom_error_rate: float = 1e-7,
46
+ ) -> None:
47
+ """Initialize the Redis storage client.
48
+
49
+ Args:
50
+ connection_string: Redis connection string (e.g., "redis://localhost:6379").
51
+ Supports standard Redis URL format with optional database selection.
52
+ redis: Pre-configured Redis client instance.
53
+ queue_dedup_strategy: Strategy for request queue deduplication. Options are:
54
+ - 'default': Uses Redis sets for exact deduplication.
55
+ - 'bloom': Uses Redis Bloom filters for probabilistic deduplication with lower memory usage. When using
56
+ this approach, approximately 1 in 1e-7 requests will be falsely considered duplicate.
57
+ queue_bloom_error_rate: Desired false positive rate for Bloom filter deduplication. Only relevant if
58
+ `queue_dedup_strategy` is set to 'bloom'.
59
+ """
60
+ match (redis, connection_string):
61
+ case (None, None):
62
+ raise ValueError('Either redis or connection_string must be provided.')
63
+ case (Redis(), None):
64
+ self._redis = redis
65
+ case (None, str()):
66
+ self._redis = Redis.from_url(connection_string)
67
+ case (Redis(), str()):
68
+ raise ValueError('Either redis or connection_string must be provided, not both.')
69
+
70
+ self._queue_dedup_strategy = queue_dedup_strategy
71
+ self._queue_bloom_error_rate = queue_bloom_error_rate
72
+
73
+ # Call the notification only once
74
+ warnings.warn(
75
+ (
76
+ 'RedisStorageClient is experimental and its API, behavior, and key structure may change in future '
77
+ 'releases.'
78
+ ),
79
+ category=UserWarning,
80
+ stacklevel=2,
81
+ )
82
+
83
+ @override
84
+ async def create_dataset_client(
85
+ self,
86
+ *,
87
+ id: str | None = None,
88
+ name: str | None = None,
89
+ alias: str | None = None,
90
+ configuration: Configuration | None = None,
91
+ ) -> RedisDatasetClient:
92
+ configuration = configuration or Configuration.get_global_configuration()
93
+
94
+ client = await RedisDatasetClient.open(
95
+ id=id,
96
+ name=name,
97
+ alias=alias,
98
+ redis=self._redis,
99
+ )
100
+
101
+ await self._purge_if_needed(client, configuration)
102
+ return client
103
+
104
+ @override
105
+ async def create_kvs_client(
106
+ self,
107
+ *,
108
+ id: str | None = None,
109
+ name: str | None = None,
110
+ alias: str | None = None,
111
+ configuration: Configuration | None = None,
112
+ ) -> RedisKeyValueStoreClient:
113
+ configuration = configuration or Configuration.get_global_configuration()
114
+
115
+ client = await RedisKeyValueStoreClient.open(
116
+ id=id,
117
+ name=name,
118
+ alias=alias,
119
+ redis=self._redis,
120
+ )
121
+
122
+ await self._purge_if_needed(client, configuration)
123
+ return client
124
+
125
+ @override
126
+ async def create_rq_client(
127
+ self,
128
+ *,
129
+ id: str | None = None,
130
+ name: str | None = None,
131
+ alias: str | None = None,
132
+ configuration: Configuration | None = None,
133
+ ) -> RedisRequestQueueClient:
134
+ configuration = configuration or Configuration.get_global_configuration()
135
+
136
+ client = await RedisRequestQueueClient.open(
137
+ id=id,
138
+ name=name,
139
+ alias=alias,
140
+ redis=self._redis,
141
+ dedup_strategy=self._queue_dedup_strategy,
142
+ bloom_error_rate=self._queue_bloom_error_rate,
143
+ )
144
+
145
+ await self._purge_if_needed(client, configuration)
146
+ return client
@@ -0,0 +1,23 @@
1
+ from collections.abc import Awaitable
2
+ from pathlib import Path
3
+ from typing import TypeVar, overload
4
+
5
+ T = TypeVar('T')
6
+
7
+
8
+ @overload
9
+ async def await_redis_response(response: Awaitable[T]) -> T: ...
10
+ @overload
11
+ async def await_redis_response(response: T) -> T: ...
12
+
13
+
14
+ async def await_redis_response(response: Awaitable[T] | T) -> T:
15
+ """Solve the problem of ambiguous typing for redis."""
16
+ return await response if isinstance(response, Awaitable) else response
17
+
18
+
19
+ def read_lua_script(script_name: str) -> str:
20
+ """Read a Lua script from a file."""
21
+ file_path = Path(__file__).parent / 'lua_scripts' / script_name
22
+ with file_path.open('r', encoding='utf-8') as file:
23
+ return file.read()
@@ -0,0 +1,36 @@
1
+ local added_filter_key = KEYS[1]
2
+ local queue_key = KEYS[2]
3
+ local data_key = KEYS[3]
4
+
5
+ local forefront = ARGV[1] == '1'
6
+ local unique_keys = cjson.decode(ARGV[2])
7
+ local requests_data = cjson.decode(ARGV[3])
8
+
9
+ -- Add and check which unique keys are actually new using Bloom filter
10
+ local bf_results = redis.call('bf.madd', added_filter_key, unpack(unique_keys))
11
+
12
+ local actually_added = {}
13
+ local hset_args = {}
14
+
15
+ -- Process the results
16
+ for i, unique_key in ipairs(unique_keys) do
17
+ if bf_results[i] == 1 then
18
+ -- This key was added by us (did not exist before)
19
+ table.insert(hset_args, unique_key)
20
+ table.insert(hset_args, requests_data[unique_key])
21
+ table.insert(actually_added, unique_key)
22
+ end
23
+ end
24
+
25
+ -- Add only those that are actually new
26
+ if #actually_added > 0 then
27
+ redis.call('hset', data_key, unpack(hset_args))
28
+
29
+ if forefront then
30
+ redis.call('lpush', queue_key, unpack(actually_added))
31
+ else
32
+ redis.call('rpush', queue_key, unpack(actually_added))
33
+ end
34
+ end
35
+
36
+ return cjson.encode(actually_added)
@@ -0,0 +1,49 @@
1
+ local queue_key = KEYS[1]
2
+ local in_progress_key = KEYS[2]
3
+ local data_key = KEYS[3]
4
+ local client_id = ARGV[1]
5
+ local blocked_until_timestamp = ARGV[2]
6
+ local batch_size = tonumber(ARGV[3])
7
+
8
+ -- Pop batch unique_key from queue
9
+ local batch_result = redis.call('LMPOP', 1, queue_key, 'LEFT', 'COUNT', batch_size)
10
+ if not batch_result then
11
+ return nil
12
+ end
13
+ local unique_keys = batch_result[2]
14
+
15
+ -- Get requests data
16
+ local requests_data = redis.call('HMGET', data_key, unpack(unique_keys))
17
+ if not requests_data then
18
+ -- Data missing, skip this request
19
+ return nil
20
+ end
21
+
22
+ -- Prepare results and update in_progress
23
+ local final_result = {}
24
+ local in_progress_hmset = {}
25
+ local pending_decrement = 0
26
+ local in_progress_data = cjson.encode({
27
+ client_id = client_id,
28
+ blocked_until_timestamp = tonumber(blocked_until_timestamp)
29
+ })
30
+ for i = 1, #unique_keys do
31
+ local unique_key = unique_keys[i]
32
+ local request_data = requests_data[i]
33
+
34
+ if request_data then
35
+ -- Add to in_progress hash
36
+ table.insert(in_progress_hmset, unique_key)
37
+ table.insert(in_progress_hmset, in_progress_data)
38
+
39
+ table.insert(final_result, request_data)
40
+ end
41
+ end
42
+
43
+ -- Update in_progress hash
44
+ if #in_progress_hmset > 0 then
45
+ redis.call('HMSET', in_progress_key, unpack(in_progress_hmset))
46
+ end
47
+
48
+ -- Return result with requests data
49
+ return final_result
@@ -0,0 +1,37 @@
1
+ local added_filter_key = KEYS[1]
2
+ local queue_key = KEYS[2]
3
+ local data_key = KEYS[3]
4
+
5
+ local forefront = ARGV[1] == '1'
6
+ local unique_keys = cjson.decode(ARGV[2])
7
+ local requests_data = cjson.decode(ARGV[3])
8
+
9
+ -- Add and check which unique keys are actually new using Redis set
10
+ local actually_added = {}
11
+ local hset_args = {}
12
+
13
+ -- Process each unique key
14
+ for _, unique_key in ipairs(unique_keys) do
15
+ -- Try to add the key to the set, returns 1 if added, 0 if already existed
16
+ local set_result = redis.call('sadd', added_filter_key, unique_key)
17
+
18
+ if set_result == 1 then
19
+ -- This key was added by us (did not exist before)
20
+ table.insert(hset_args, unique_key)
21
+ table.insert(hset_args, requests_data[unique_key])
22
+ table.insert(actually_added, unique_key)
23
+ end
24
+ end
25
+
26
+ -- Add only those that are actually new
27
+ if #actually_added > 0 then
28
+ redis.call('hset', data_key, unpack(hset_args))
29
+
30
+ if forefront then
31
+ redis.call('lpush', queue_key, unpack(actually_added))
32
+ else
33
+ redis.call('rpush', queue_key, unpack(actually_added))
34
+ end
35
+ end
36
+
37
+ return cjson.encode(actually_added)
@@ -0,0 +1,34 @@
1
+ local in_progress_key = KEYS[1]
2
+ local queue_key = KEYS[2]
3
+ local data_key = KEYS[3]
4
+ local current_time = tonumber(ARGV[1])
5
+
6
+ local max_reclaim = 1000
7
+
8
+ local cursor = "0"
9
+ local count = 0
10
+
11
+ repeat
12
+ local result = redis.call('hscan', in_progress_key, cursor, 'COUNT', 100)
13
+ cursor = result[1]
14
+ local entries = result[2]
15
+
16
+ for i = 1, #entries, 2 do
17
+ if count >= max_reclaim then
18
+ break
19
+ end
20
+
21
+ local unique_key = entries[i]
22
+ local data = cjson.decode(entries[i + 1])
23
+
24
+ -- Check if timed out
25
+ if current_time > data.blocked_until_timestamp then
26
+ -- Atomically remove from in_progress and add back to queue
27
+ redis.call('hdel', in_progress_key, unique_key)
28
+ redis.call('rpush', queue_key, unique_key)
29
+ count = count + 1
30
+ end
31
+ end
32
+ until cursor == "0" or count >= max_reclaim
33
+
34
+ return count
File without changes
@@ -0,0 +1,6 @@
1
+ from ._dataset_client import SqlDatasetClient
2
+ from ._key_value_store_client import SqlKeyValueStoreClient
3
+ from ._request_queue_client import SqlRequestQueueClient
4
+ from ._storage_client import SqlStorageClient
5
+
6
+ __all__ = ['SqlDatasetClient', 'SqlKeyValueStoreClient', 'SqlRequestQueueClient', 'SqlStorageClient']