Skip to content

Commit b7fa9bb

Browse files
committed
refactor: replace aiohttp with httpx
1 parent 9e387bc commit b7fa9bb

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

app/core/direct_downloader.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import time
44
from typing import Optional
55

6-
import aiohttp
6+
import httpx
77

88
from ..utils.logger import logger
99

@@ -22,7 +22,7 @@ def __init__(self,
2222
self.record_url = record_url
2323
self.save_path = save_path
2424
self.headers = headers or {}
25-
self.proxy = proxy
25+
self.proxy = proxy or None
2626
self.chunk_size = chunk_size
2727
self.stop_event = asyncio.Event()
2828
self.process = None
@@ -50,22 +50,15 @@ async def _download_stream(self) -> None:
5050
try:
5151
os.makedirs(os.path.dirname(self.save_path), exist_ok=True)
5252

53-
async with aiohttp.ClientSession() as session:
54-
proxy_settings = {}
55-
if self.proxy:
56-
proxy_settings['proxy'] = self.proxy
57-
58-
async with session.get(self.record_url, headers=self.headers,
59-
timeout=aiohttp.ClientTimeout(total=None),
60-
**proxy_settings) as response:
61-
if response.status != 200:
62-
logger.error(f"Request Stream Failed, Status Code: {response.status}")
53+
async with httpx.AsyncClient(headers=self.headers, proxy=self.proxy, timeout=None) as client:
54+
async with client.stream("GET", self.record_url) as response:
55+
if response.status_code != 200:
56+
logger.error(f"Request Stream Failed, Status Code: {response.status_code}")
6357
return
6458

6559
with open(self.save_path, 'wb') as f:
66-
while not self.stop_event.is_set():
67-
chunk = await response.content.read(self.chunk_size)
68-
if not chunk:
60+
async for chunk in response.aiter_bytes(self.chunk_size):
61+
if self.stop_event.is_set():
6962
break
7063

7164
f.write(chunk)

0 commit comments

Comments
 (0)