zenx 0.6.4__py3-none-any.whl → 0.6.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.
zenx/clients/http.py CHANGED
@@ -1,4 +1,6 @@
1
1
  from __future__ import annotations
2
+ import time
3
+ from collections import deque
2
4
  import parsel
3
5
  import random
4
6
  from curl_cffi.requests.impersonate import BrowserTypeLiteral
@@ -11,7 +13,7 @@ import json
11
13
  from structlog import BoundLogger
12
14
 
13
15
  from zenx.settings import Settings
14
- from zenx.utils import get_time
16
+ from zenx.utils import get_time, record_request
15
17
 
16
18
 
17
19
  @dataclass
@@ -56,8 +58,24 @@ class HttpClient(ABC):
56
58
  self.logger = logger
57
59
  self.settings = settings
58
60
  self._session_pool: asyncio.Queue
61
+ # stats
62
+ self._requests_timestamps = deque()
63
+ self._total_requests = 0
64
+
59
65
 
66
+ def get_stats(self) -> Dict:
67
+ # calculate RPM based on rolling window of 1 min
68
+ now = time.time()
69
+ # remove requests older than 60 sec
70
+ while self._requests_timestamps and self._requests_timestamps[0] < (now - 60):
71
+ self._requests_timestamps.popleft()
72
+ rpm = len(self._requests_timestamps)
73
+ return {
74
+ "rpm": rpm,
75
+ "total_requests": self._total_requests,
76
+ }
60
77
 
78
+
61
79
  @abstractmethod
