datamarket 0.7.99__py3-none-any.whl → 0.7.101__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 datamarket might be problematic. Click here for more details.

@@ -12,3 +12,9 @@ class NotFoundError(Exception):
12
12
  def __init__(self, message="Not found!"):
13
13
  self.message = message
14
14
  super().__init__(self.message)
15
+
16
+
17
+ class BadRequestError(Exception):
18
+ def __init__(self, message="Bad request!"):
19
+ self.message = message
20
+ super().__init__(self.message)
datamarket/utils/main.py CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  import asyncio
5
5
  import configparser
6
+ from datetime import timedelta
6
7
  import logging
7
8
  import random
8
9
  import re
@@ -25,7 +26,7 @@ from tenacity import (
25
26
  wait_exponential,
26
27
  )
27
28
 
28
- from ..exceptions import RedirectionDetectedError, NotFoundError
29
+ from ..exceptions import RedirectionDetectedError, NotFoundError, BadRequestError
29
30
  from ..interfaces.proxy import ProxyInterface
30
31
 
31
32
  ########################################################################################################################
@@ -131,7 +132,7 @@ def parse_field(dict_struct, field_path, format_method=None):
131
132
 
132
133
 
133
134
  @retry(
134
- retry=retry_if_not_exception_type((NotFoundError, RedirectionDetectedError, ProxyError)),
135
+ retry=retry_if_not_exception_type((NotFoundError, BadRequestError, RedirectionDetectedError, ProxyError)),
135
136
  wait=wait_exponential(exp_base=3, multiplier=3, max=60),
136
137
  stop=stop_after_attempt(5),
137
138
  before_sleep=before_sleep_log(logger, logging.WARNING),
@@ -144,7 +145,7 @@ def get_data(
144
145
  sleep: tuple = (6, 3),
145
146
  proxy_interface: ProxyInterface = None,
146
147
  use_auth_proxies: bool = False,
147
- max_proxy_delay: int = 1800,
148
+ max_proxy_delay: timedelta = timedelta(minutes=10),
148
149
  **kwargs,
149
150
  ):
150
151
  retry_type = retry_if_exception_type(ProxyError)
@@ -175,6 +176,8 @@ def get_data(
175
176
 
176
177
  if r.status_code == 404:
177
178
  raise NotFoundError(f"404 Not Found error for {url}")
179
+ if r.status_code == 400:
180
+ raise BadRequestError(f"400 Bad Request error for {url}")
178
181
  r.raise_for_status()
179
182
  r.encoding = "utf-8"
180
183
 
@@ -53,12 +53,14 @@ def human_press_key(page: Page, key: str, count: int = 1, delay: int = 100, add_
53
53
  class PlaywrightCrawler:
54
54
  """A robust, proxy-enabled Playwright crawler with captcha bypass and retry logic."""
55
55
 
56
- def __init__(self, proxy_interface: ProxyInterface):
56
+ def __init__(self, proxy_interface: Optional[ProxyInterface] = None):
57
57
  """
58
- Initializes the crawler with a proxy interface.
58
+ Initializes the crawler.
59
59
 
60
60
  Args:
61
- proxy_interface (ProxyInterface): An object to fetch proxy credentials.
61
+ proxy_interface (Optional[ProxyInterface], optional): Provider used to fetch
62
+ proxy credentials. Defaults to None. When None, no proxy is configured and
63
+ the browser will run without a proxy.
62
64
  """
63
65
  self.proxy_interface = proxy_interface
64
66
  self.pw: Optional[Camoufox] = None
@@ -81,6 +83,25 @@ class PlaywrightCrawler:
81
83
  if self.pw:
82
84
  self.pw.__exit__(exc_type, exc_val, exc_tb)
83
85
 
86
+ def _build_proxy_config(self) -> Optional[dict]:
87
+ """Builds the proxy configuration dictionary.
88
+
89
+ Returns:
90
+ Optional[dict]: Proxy configuration if a proxy_interface is provided; otherwise None.
91
+ """
92
+ if not self.proxy_interface:
93
+ logger.info("Starting browser without proxy.")
94
+ return None
95
+
96
+ host, port, user, pwd = self.proxy_interface.get_proxies(raw=True, use_auth=True)
97
+ proxy_url = f"http://{host}:{port}"
98
+ proxy_cfg: dict = {"server": proxy_url}
99
+ if user and pwd:
100
+ proxy_cfg.update({"username": user, "password": pwd})
101
+
102
+ logger.info(f"Starting browser with proxy: {proxy_url}")
103
+ return proxy_cfg
104
+
84
105
  @retry(
85
106
  wait=wait_exponential(exp_base=2, multiplier=3, max=90),
86
107
  stop=stop_after_delay(timedelta(minutes=10)),
@@ -88,16 +109,20 @@ class PlaywrightCrawler:
88
109
  reraise=True,
89
110
  )
90
111
  def init_context(self) -> Self:
91
- """Initializes a new browser instance and context with a fresh proxy."""
92
- try:
93
- host, port, user, pwd = self.proxy_interface.get_proxies(raw=True, use_auth=True)
94
- proxy_url = f"http://{host}:{port}"
95
- proxy_cfg = {"server": proxy_url}
112
+ """
113
+ Initializes a new browser instance and context.
114
+
115
+ Behavior:
116
+ - If a proxy_interface is provided, fetches fresh proxy credentials and starts
117
+ the browser using that proxy.
118
+ - If proxy_interface is None, starts the browser without any proxy.
96
119
 
97
- if user and pwd:
98
- proxy_cfg.update({"username": user, "password": pwd})
120
+ Returns:
121
+ Self: The crawler instance with active browser, context, and page.
122
+ """
123
+ try:
124
+ proxy_cfg: Optional[dict] = self._build_proxy_config()
99
125
 
100
- logger.info(f"Starting browser with proxy: {proxy_url}")
101
126
  self.pw = Camoufox(headless=True, geoip=True, humanize=True, proxy=proxy_cfg)
102
127
  self.browser = self.pw.__enter__()
103
128
  self.context = self.browser.new_context()
@@ -146,4 +171,4 @@ class PlaywrightCrawler:
146
171
  if not self.page:
147
172
  logger.info("Browser context not found, initializing now...")
148
173
  self.init_context()
149
- return self._goto_with_retry(url)
174
+ return self._goto_with_retry(url)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: datamarket
3
- Version: 0.7.99
3
+ Version: 0.7.101
4
4
  Summary: Utilities that integrate advanced scraping knowledge into just one library.
5
5
  License: GPL-3.0-or-later
6
6
  Author: DataMarket
@@ -1,6 +1,6 @@
1
1
  datamarket/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  datamarket/exceptions/__init__.py,sha256=-Vu-RZNKjW6fYCLqbUJTkKNuHeA8Yi_gyR50oZNaA_8,33
3
- datamarket/exceptions/main.py,sha256=MP5ql6M7DoMbBf-Dg_2ohcUFdWXgzv-dXHntPPit31s,453
3
+ datamarket/exceptions/main.py,sha256=AlPOzLoJT14QSNT7hHvvDBxfNew50evFUgCZ9FxF9lA,607
4
4
  datamarket/interfaces/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  datamarket/interfaces/alchemy.py,sha256=i2lKLLLy3-jpbzV3-jxfRCXTy7jRoTsNU3063pmSonk,15749
6
6
  datamarket/interfaces/aws.py,sha256=co5JkC3iFIp-0FqdYX4eKy3_m71LhZKuJoW6kXwEImc,4780
@@ -16,11 +16,11 @@ datamarket/params/nominatim.py,sha256=RnmYXGoJQCijOsuCavCYcxw98WvOd_vOMK4KaraI0R
16
16
  datamarket/utils/__init__.py,sha256=FHLh-Qp9XpM4LkAocppCf_llW2CWVVghGorkqxqt1wk,34
17
17
  datamarket/utils/airflow.py,sha256=al0vc0YUikNu3Oy51VSn52I7pMU40akFBOl_UlHa2E4,795
18
18
  datamarket/utils/alchemy.py,sha256=SRq6kgh1aANXVShBPgAuglmNhZssPWwWEY503gKSia8,635
19
- datamarket/utils/main.py,sha256=KYHjDOps6_Q3TFV_Jj7MLj-L9Evx05AXELCvp06BARU,5857
19
+ datamarket/utils/main.py,sha256=1F-Klw7GX59BkRvWbTl3j7gX6MneZwlI3KOTmJK4P8Q,6040
20
20
  datamarket/utils/nominatim.py,sha256=IxexKY2KOlDhiKtzsqQfoVUjJXPxJl7tn3iHUaQKg08,5795
21
21
  datamarket/utils/playwright/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
22
  datamarket/utils/playwright/async_api.py,sha256=UbA2D4ScBtYeMfrRjly4RO-s8wXIub9c05J1eoOCpsQ,5782
23
- datamarket/utils/playwright/sync_api.py,sha256=Tw_-KLB3vipFuEQwcX8iCbj7giCzcwXB-bhl_ncR-2Q,5542
23
+ datamarket/utils/playwright/sync_api.py,sha256=eXaZsd7xgWSYJtZv6EAstjSbS2bl9OYlkwMBfqqTbFY,6434
24
24
  datamarket/utils/selenium.py,sha256=IMKlbLzXABFhACnWzhHmB0l2hhVzNwHGZwbo14nEewQ,2499
25
25
  datamarket/utils/soda.py,sha256=eZTXFbI1P3WoMd1MM-YjoVTpdjTcDSWuvBb7ViBMhSQ,941
26
26
  datamarket/utils/strings/__init__.py,sha256=b6TYOT9v7y9ID-lDyZk4E8BH2uIPbsF2ZSLGjCQ1MCQ,43
@@ -29,7 +29,7 @@ datamarket/utils/strings/obfuscation.py,sha256=Jo-x3f2Cb75983smmpcdPqUlBrLCTyrnm
29
29
  datamarket/utils/strings/standardization.py,sha256=c8CAG6HI3AfK0hB3A3IGwsbnQebZ6R3PrA5PELHRXM0,1492
30
30
  datamarket/utils/typer.py,sha256=FDF3l6gh3UlAFPsHCtesnekvct2rKz0oFn3uKARBQvE,814
31
31
  datamarket/utils/types.py,sha256=vxdQZdwdXrfPR4Es52gBgol-tMRIOD6oK9cBo3rB0JQ,74
32
- datamarket-0.7.99.dist-info/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
33
- datamarket-0.7.99.dist-info/METADATA,sha256=RdJIjdRmB4RK9WsZpXkuXL8Je2ZtexQlVj9Qiq2jJt8,7381
34
- datamarket-0.7.99.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
35
- datamarket-0.7.99.dist-info/RECORD,,
32
+ datamarket-0.7.101.dist-info/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
33
+ datamarket-0.7.101.dist-info/METADATA,sha256=1UzLpLNWiHpv7qTEzfxCwvfUfZVzAMAYa5hAihtNdiQ,7382
34
+ datamarket-0.7.101.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
35
+ datamarket-0.7.101.dist-info/RECORD,,