Skip to content

Commit 71fbf93

Browse files
committed
fix: update request proxy
1 parent a61bdc8 commit 71fbf93

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

douyinliverecorder/room.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
Author: Hmily
55
GitHub:https://github.com/ihmily
66
Date: 2023-07-17 23:52:05
7-
Update: 2024-10-08 23:35:00
7+
Update: 2025-01-27 22:08:00
88
Copyright (c) 2023 by Hmily, All Rights Reserved.
99
"""
1010
import json
1111
import re
1212
import urllib.parse
1313
import execjs
1414
import httpx
15-
import requests
1615
import urllib.request
1716
from . import JS_SCRIPT_PATH
17+
from .utils import handle_proxy_addr
1818

1919
no_proxy_handler = urllib.request.ProxyHandler({})
2020
opener = urllib.request.build_opener(no_proxy_handler)
@@ -51,7 +51,8 @@ async def get_sec_user_id(url: str, proxy_addr: str | None = None, headers: dict
5151
headers = HEADERS
5252

5353
try:
54-
async with httpx.AsyncClient(proxies=proxy_addr, timeout=15) as client:
54+
proxy_addr = handle_proxy_addr(proxy_addr)
55+
async with httpx.AsyncClient(proxy=proxy_addr, timeout=15) as client:
5556
response = await client.get(url, headers=headers, follow_redirects=True)
5657

5758
redirect_url = response.url
@@ -78,7 +79,8 @@ async def get_unique_id(url: str, proxy_addr: str | None = None, headers: dict |
7879
headers = HEADERS_PC
7980

8081
try:
81-
async with httpx.AsyncClient(proxies=proxy_addr, timeout=15) as client:
82+
proxy_addr = handle_proxy_addr(proxy_addr)
83+
async with httpx.AsyncClient(proxy=proxy_addr, timeout=15) as client:
8284
# 第一次请求,获取重定向后的URL以提取sec_user_id
8385
response = await client.get(url, headers=headers, follow_redirects=True)
8486
redirect_url = str(response.url)
@@ -126,7 +128,8 @@ async def get_live_room_id(room_id: str, sec_user_id: str, proxy_addr: str | Non
126128
api = api + "&X-Bogus=" + xbogus
127129

128130
try:
129-
async with httpx.AsyncClient(proxies={"http://": proxy_addr, "https://": proxy_addr} if proxy_addr else None,
131+
proxy_addr = handle_proxy_addr(proxy_addr)
132+
async with httpx.AsyncClient(proxy=proxy_addr,
130133
timeout=15) as client:
131134
response = await client.get(api, headers=headers)
132135
response.raise_for_status() # 检查HTTP响应状态码是否表示成功

douyinliverecorder/spider.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Author: Hmily
55
GitHub: https://github.com/ihmily
66
Date: 2023-07-15 23:15:00
7-
Update: 2025-01-27 13:17:16
7+
Update: 2025-01-27 22:08:16
88
Copyright (c) 2023-2024 by Hmily, All Rights Reserved.
99
Function: Get live stream data.
1010
"""
@@ -26,7 +26,7 @@
2626
import execjs
2727
import urllib.request
2828
from .utils import (
29-
trace_error_decorator, dict_to_cookie_str
29+
trace_error_decorator, dict_to_cookie_str, handle_proxy_addr
3030
)
3131
from .logger import script_path
3232
from .room import get_sec_user_id, get_unique_id
@@ -57,10 +57,7 @@ async def async_req(
5757
if headers is None:
5858
headers = {}
5959
try:
60-
if proxy_addr:
61-
if not proxy_addr.startswith('http'):
62-
proxy_addr = 'http://' + proxy_addr
63-
60+
proxy_addr = handle_proxy_addr(proxy_addr)
6461
if data or json_data:
6562
async with httpx.AsyncClient(proxy=proxy_addr, timeout=timeout) as client:
6663
response = await client.post(url, data=data, json=json_data, headers=headers)
@@ -156,6 +153,7 @@ async def get_response_status(url: str, proxy_addr: OptionalStr = None, headers:
156153
abroad: bool = False) -> bool:
157154

158155
try:
156+
proxy_addr = handle_proxy_addr(proxy_addr)
159157
async with httpx.AsyncClient(proxy=proxy_addr, timeout=timeout) as client:
160158
response = await client.head(url, headers=headers, follow_redirects=True)
161159
return response.status_code == 200
@@ -1638,6 +1636,7 @@ async def login_popkontv(
16381636
url = 'https://www.popkontv.com/api/proxy/member/v1/login'
16391637

16401638
try:
1639+
proxy_addr = handle_proxy_addr(proxy_addr)
16411640
async with httpx.AsyncClient(proxy=proxy_addr, timeout=20) as client:
16421641
response = await client.post(url, json=data, headers=headers)
16431642
response.raise_for_status()

douyinliverecorder/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,12 @@ def check_disk_capacity(file_path: str | Path, show: bool = False) -> float:
151151
f"Used: {disk_usage.used / (1024 ** 3):.2f} GB "
152152
f"Free: {free_space_gb:.2f} GB\n")
153153
return free_space_gb
154+
155+
156+
def handle_proxy_addr(proxy_addr):
157+
if proxy_addr:
158+
if not proxy_addr.startswith('http'):
159+
proxy_addr = 'http://' + proxy_addr
160+
else:
161+
proxy_addr = None
162+
return proxy_addr

0 commit comments

Comments
 (0)