akshare-proxy-patch 0.2.2__tar.gz → 0.2.4__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.4
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
@@ -24,21 +24,24 @@ Dynamic: requires-python
24
24
 
25
25
  ## 安装
26
26
 
27
+ 1. 将官方 `akshare` 升级到最新
28
+
27
29
  ```
28
- pip install --upgrade akshare-proxy-patch
30
+ pip install upgrade akshare
29
31
  ```
30
32
 
31
- ## 使用
33
+ 2. 安装最新版本的 `akshare-proxy-patch`
34
+ ```
35
+ pip install akshare-proxy-patch==0.2.3
36
+ ```
32
37
 
33
- 1. 登录 https://cheapproxy.net,获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
38
+ 最好默认用国外源,国内源有时不是最新的
34
39
 
35
- 2. 将 akshare 升级到最新
40
+ ## 使用
36
41
 
37
- ```
38
- pip install --upgrade akshare
39
- ```
42
+ 1. 登录 https://cheapproxy.net,获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
40
43
 
41
- 3. 在使用 akshare 文件顶部添加下面的内容
44
+ 2. 在使用 akshare 文件顶部添加下面的内容
42
45
  ```
43
46
  # 文件顶部添加
44
47
  import akshare_proxy_patch
@@ -55,9 +58,8 @@ akshare_proxy_patch.install_patch("AUTH_IP", "AUTH_TOKEN")
55
58
  - push2.eastmoney.com
56
59
  - push2his.eastmoney.com
57
60
 
58
- ## TODO
59
- - 多线程情况下容易出现问题,需暂时不使用多线程或减少线程数
60
-
61
61
  ## 使用问题交流群
62
62
 
63
+ 如遇到部分接口报错,可进群交流:
64
+
63
65
  ![8jmzvNOjKWFzwNUDW0Xiytcpfr50uMr5.webp](https://cdn.nodeimage.com/i/8jmzvNOjKWFzwNUDW0Xiytcpfr50uMr5.webp)
@@ -3,21 +3,24 @@
3
3
 
4
4
  ## 安装
5
5
 
6
+ 1. 将官方 `akshare` 升级到最新
7
+
6
8
  ```
7
- pip install --upgrade akshare-proxy-patch
9
+ pip install upgrade akshare
8
10
  ```
9
11
 
10
- ## 使用
12
+ 2. 安装最新版本的 `akshare-proxy-patch`
13
+ ```
14
+ pip install akshare-proxy-patch==0.2.3
15
+ ```
11
16
 
12
- 1. 登录 https://cheapproxy.net,获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
17
+ 最好默认用国外源,国内源有时不是最新的
13
18
 
14
- 2. 将 akshare 升级到最新
19
+ ## 使用
15
20
 
16
- ```
17
- pip install --upgrade akshare
18
- ```
21
+ 1. 登录 https://cheapproxy.net,获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
19
22
 
20
- 3. 在使用 akshare 文件顶部添加下面的内容
23
+ 2. 在使用 akshare 文件顶部添加下面的内容
21
24
  ```
22
25
  # 文件顶部添加
23
26
  import akshare_proxy_patch
@@ -34,9 +37,8 @@ akshare_proxy_patch.install_patch("AUTH_IP", "AUTH_TOKEN")
34
37
  - push2.eastmoney.com
35
38
  - push2his.eastmoney.com
36
39
 
37
- ## TODO
38
- - 多线程情况下容易出现问题,需暂时不使用多线程或减少线程数
39
-
40
40
  ## 使用问题交流群
41
41
 
42
+ 如遇到部分接口报错,可进群交流:
43
+
42
44
  ![8jmzvNOjKWFzwNUDW0Xiytcpfr50uMr5.webp](https://cdn.nodeimage.com/i/8jmzvNOjKWFzwNUDW0Xiytcpfr50uMr5.webp)
@@ -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.4"
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,66 @@ 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
+
87
88
  kwargs["timeout"] = 8
88
89
 
89
90
  try:
90
- resp = _original_send(self, request, **kwargs)
91
+ # 调用原始 request 方法
92
+ resp = _original_request(self, method, url, **kwargs)
91
93
  if resp.ok:
92
94
  return resp
95
+
96
+ # 如果遇到 403 等可能是代理失效
97
+ if resp.status_code == 403:
98
+ with _cache.lock:
99
+ _cache.expire_at = 0
93
100
  except Exception:
101
+ # 网络异常直接使缓存失效,强制下一个请求换 IP
94
102
  with _cache.lock:
95
103
  _cache.expire_at = 0
96
104
  time.sleep(0.1)
97
105
 
98
- return _original_send(self, request, **kwargs)
106
+ # 最终尝试
107
+ return _original_request(self, method, url, **kwargs)
108
+
109
+ # 关键:全局替换 Session 的 request 入口
110
+ requests.Session.request = patched_request
111
+
99
112
 
100
- HTTPAdapter.send = patched_send
113
+ # 使用示例
114
+ # install_patch("你的IP", "你的TOKEN")
115
+ # 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.4
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
@@ -24,21 +24,24 @@ Dynamic: requires-python
24
24
 
25
25
  ## 安装
26
26
 
27
+ 1. 将官方 `akshare` 升级到最新
28
+
27
29
  ```
28
- pip install --upgrade akshare-proxy-patch
30
+ pip install upgrade akshare
29
31
  ```
30
32
 
31
- ## 使用
33
+ 2. 安装最新版本的 `akshare-proxy-patch`
34
+ ```
35
+ pip install akshare-proxy-patch==0.2.3
36
+ ```
32
37
 
33
- 1. 登录 https://cheapproxy.net,获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
38
+ 最好默认用国外源,国内源有时不是最新的
34
39
 
35
- 2. 将 akshare 升级到最新
40
+ ## 使用
36
41
 
37
- ```
38
- pip install --upgrade akshare
39
- ```
42
+ 1. 登录 https://cheapproxy.net,获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
40
43
 
41
- 3. 在使用 akshare 文件顶部添加下面的内容
44
+ 2. 在使用 akshare 文件顶部添加下面的内容
42
45
  ```
43
46
  # 文件顶部添加
44
47
  import akshare_proxy_patch
@@ -55,9 +58,8 @@ akshare_proxy_patch.install_patch("AUTH_IP", "AUTH_TOKEN")
55
58
  - push2.eastmoney.com
56
59
  - push2his.eastmoney.com
57
60
 
58
- ## TODO
59
- - 多线程情况下容易出现问题,需暂时不使用多线程或减少线程数
60
-
61
61
  ## 使用问题交流群
62
62
 
63
+ 如遇到部分接口报错,可进群交流:
64
+
63
65
  ![8jmzvNOjKWFzwNUDW0Xiytcpfr50uMr5.webp](https://cdn.nodeimage.com/i/8jmzvNOjKWFzwNUDW0Xiytcpfr50uMr5.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.2.2"
7
+ version = "0.2.4"
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.4", # 版本号
9
9
  author="cheapproxy.net",
10
10
  author_email="emezxyj1@gmail.com",
11
11
  description="AkShare 报错全局Hook",