akshare-proxy-patch 0.1.5__tar.gz → 0.2.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.1.5
3
+ Version: 0.2.1
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
@@ -30,7 +30,7 @@ pip install --upgrade akshare-proxy-patch
30
30
 
31
31
  ## 使用
32
32
 
33
- 1. 访问 https://cheapproxy.net 联系客服(备注: token),获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
33
+ 1. 登录 https://cheapproxy.net,获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
34
34
 
35
35
  2. 将 akshare 升级到最新
36
36
 
@@ -40,16 +40,24 @@ pip install --upgrade akshare
40
40
 
41
41
  3. 在使用 akshare 文件顶部添加下面的内容
42
42
  ```
43
+ # 文件顶部添加
43
44
  import akshare_proxy_patch
44
45
 
45
46
  akshare_proxy_patch.install_patch("AUTH_IP", "AUTH_TOKEN")
46
47
 
47
-
48
+ # 你的正常业务代码......
48
49
  # import akshare as ak
49
50
  # df = ak.stock_zh_a_spot_em()
50
- # 你的其他代码......
51
51
  ```
52
52
 
53
- # 使用问题交流群
53
+ ## 目前Hook的接口域名清单
54
+ - fund.eastmoney.com
55
+ - push2.eastmoney.com
56
+ - push2his.eastmoney.com
57
+
58
+ ## TODO
59
+ - 多线程情况下容易出现问题,需暂时不使用多线程或减少线程数
60
+
61
+ ## 使用问题交流群
54
62
 