62
80
  async def request(
63
81
  self,
@@ -96,6 +114,7 @@ class CurlCffi(HttpClient):
96
114
  return chosen_fingerprint
97
115
 
98
116
 
117
+ @record_request
99
118
  async def request(
100
119
  self,
101
120
  url: str,
@@ -124,7 +143,7 @@ class CurlCffi(HttpClient):
124
143
  )
125
144
  recv_at = get_time()
126
145
  latency = recv_at - req_at
127
- self.logger.debug("response", status=response.status_code, url=url, impersonate=impersonate, client=self.name, requested_at=req_at, responded_at=recv_at, latency_ms=latency)
146
+ self.logger.debug("response", status=response.status_code, url=url, impersonate=session.impersonate, client=self.name, requested_at=req_at, responded_at=recv_at, latency_ms=latency)
128
147
  except Exception:
129
148
  self.logger.exception("request", url=url, client=self.name)
130
149
  raise
zenx/spiders/base.py CHANGED
@@ -1,7 +1,5 @@
1
1
  from __future__ import annotations
2
2
  from abc import ABC, abstractmethod
3
- from collections import deque
4
- import time
5
3
  from typing import ClassVar, Dict, List, Literal, Type
6
4
  from structlog import BoundLogger
7
5
 
@@ -46,22 +44,6 @@ class Spider(ABC):
46
44
  self.pm = pm
47
45
  self.logger = logger
48
46
  self.settings = settings
49
- # stats
50
- self.requests_timestamps = deque()
51
- self.total_requests = 0
52
-
53
-
54
- def get_stats(self) -> Dict:
55
- # calculate RPM based on rolling window of 1 min
56
- now = time.time()
57
- # remove requests older than 60 sec
58
- while self.requests_timestamps and self.requests_timestamps[0] < (now - 60):
59
- self.requests_timestamps.popleft()
60
- rpm = len(self.requests_timestamps)
61
- return {
62
- "rpm": rpm,
63
- "total_requests": self.total_requests,
64
- }
65
47
 
66
48
 
67
49
  @abstractmethod
zenx/utils.py CHANGED
@@ -2,6 +2,8 @@ import time
2
2
  from typing import Dict
3
3
  import functools
4
4
 
5
+ from zenx.clients.http import HttpClient, Response
6
+
5
7
 
6
8
  def get_time() -> int:
7
9
  """ current unix time in milliseconds """
@@ -17,3 +19,13 @@ def log_processing_time(func):
17
19
  self.logger.info("processed", id=item['_id'], time_ms=processed_time, pipeline=self.name)
18
20
  return result
19
21
  return wrapper
22
+
23
+
24
+ def record_request(func):
25
+ @functools.wraps(func)
26
+ async def wrapper(self: HttpClient, *args, **kwargs) -> Response:
27
+ result = await func(self, *args, **kwargs)
28
+ self._requests_timestamps.append(time.time())
29
+ self._total_requests +=1
30
+ return result
31
+ return wrapper
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zenx
3
- Version: 0.6.4
3
+ Version: 0.6.5
4
4
  Summary: mini-framework
5
5
  Requires-Python: >=3.12
6
6
  Requires-Dist: curl-cffi>=0.12.0
@@ -5,10 +5,10 @@ zenx/engine.py,sha256=1rIX44wMjgGrvarVVe6uD3MQO9HJ31cBHSTPqo-qNE8,2948
5
5
  zenx/exceptions.py,sha256=BJXxzwwX2CU6inhppfblx8c8Z6Mhvsk7MAhQ1LAnhBg,37
6
6
  zenx/logger.py,sha256=UmEk0vV1mSCozV7z_DDgCCXdAManDr5wgkrhKiRQtyU,1651
7
7
  zenx/settings.py,sha256=Q0z3oGyVkhucRyqsraZQR1lAPSq1sjiGKmAkDCmQsA0,940
8
- zenx/utils.py,sha256=ozf3dpcH1KyDXh9coDe3NqzL8OS-1uA6SaQ8lfHfI40,587
8
+ zenx/utils.py,sha256=oMKKssN8VNSY7JJ7G04liOKuroPz4chLPJEHfpDeUF0,943
9
9
  zenx/clients/__init__.py,sha256=CaAAuNa8DPyMdejR0KNSDDg_UzC3WxaTol5_QvwwwG8,132
10
10
  zenx/clients/database.py,sha256=GaIafQ2OkYJQ_sz2f6u7sZKn77VOReWun09hogk6dO0,6019
11
- zenx/clients/http.py,sha256=MXXC0dYqd3mfuywsh4UMPXm0-9d0qoXHUky-ZC1DTE4,5692
11
+ zenx/clients/http.py,sha256=uhwEbrrOGtNclPYKQik4xtQuA61FvwOzWYSkt3VhQiA,6324
12
12
  zenx/pipelines/__init__.py,sha256=IxkZ0UpEJdYjLdd-PMcC9PzzzArTBNNcpgKc7NiOe5Y,131
13
13
  zenx/pipelines/base.py,sha256=N_388z5DFMeaU6wMwcClZAbQFWKh4kpAF7eUJhpQevs,1863
14
14
  zenx/pipelines/google_rpc.py,sha256=F7p5ml9W1UliZbrDrF9MFNVKlCP5pG1WpO6rdmBgKp8,4707
@@ -19,9 +19,9 @@ zenx/resources/proto/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hS
19
19
  zenx/resources/proto/feed_pb2.py,sha256=ZyICOLnyuXekkvV4bAHZ1nE1-wwzcYYRRrmRJCMrSoo,2810
20
20
  zenx/resources/proto/feed_pb2_grpc.py,sha256=Mim6FfBgIMj0PmTqHk036nVUMJH3A6I3ts6r1j3bQF8,7441
21
21
  zenx/spiders/__init__.py,sha256=rs5LuqdM2MQlUYiTGJrzkYhzN8_SSLTrR7wGjSRrrSo,25
22
- zenx/spiders/base.py,sha256=V_fOG_4XGXXbjTsi1T7_yXkQetHhceQ2vzoTAyaeDyI,2245
23
- zenx-0.6.4.dist-info/METADATA,sha256=87T6NmnGEsrajcvT6_XE-x8lOFbjb0OrtmRUVPZwLQs,1273
24
- zenx-0.6.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
25
- zenx-0.6.4.dist-info/entry_points.txt,sha256=8JXob2f1VtvzGFris-e9Usqywg7oca-cChDlH9moOZU,38
26
- zenx-0.6.4.dist-info/top_level.txt,sha256=JeXwvK86d7sB-2x-avugFnZIZa33zaHWKI8RHWJR6KY,5
27
- zenx-0.6.4.dist-info/RECORD,,
22
+ zenx/spiders/base.py,sha256=YB-KqsAzfIUTzDMy5_ElgW1mul-I4Ltft6JAJxpy4hg,1672
23
+ zenx-0.6.5.dist-info/METADATA,sha256=5Gbx1XogHN6sq_fFCcZeE8rK1W_IqGX-9Ema5jg1pDw,1273
24
+ zenx-0.6.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
25
+ zenx-0.6.5.dist-info/entry_points.txt,sha256=8JXob2f1VtvzGFris-e9Usqywg7oca-cChDlH9moOZU,38
26
+ zenx-0.6.5.dist-info/top_level.txt,sha256=JeXwvK86d7sB-2x-avugFnZIZa33zaHWKI8RHWJR6KY,5
27
+ zenx-0.6.5.dist-info/RECORD,,
File without changes