PlaywrightCapture 1.24.3__py3-none-any.whl → 1.24.5__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.
@@ -14,8 +14,9 @@ import time
14
14
 
15
15
  from base64 import b64decode
16
16
  from io import BytesIO
17
+ from logging import LoggerAdapter, Logger
17
18
  from tempfile import NamedTemporaryFile
18
- from typing import Any, TypedDict, Literal, TYPE_CHECKING
19
+ from typing import Any, TypedDict, Literal, TYPE_CHECKING, MutableMapping
19
20
  from urllib.parse import urlparse, unquote, urljoin
20
21
  from zipfile import ZipFile
21
22
 
@@ -79,6 +80,16 @@ class CaptureResponse(TypedDict, total=False):
79
80
  potential_favicons: set[bytes] | None
80
81
 
81
82
 
83
+ class PlaywrightCaptureLogAdapter(LoggerAdapter): # type: ignore[type-arg]
84
+ """
85
+ Prepend log entry with the UUID of the capture
86
+ """
87
+ def process(self, msg: str, kwargs: MutableMapping[str, Any]) -> tuple[str, MutableMapping[str, Any]]:
88
+ if self.extra:
89
+ return '[{}] {}'.format(self.extra['uuid'], msg), kwargs
90
+ return msg, kwargs
91
+
92
+
82
93
  class Capture():
83
94
 
84
95
  _browsers: list[BROWSER] = ['chromium', 'firefox', 'webkit']
@@ -90,7 +101,8 @@ class Capture():
90
101
 
91
102
  def __init__(self, browser: BROWSER | None=None, device_name: str | None=None,
92
103
  proxy: str | dict[str, str] | None=None,
93
- general_timeout_in_sec: int | None = None, loglevel: str | int='INFO'):
104
+ general_timeout_in_sec: int | None = None, loglevel: str | int='INFO',
105
+ uuid: str | None=None):
94
106
  """Captures a page with Playwright.
95
107
 
96
108
  :param browser: The browser to use for the capture.
@@ -98,9 +110,15 @@ class Capture():
98
110
  :param proxy: The external proxy to use for the capture.
99
111
  :param general_timeout_in_sec: The general timeout for the capture, including children.
100
112
  :param loglevel: Python loglevel
113
+ :param uuid: The UUID of the capture.
101
114
  """
102
- self.logger = logging.getLogger('playwrightcapture')
103
- self.logger.setLevel(loglevel)
115
+ master_logger = logging.getLogger('playwrightcapture')
116
+ master_logger.setLevel(loglevel)
117
+ self.logger: Logger | PlaywrightCaptureLogAdapter
118
+ if uuid is not None:
119
+ self.logger = PlaywrightCaptureLogAdapter(master_logger, {'uuid': uuid})
120
+ else:
121
+ self.logger = master_logger
104
122
  self.browser_name: BROWSER = browser if browser else 'chromium'
105
123
 
106
124
  if general_timeout_in_sec is None:
@@ -681,7 +699,7 @@ class Capture():
681
699
  self.should_retry = True
682
700
  except Exception:
683
701
  raise e
684
- elif self._exception_is_network_error(initial_error):
702
+ else:
685
703
  raise initial_error
686
704
  else:
687
705
  await page.bring_to_front()
@@ -718,8 +736,8 @@ class Capture():
718
736
  if allow_tracking:
719
737
  await self._wait_for_random_timeout(page, 2)
720
738
  # This event is required trigger the add_locator_handler
721
- if await page.locator("body").is_visible():
722
- await page.locator("body").click(button="right", timeout=2000)
739
+ if await page.locator("body").first.is_visible():
740
+ await page.locator("body").first.click(button="right", timeout=2000)
723
741
 
724
742
  # move mouse
725
743
  await page.mouse.move(x=random.uniform(300, 800), y=random.uniform(200, 500))
@@ -864,7 +882,13 @@ class Capture():
864
882
  'Connection closed',
865
883
  'Navigation interrupted by another one',
866
884
  'Navigation failed because page was closed!',
867
- 'Protocol error (Page.bringToFront): Not attached to an active page']:
885
+ 'Protocol error (Page.bringToFront): Not attached to an active page',
886
+ 'Peer failed to perform TLS handshake: The TLS connection was non-properly terminated.',
887
+ 'Peer failed to perform TLS handshake: Error sending data: Connection reset by peer',
888
+ 'Peer sent fatal TLS alert: The server name sent was not recognized',
889
+ 'Load cannot follow more than 20 redirections',
890
+ 'Page crashed',
891
+ 'Error receiving data: Connection reset by peer']:
868
892
  # Other errors, let's give it another shot
869
893
  self.logger.info(f'Issue with {url} (retrying): {e.message}')
870
894
  self.should_retry = True
@@ -872,6 +896,9 @@ class Capture():
872
896
  # The browser barfed, let's try again
873
897
  self.logger.info(f'Browser barfed on {url} (retrying): {e.message}')
874
898
  self.should_retry = True
899
+ elif e.name in ['net::ERR_INVALID_AUTH_CREDENTIALS']:
900
+ # No need to retry, the credentials are wrong/missing.
901
+ pass
875
902
  else:
876
903
  # Unexpected ones
877
904
  self.logger.exception(f'Something went poorly with {url}: {e.message}')
