akshare-proxy-patch 0.2.2__tar.gz → 0.2.3__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.2.2
3
+ Version: 0.2.3
4
4
  Summary: 针对 AkShare 的底层补丁,自动为东财等接口注入代理认证头,从而避免 `stock_zh_a_spot_em` 等方法报错。
5
5
  Home-page: https://github.com/helloyie/akshare-proxy-patch
6
6
  Author: cheapproxy.net
@@ -2,11 +2,10 @@ import time
2
2
  import threading
3
3
  import requests
4
4
  from requests.adapters import HTTPAdapter
5
- from copy import deepcopy
6
5
 
7
- # --- 全局配置与状态 ---
8
- __version__ = "1.0.2"
9
- _original_send = HTTPAdapter.send
6
+ __version__ = "0.2.3"
7
+ # 备份 Session 的原始 request 方法,这是所有 requests.get/post 的最终入口
8
+ _original_request = requests.Session.request
10
9
  _auth_session = requests.Session()
11
10
 
12
11
 
@@ -23,14 +22,12 @@ _cache = AuthCache()
23
22
 
24
23
  def get_auth_config_with_cache(auth_url, auth_token):
25
24
  now = time.time()
26
-
27
- # 检查缓存是否有效 (双重检查锁前置)
25
+ # 1. 检查缓存是否有效
28
26
  if _cache.data and now < _cache.expire_at:
29
27
  return _cache.data
30
28
 
31
- # 缓存失效,加锁更新
29
+ # 2. 缓存失效,加锁更新
32
30
  with _cache.lock:
33
- # 再次检查,防止多个线程同时穿透锁
34
31
  if _cache.data and now < _cache.expire_at:
35
32
  return _cache.data
36
33
 
@@ -53,48 +50,67 @@ def get_auth_config_with_cache(auth_url, auth_token):
53
50
 
54
51
 
55
52
  def install_patch(auth_ip, auth_token):
56
- def patched_send(self, request, **kwargs):
57
- # 排除非目标域名,减少性能损耗
58
- url = request.url or ""
59
- if not any(
60
- d in url
53
+ def patched_request(self, method, url, **kwargs):
54
+ # 排除非目标域名
55
+ is_target = any(
56
+ d in (url or "")
61
57
  for d in [
62
58
  "fund.eastmoney.com",
63
59
  "push2.eastmoney.com",
64
60
  "push2his.eastmoney.com",
65
61
  ]
66
- ):
67
- return _original_send(self, request, **kwargs)
62
+ )
63
+
64
+ if not is_target:
65
+ return _original_request(self, method, url, **kwargs)
68
66
 
69
67
  auth_url = f"http://{auth_ip}:47001/api/akshare-auth"
70
68
 
71
- for _ in range(30):
69
+ # 重试逻辑
70
+ for _ in range(10):
72
71
  auth_res = get_auth_config_with_cache(auth_url, auth_token)
73
72
  if not auth_res:
74
73
  time.sleep(0.5)
75
74
  continue
76
75
 
77
- request.headers["User-Agent"] = auth_res["ua"]
78
- request.headers["Cookie"] = (
76
+ # 处理 Headers:确保不破坏业务代码传入的 headers
77
+ headers = kwargs.get("headers") or {}
78
+ headers["User-Agent"] = auth_res["ua"]
79
+ headers["Cookie"] = (
79
80
  f"nid18={auth_res['nid18']}; nid18_create_time={auth_res['nid18_create_time']}"
80
81
  )
81
-
82
- # 动态注入代理
82
+ kwargs["headers"] = headers
83
83
  kwargs["proxies"] = {
84
84
  "http": auth_res["proxy"],
85
85
  "https": auth_res["proxy"],
86
86
  }
87
- kwargs["timeout"] = 8
87
+
88
+ if "timeout" not in kwargs:
89
+ kwargs["timeout"] = 8
88
90
 
89
91
  try:
90
- resp = _original_send(self, request, **kwargs)
92
+ # 调用原始 request 方法
93
+ resp = _original_request(self, method, url, **kwargs)
91
94
  if resp.ok:
92
95
  return resp
96
+
97
+ # 如果遇到 403 等可能是代理失效
98
+ if resp.status_code == 403:
99
+ with _cache.lock:
100
+ _cache.expire_at = 0
93
101
  except Exception:
102
+ # 网络异常直接使缓存失效,强制下一个请求换 IP
94
103
  with _cache.lock:
95
104
  _cache.expire_at = 0
96
105
  time.sleep(0.1)
97
106
 
98
- return _original_send(self, request, **kwargs)
107
+ # 最终尝试
108
+ return _original_request(self, method, url, **kwargs)
109
+
110
+ # 关键:全局替换 Session 的 request 入口
111
+ requests.Session.request = patched_request
112
+
99
113
 
100
- HTTPAdapter.send = patched_send
114
+ # 使用示例
115
+ # install_patch("你的IP", "你的TOKEN")
116
+ # requests.get("http://fund.eastmoney.com/...")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: akshare-proxy-patch
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: 针对 AkShare 的底层补丁,自动为东财等接口注入代理认证头,从而避免 `stock_zh_a_spot_em` 等方法报错。
5
5
  Home-page: https://github.com/helloyie/akshare-proxy-patch
6
6
  Author: cheapproxy.net
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "akshare-proxy-patch" # 包名,需在 PyPI 唯一
7
- version = "0.2.2"
7
+ version = "0.2.3"
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.2.2", # 版本号
8
+ version="0.2.3", # 版本号
9
9
  author="cheapproxy.net",
10
10
  author_email="emezxyj1@gmail.com",
11
11
  description="AkShare 报错全局Hook",