Skip to content

Commit

Permalink
fix: update request proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ihmily committed Jan 27, 2025
1 parent a61bdc8 commit 71fbf93
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
13 changes: 8 additions & 5 deletions douyinliverecorder/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
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
import re
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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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响应状态码是否表示成功
Expand Down
11 changes: 5 additions & 6 deletions douyinliverecorder/spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions douyinliverecorder/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 71fbf93

Please sign in to comment.