@@ -932,8 +959,8 @@ class Capture():
932
959
  max_wait = force_max_wait_in_sec
933
960
  else:
934
961
  max_wait = self._capture_timeout / self.__network_not_idle
935
- max_wait *= 1000
936
962
  self.logger.debug(f'Waiting for network idle, max wait: {max_wait}s')
963
+ max_wait *= 1000
937
964
  # If we don't have networkidle relatively quick, it's probably because we're playing a video.
938
965
  await page.wait_for_load_state('networkidle', timeout=max_wait)
939
966
  except PlaywrightTimeoutError:
@@ -1103,8 +1130,11 @@ class Capture():
1103
1130
  'net::ERR_CONNECTION_CLOSED',
1104
1131
  'net::ERR_CONNECTION_REFUSED',
1105
1132
  'net::ERR_CONNECTION_RESET',
1133
+ 'net::ERR_CONNECTION_TIMED_OUT',
1106
1134
  'net::ERR_EMPTY_RESPONSE',
1135
+ 'net::ERR_HTTP_RESPONSE_CODE_FAILURE',
1107
1136
  'net::ERR_HTTP2_PROTOCOL_ERROR',
1137
+ 'net::ERR_INVALID_RESPONSE',
1108
1138
  'net::ERR_NAME_NOT_RESOLVED',
1109
1139
  'net::ERR_SOCKS_CONNECTION_FAILED',
1110
1140
  'net::ERR_SSL_UNRECOGNIZED_NAME_ALERT',
@@ -1251,8 +1281,14 @@ class Capture():
1251
1281
  # unable to identify the mimetype
1252
1282
  self.logger.debug(f'Unable to identify the mimetype for favicon from {u}')
1253
1283
  else:
1254
- if mimetype.startswith('image'):
1284
+ if not mimetype:
1285
+ # empty, ignore
1286
+ pass
1287
+ elif mimetype.startswith('image'):
1255
1288
  to_return.add(favicon)
1289
+ elif mimetype.startswith('text'):
1290
+ # Just ignore, it's probably a 404 page
1291
+ pass
1256
1292
  else:
1257
1293
  self.logger.warning(f'Unexpected mimetype for favicon from {u}: {mimetype}')
1258
1294
  self.logger.debug(f'Done with favicon from {u}.')
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PlaywrightCapture
3
- Version: 1.24.3
3
+ Version: 1.24.5
4
4
  Summary: A simple library to capture websites using playwright
5
5
  Home-page: https://github.com/Lookyloo/PlaywrightCapture
6
6
  License: BSD-3-Clause
@@ -29,7 +29,7 @@ Requires-Dist: puremagic (>=1.21,<2.0)
29
29
  Requires-Dist: pydub (>=0.25.1,<0.26.0) ; extra == "recaptcha"
30
30
  Requires-Dist: pytz (>=2024.1,<2025.0) ; python_version < "3.9"
31
31
  Requires-Dist: requests[socks] (>=2.31.0,<3.0.0) ; extra == "recaptcha"
32
- Requires-Dist: setuptools (>=69.2.0,<70.0.0)
32
+ Requires-Dist: setuptools (>=69.5.1,<70.0.0)
33
33
  Requires-Dist: tzdata (>=2024.1,<2025.0)
34
34
  Requires-Dist: w3lib (>=2.1.2,<3.0.0)
35
35
  Project-URL: Repository, https://github.com/Lookyloo/PlaywrightCapture
@@ -1,9 +1,9 @@
1
1
  playwrightcapture/__init__.py,sha256=F90Y8wYS13tDjgsfjuFrCfmzQfdnH44G-ovuilJfLEE,511
2
- playwrightcapture/capture.py,sha256=KuIaHzPtZqZKYNDO8WfPx22D6Ms6xKezZmgg4I4yCNs,61485
2
+ playwrightcapture/capture.py,sha256=-30zUyPeS8VPt-8MZhkYmFiJEvq9LwAZFp0ChhWeRsY,63307
3
3
  playwrightcapture/exceptions.py,sha256=LhGJQCGHzEu7Sx2Dfl28OFeDg1OmrwufFjAWXlxQnEA,366
4
4
  playwrightcapture/helpers.py,sha256=SXQLEuxMs8-bcWykMiUVosHzzxBKuS-QC0gBV3OmKmo,1764
5
5
  playwrightcapture/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- playwrightcapture-1.24.3.dist-info/LICENSE,sha256=uwFc39fTLacBUG-XTuxX6IQKTKhg4z14gWOLt3ex4Ho,1775
7
- playwrightcapture-1.24.3.dist-info/METADATA,sha256=yuyoEKN1Qa5Dcsx0YkWGST9sPFSP7RlClu9FjMWz53A,3077
8
- playwrightcapture-1.24.3.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
9
- playwrightcapture-1.24.3.dist-info/RECORD,,
6
+ playwrightcapture-1.24.5.dist-info/LICENSE,sha256=uwFc39fTLacBUG-XTuxX6IQKTKhg4z14gWOLt3ex4Ho,1775
7
+ playwrightcapture-1.24.5.dist-info/METADATA,sha256=fT8WNXOeQ5hQVkN7cQhd29L1S3CwShSvd5jpq53orYE,3077
8
+ playwrightcapture-1.24.5.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
9
+ playwrightcapture-1.24.5.dist-info/RECORD,,