diff --git a/douyinliverecorder/room.py b/douyinliverecorder/room.py index b856fe63..7fc9de76 100644 --- a/douyinliverecorder/room.py +++ b/douyinliverecorder/room.py @@ -4,7 +4,7 @@ Author: Hmily GitHub:https://github.com/ihmily Date: 2023-07-17 23:52:05 -Update: 2024-10-08 23:35:00 +Update: 2025-01-27 22:08:00 Copyright (c) 2023 by Hmily, All Rights Reserved. """ import json @@ -12,9 +12,9 @@ import urllib.parse import execjs import httpx -import requests import urllib.request from . import JS_SCRIPT_PATH +from .utils import handle_proxy_addr no_proxy_handler = urllib.request.ProxyHandler({}) 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 headers = HEADERS try: - async with httpx.AsyncClient(proxies=proxy_addr, timeout=15) as client: + proxy_addr = handle_proxy_addr(proxy_addr) + async with httpx.AsyncClient(proxy=proxy_addr, timeout=15) as client: response = await client.get(url, headers=headers, follow_redirects=True) redirect_url = response.url @@ -78,7 +79,8 @@ async def get_unique_id(url: str, proxy_addr: str | None = None, headers: dict | headers = HEADERS_PC try: - async with httpx.AsyncClient(proxies=proxy_addr, timeout=15) as client: + proxy_addr = handle_proxy_addr(proxy_addr) + async with httpx.AsyncClient(proxy=proxy_addr, timeout=15) as client: # 第一次请求,获取重定向后的URL以提取sec_user_id response = await client.get(url, headers=headers, follow_redirects=True) 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 api = api + "&X-Bogus=" + xbogus try: - async with httpx.AsyncClient(proxies={"http://": proxy_addr, "https://": proxy_addr} if proxy_addr else None, + proxy_addr = handle_proxy_addr(proxy_addr) + async with httpx.AsyncClient(proxy=proxy_addr, timeout=15) as client: response = await client.get(api, headers=headers) response.raise_for_status() # 检查HTTP响应状态码是否表示成功 diff --git a/douyinliverecorder/spider.py b/douyinliverecorder/spider.py index f5575cc6..df766c35 100644 --- a/douyinliverecorder/spider.py +++ b/douyinliverecorder/spider.py @@ -4,7 +4,7 @@ Author: Hmily GitHub: https://github.com/ihmily Date: 2023-07-15 23:15:00 -Update: 2025-01-27 13:17:16 +Update: 2025-01-27 22:08:16 Copyright (c) 2023-2024 by Hmily, All Rights Reserved. Function: Get live stream data. """ @@ -26,7 +26,7 @@ import execjs import urllib.request from .utils import ( - trace_error_decorator, dict_to_cookie_str + trace_error_decorator, dict_to_cookie_str, handle_proxy_addr ) from .logger import script_path from .room import get_sec_user_id, get_unique_id @@ -57,10 +57,7 @@ async def async_req( if headers is None: headers = {} try: - if proxy_addr: - if not proxy_addr.startswith('http'): - proxy_addr = 'http://' + proxy_addr - + proxy_addr = handle_proxy_addr(proxy_addr) if data or json_data: async with httpx.AsyncClient(proxy=proxy_addr, timeout=timeout) as client: 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: abroad: bool = False) -> bool: try: + proxy_addr = handle_proxy_addr(proxy_addr) async with httpx.AsyncClient(proxy=proxy_addr, timeout=timeout) as client: response = await client.head(url, headers=headers, follow_redirects=True) return response.status_code == 200 @@ -1638,6 +1636,7 @@ async def login_popkontv( url = 'https://www.popkontv.com/api/proxy/member/v1/login' try: + proxy_addr = handle_proxy_addr(proxy_addr) async with httpx.AsyncClient(proxy=proxy_addr, timeout=20) as client: response = await client.post(url, json=data, headers=headers) response.raise_for_status() diff --git a/douyinliverecorder/utils.py b/douyinliverecorder/utils.py index e2f32098..da40b5c8 100644 --- a/douyinliverecorder/utils.py +++ b/douyinliverecorder/utils.py @@ -151,3 +151,12 @@ def check_disk_capacity(file_path: str | Path, show: bool = False) -> float: f"Used: {disk_usage.used / (1024 ** 3):.2f} GB " f"Free: {free_space_gb:.2f} GB\n") return free_space_gb + + +def handle_proxy_addr(proxy_addr): + if proxy_addr: + if not proxy_addr.startswith('http'): + proxy_addr = 'http://' + proxy_addr + else: + proxy_addr = None + return proxy_addr