55
63
  ![8jmzvNOjKWFzwNUDW0Xiytcpfr50uMr5.webp](https://cdn.nodeimage.com/i/8jmzvNOjKWFzwNUDW0Xiytcpfr50uMr5.webp)
@@ -9,7 +9,7 @@ pip install --upgrade akshare-proxy-patch
9
9
 
10
10
  ## 使用
11
11
 
12
- 1. 访问 https://cheapproxy.net 联系客服(备注: token),获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
12
+ 1. 登录 https://cheapproxy.net,获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
13
13
 
14
14
  2. 将 akshare 升级到最新
15
15
 
@@ -19,16 +19,24 @@ pip install --upgrade akshare
19
19
 
20
20
  3. 在使用 akshare 文件顶部添加下面的内容
21
21
  ```
22
+ # 文件顶部添加
22
23
  import akshare_proxy_patch
23
24
 
24
25
  akshare_proxy_patch.install_patch("AUTH_IP", "AUTH_TOKEN")
25
26
 
26
-
27
+ # 你的正常业务代码......
27
28
  # import akshare as ak
28
29
  # df = ak.stock_zh_a_spot_em()
29
- # 你的其他代码......
30
30
  ```
31
31
 
32
- # 使用问题交流群
32
+ ## 目前Hook的接口域名清单
33
+ - fund.eastmoney.com
34
+ - push2.eastmoney.com
35
+ - push2his.eastmoney.com
36
+
37
+ ## TODO
38
+ - 多线程情况下容易出现问题,需暂时不使用多线程或减少线程数
39
+
40
+ ## 使用问题交流群
33
41
 
34
42
  ![8jmzvNOjKWFzwNUDW0Xiytcpfr50uMr5.webp](https://cdn.nodeimage.com/i/8jmzvNOjKWFzwNUDW0Xiytcpfr50uMr5.webp)
@@ -0,0 +1,93 @@
1
+ import time
2
+ import threading
3
+ import requests
4
+ from copy import deepcopy
5
+ from requests.adapters import HTTPAdapter
6
+
7
+ # 备份原始 send
8
+ _original_send = HTTPAdapter.send
9
+
10
+ # 线程局部状态
11
+ _thread_state = threading.local()
12
+
13
+ # 独立 Session:专门用于授权请求(不走 hook)
14
+ _auth_session = requests.Session()
15
+
16
+
17
+ def get_auth_config_realtime(auth_url, auth_token):
18
+ """实时获取授权信息(不走全局 hook)"""
19
+ try:
20
+ resp = _auth_session.get(
21
+ auth_url,
22
+ params={"token": auth_token},
23
+ timeout=5,
24
+ )
25
+ data = resp.json()
26
+ if data.get("ua"):
27
+ return data
28
+ error_msg = (
29
+ data.get("error_msg")
30
+ or "接口授权失败,请登录 https://cheapproxy.net 联系客服获取授权码"
31
+ )
32
+ print(f"========={error_msg}========")
33
+ except Exception as e:
34
+ print(f"=========接口授权失败,{e}========")
35
+ return None
36
+
37
+
38
+ def install_patch(auth_ip, auth_token):
39
+ def patched_send(self, request, **kwargs):
40
+ # 如果明确标记跳过 hook,直接原逻辑
41
+ if getattr(_thread_state, "skip_hook", False):
42
+ return _original_send(self, request, **kwargs)
43
+
44
+ url = request.url or ""
45
+
46
+ is_target = any(
47
+ d in url
48
+ for d in (
49
+ "fund.eastmoney.com",
50
+ "push2.eastmoney.com",
51
+ "push2his.eastmoney.com",
52
+ )
53
+ )
54
+
55
+ # 非目标域名直接放行
56
+ if not is_target:
57
+ return _original_send(self, request, **kwargs)
58
+
59
+ auth_url = f"http://{auth_ip}:47001/api/akshare-auth"
60
+
61
+ # 每次请求都用“独立副本”
62
+ new_request = deepcopy(request)
63
+ new_kwargs = deepcopy(kwargs)
64
+
65
+ for _ in range(30):
66
+ try:
67
+ auth_res = get_auth_config_realtime(auth_url, auth_token)
68
+ if auth_res:
69
+ new_request.headers = deepcopy(request.headers)
70
+ new_request.headers["User-Agent"] = auth_res["ua"]
71
+ new_request.headers["Cookie"] = (
72
+ f"nid18={auth_res['nid18']}; "
73
+ f"nid18_create_time={auth_res['nid18_create_time']}"
74
+ )
75
+
76
+ proxy = auth_res["proxy"]
77
+ new_kwargs["proxies"] = {
78
+ "http": proxy,
79
+ "https": proxy,
80
+ }
81
+ new_kwargs["timeout"] = 8
82
+
83
+ resp = _original_send(self, new_request, **new_kwargs)
84
+ if resp.ok:
85
+ return resp
86
+
87
+ except Exception:
88
+ time.sleep(0.01)
89
+
90
+ # 兜底:30 次都失败,走原请求
91
+ return _original_send(self, request, **kwargs)
92
+
93
+ HTTPAdapter.send = patched_send
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: akshare-proxy-patch
3
- Version: 0.1.5
3
+ Version: 0.2.1
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
@@ -30,7 +30,7 @@ pip install --upgrade akshare-proxy-patch
30
30
 
31
31
  ## 使用
32
32
 
33
- 1. 访问 https://cheapproxy.net 联系客服(备注: token),获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
33
+ 1. 登录 https://cheapproxy.net,获取授权码 `AUTH_IP` 和 `AUTH_TOKEN`
34
34
 
35
35
  2. 将 akshare 升级到最新
36
36
 
@@ -40,16 +40,24 @@ pip install --upgrade akshare
40
40
 
41
41
  3. 在使用 akshare 文件顶部添加下面的内容
42
42
  ```
43
+ # 文件顶部添加
43
44
  import akshare_proxy_patch
44
45
 
45
46
  akshare_proxy_patch.install_patch("AUTH_IP", "AUTH_TOKEN")
46
47
 
47
-
48
+ # 你的正常业务代码......
48
49
  # import akshare as ak
49
50
  # df = ak.stock_zh_a_spot_em()
50
- # 你的其他代码......
51
51
  ```
52
52
 
53
- # 使用问题交流群
53
+ ## 目前Hook的接口域名清单
54
+ - fund.eastmoney.com
55
+ - push2.eastmoney.com
56
+ - push2his.eastmoney.com
57
+
58
+ ## TODO
59
+ - 多线程情况下容易出现问题,需暂时不使用多线程或减少线程数
60
+
61
+ ## 使用问题交流群
54
62
 
55
63
  ![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.1.5"
7
+ version = "0.2.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.1.5", # 版本号
8
+ version="0.2.1", # 版本号
9
9
  author="cheapproxy.net",
10
10
  author_email="emezxyj1@gmail.com",
11
11
  description="AkShare 报错全局Hook",
@@ -1,49 +0,0 @@
1
- import time
2
- import requests
3
- from requests.adapters import HTTPAdapter
4
-
5
- original_send = HTTPAdapter.send
6
-
7
-
8
- def install_patch(auth_ip, auth_token):
9
- def patched_send(self, request, **kwargs):
10
- auth_url = f"http://{auth_ip}:47001/api/akshare-auth"
11
- url = request.url
12
- is_hook = (
13
- ("fund.eastmoney.com" in url)
14
- or ("push2.eastmoney.com" in url)
15
- or ("push2his.eastmoney.com" in url)
16
- )
17
- for i in range(60):
18
- try:
19
- if not is_hook:
20
- return original_send(self, request, **kwargs)
21
- with requests.Session() as s:
22
- HTTPAdapter.send = original_send
23
- auth_res = s.get(
24
- auth_url, params={"token": auth_token}, timeout=5
25
- ).json()
26
- if not auth_res["ua"]:
27
- error_msg = (
28
- auth_res.get("error_msg", "")
29
- or "未知异常,请联系 cheapproxy.net 客服"
30
- )
31
- print(error_msg)
32
- return None
33
-
34
- HTTPAdapter.send = patched_send
35
- request.headers["User-Agent"] = auth_res["ua"]
36
- cookie_str = f"nid18={auth_res['nid18']}; nid18_create_time={auth_res['nid18_create_time']}"
37
- request.headers["Cookie"] = cookie_str
38
- proxy = auth_res["proxy"]
39
- kwargs["proxies"] = {"http": proxy, "https": proxy}
40
- kwargs["timeout"] = 8
41
- return original_send(self, request, **kwargs)
42
- except Exception as e:
43
- if not is_hook:
44
- time.sleep(0.5)
45
- HTTPAdapter.send = patched_send
46
-
47
- return original_send(self, request, **kwargs)
48
-
49
- HTTPAdapter.send = patched_send