akshare-proxy-patch 0.4.0__tar.gz → 0.4.1__tar.gz

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 akshare-proxy-patch might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: akshare-proxy-patch
3
- Version: 0.4.0
3
+ Version: 0.4.1
4
4
  Summary: 针对 AkShare 和 efinance 的底层补丁,自动为东财等接口注入代理认证头,从而避免 `stock_zh_a_spot_em` 等方法报错。
5
5
  Home-page: https://github.com/helloyie/akshare-proxy-patch
6
6
  Author: cheapproxy.net
@@ -36,7 +36,7 @@ Dynamic: requires-python
36
36
  2. 安装 `akshare-proxy-patch` 插件
37
37
 
38
38
  ```
39
- pip install akshare-proxy-patch==0.4.0
39
+ pip install akshare-proxy-patch==0.4.1
40
40
  ```
41
41
 
42
42
  ## 🚀 使用方法(akshare / efinance / yfinance)
@@ -156,4 +156,4 @@ if __name__ == '__main__':
156
156
 
157
157
  如使用时遇到问题,或对插件有什么意见或建议,可进群交流:
158
158
 
159
- ![YpRjixvpykLmzPtrX0kIkdPlvffOFJvF.webp](https://cdn.nodeimage.com/i/YpRjixvpykLmzPtrX0kIkdPlvffOFJvF.webp)
159
+ ![Knfv1JVAEBWyuaZc79CJKJALR7BVUgpH.webp](https://cdn.nodeimage.com/i/Knfv1JVAEBWyuaZc79CJKJALR7BVUgpH.webp)
@@ -15,7 +15,7 @@
15
15
  2. 安装 `akshare-proxy-patch` 插件
16
16
 
17
17
  ```
18
- pip install akshare-proxy-patch==0.4.0
18
+ pip install akshare-proxy-patch==0.4.1
19
19
  ```
20
20
 
21
21
  ## 🚀 使用方法(akshare / efinance / yfinance)
@@ -135,4 +135,4 @@ if __name__ == '__main__':
135
135
 
136
136
  如使用时遇到问题,或对插件有什么意见或建议,可进群交流:
137
137
 
138
- ![YpRjixvpykLmzPtrX0kIkdPlvffOFJvF.webp](https://cdn.nodeimage.com/i/YpRjixvpykLmzPtrX0kIkdPlvffOFJvF.webp)
138
+ ![Knfv1JVAEBWyuaZc79CJKJALR7BVUgpH.webp](https://cdn.nodeimage.com/i/Knfv1JVAEBWyuaZc79CJKJALR7BVUgpH.webp)
@@ -13,7 +13,7 @@ except ImportError:
13
13
 
14
14
  import requests as std_requests
15
15
 
16
- __version__ = "0.4.0"
16
+ __version__ = "0.4.1"
17
17
 
18
18
  # 授权接口固定使用标准 requests,避免干扰
19
19
  _auth_session = std_requests.Session()
@@ -71,53 +71,40 @@ default_hook_domains = [
71
71
  def install_patch(
72
72
  auth_ip, auth_token="", retry=30, hook_domains=default_hook_domains, timeout=5
73
73
  ):
74
- # 获取原始的 curl_cffi Session 类
75
- # 1. 解决 Linux 下的 AttributeError: 'curl_cffi.requests' has no attribute 'adapters'
76
- # 动态将标准 requests 的子模块挂载到 curl_requests 上
77
- needed_submodules = [
78
- "adapters",
79
- "auth",
80
- "cookies",
81
- "exceptions",
82
- "models",
83
- "status_codes",
84
- "structures",
85
- "utils",
86
- "packages",
87
- ]
88
-
89
- setattr(curl_requests, "RequestException", RequestException)
90
- for sub in needed_submodules:
91
- if not hasattr(curl_requests, sub):
92
- # 优先从标准 requests 借用这些定义,保证 akshare 里的 isinstance 或常量检查能过
93
- setattr(curl_requests, sub, getattr(std_requests, sub, None))
94
-
95
- _BaseSession = curl_requests.Session
96
-
97
- # 定义兼容 Windows 调用的子类
98
- class PatchedSession(_BaseSession):
99
- def mount(self, prefix, adapter):
100
- pass
74
+ # --- 核心改进:备份原始 Session 避开递归死循环 ---
75
+ if not hasattr(std_requests, "_OriginalSession"):
76
+ std_requests._OriginalSession = std_requests.Session
77
+
78
+ _CFFIBaseSession = curl_requests.Session
101
79
 
80
+ # 这里的基类改为 std_requests.Session,确保对第三方库的 isinstance 检查通过
81
+ class PatchedSession(std_requests._OriginalSession):
102
82
  def request(self, method, url, **kwargs):
103
83
  is_target = any(d in (url or "") for d in hook_domains)
104
84
  clean_url = (url or "").split("?")[0].lower()
85
+
86
+ # --- 情况 1:非目标域名,使用原始 requests 逻辑 ---
105
87
  if (
106
88
  not is_target
107
89
  or clean_url.endswith(".js")
108
90
  or clean_url.endswith(".html")
109
91
  ):
110
- return super().request(method, url, **kwargs)
92
+ kwargs.pop("impersonate", None) # 移除 curl_cffi 特有参数
93
+ # 显式调用备份的原始 Session 方法,彻底避免递归
94
+ return std_requests._OriginalSession.request(
95
+ self, method, url, **kwargs
96
+ )
111
97
 
98
+ # --- 情况 2:目标域名,逻辑保持不变 (走 curl_cffi) ---
112
99
  auth_url = f"http://{auth_ip}:47001/api/akshare-auth"
113
100
 
101
+ # 这里的逻辑和之前完全一致
114
102
  for i in range(retry):
115
103
  auth_res = get_auth_config_with_cache(auth_url, auth_token)
116
104
  if not auth_res:
117
105
  time.sleep(0.05)
118
106
  continue
119
107
 
120
- # 适配参数
121
108
  headers = kwargs.get("headers") or {}
122
109
  headers["User-Agent"] = auth_res["ua"]
123
110
  kwargs["headers"] = headers
@@ -126,28 +113,24 @@ def install_patch(
126
113
  "https": auth_res["proxy"],
127
114
  }
128
115
  kwargs["timeout"] = timeout
129
-
130
116
  kwargs["impersonate"] = random.choice(target_browsers)
131
- # print(auth_res["proxy"], i, "=============")
132
117
 
133
118
  try:
134
- # 关键:调用 curl_cffi 的原生 C 实现
135
- resp = super().request(method, url, **kwargs)
136
- if resp.status_code == 200:
137
- # 业务逻辑验证...
138
- return resp
139
- with _cache.lock:
140
- _cache.expire_at = 0
119
+ # 对于目标域名,利用 curl_cffi 的 Session 来发起请求
120
+ with _CFFIBaseSession() as cffi_s:
121
+ resp = cffi_s.request(method, url, **kwargs)
122
+ if resp.status_code == 200:
123
+ return resp
124
+ with _cache.lock:
125
+ _cache.expire_at = 0
141
126
  except:
142
127
  with _cache.lock:
143
128
  _cache.expire_at = 0
144
129
  time.sleep(0.05)
145
130
 
146
- return super().request(method, url, **kwargs)
131
+ return std_requests._OriginalSession.request(self, method, url, **kwargs)
147
132
 
148
- # --- 核心改造:针对 Windows 的原地注入 ---
149
-
150
- # 定义顶层包装函数
133
+ # 定义补丁包装函数
151
134
  def patched_get(url, params=None, **kwargs):
152
135
  with PatchedSession() as s:
153
136
  return s.get(url, params=params, **kwargs)
@@ -156,23 +139,22 @@ def install_patch(
156
139
  with PatchedSession() as s:
157
140
  return s.post(url, data=data, json=json, **kwargs)
158
141
 
159
- # 1. 替换 curl_requests 的成员
142
+ def patched_request(method, url, **kwargs):
143
+ with PatchedSession() as s:
144
+ return s.request(method, url, **kwargs)
145
+
146
+ # --- 最小化原地注入 ---
147
+ # 仅修改标准 requests 模块的成员引用,不替换 sys.modules["requests"]
148
+ std_requests.Session = PatchedSession
149
+ std_requests.get = patched_get
150
+ std_requests.post = patched_post
151
+ std_requests.request = patched_request
152
+
153
+ # 同步修改 curl_requests 以保持库内部一致性
160
154
  curl_requests.Session = PatchedSession
161
155
  curl_requests.get = patched_get
162
156
  curl_requests.post = patched_post
163
157
 
164
- # 2. 【核心】原地修改 sys.modules 中的 requests 对象
165
- # 即使 akshare 已经加载了 requests,我们直接修改 requests 模块的内容
166
- if "requests" in sys.modules:
167
- req_mod = sys.modules["requests"]
168
- req_mod.Session = PatchedSession
169
- req_mod.get = patched_get
170
- req_mod.post = patched_post
171
- req_mod.request = patched_get # 部分库直接调 request
172
-
173
- # 3. 确保后续导入重定向
174
- sys.modules["requests"] = curl_requests
175
-
176
158
 
177
159
  def install_yfinance_patch(
178
160
  auth_ip, auth_token="", retry=30, hook_domains=["finance.yahoo.com"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: akshare-proxy-patch
3
- Version: 0.4.0
3
+ Version: 0.4.1
4
4
  Summary: 针对 AkShare 和 efinance 的底层补丁,自动为东财等接口注入代理认证头,从而避免 `stock_zh_a_spot_em` 等方法报错。
5
5
  Home-page: https://github.com/helloyie/akshare-proxy-patch
6
6
  Author: cheapproxy.net
@@ -36,7 +36,7 @@ Dynamic: requires-python
36
36
  2. 安装 `akshare-proxy-patch` 插件
37
37
 
38
38
  ```
39
- pip install akshare-proxy-patch==0.4.0
39
+ pip install akshare-proxy-patch==0.4.1
40
40
  ```
41
41
 
42
42
  ## 🚀 使用方法(akshare / efinance / yfinance)
@@ -156,4 +156,4 @@ if __name__ == '__main__':
156
156
 
157
157
  如使用时遇到问题,或对插件有什么意见或建议,可进群交流:
158
158
 
159
- ![YpRjixvpykLmzPtrX0kIkdPlvffOFJvF.webp](https://cdn.nodeimage.com/i/YpRjixvpykLmzPtrX0kIkdPlvffOFJvF.webp)
159
+ ![Knfv1JVAEBWyuaZc79CJKJALR7BVUgpH.webp](https://cdn.nodeimage.com/i/Knfv1JVAEBWyuaZc79CJKJALR7BVUgpH.webp)
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "akshare-proxy-patch" # 包名,需在 PyPI 唯一
7
- version = "0.4.0"
7
+ version = "0.4.1"
8
8
  authors = [
9
9
  { name="cheapproxy", email="emezxyj1@gmail.com" },
10
10
  ]
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name="akshare-proxy-patch", # 包名,用户 pip install 时的名称
8
- version="0.4.0",
8
+ version="0.4.1",
9
9
  author="cheapproxy.net",
10
10
  author_email="emezxyj1@gmail.com",
11
11
  description="AkShare 和 efinance 报错全局Hook",