Skip to content

Commit

Permalink
Switch to httpx for HTTP requests
Browse files Browse the repository at this point in the history
Replaced `requests` with `httpx` in `dylr.py` for HTTP requests. This update aims to improve performance and handle asynchronous operations more efficiently. Updated `Pipfile.lock` to reflect dependency changes and ensure compatibility.

Signed-off-by: hldh214 <[email protected]>
  • Loading branch information
hldh214 committed Sep 11, 2024
1 parent 4d877ef commit 65b95cd
Show file tree
Hide file tree
Showing 6 changed files with 647 additions and 538 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ verify_ssl = true
name = "pypi"

[packages]
requests = "==2.*"
google-api-python-client = "==2.*"
google-auth-oauthlib = "==0.*"
toml = "==0.10.*"
Expand All @@ -30,6 +29,7 @@ tensorflow = "==2.14.*"
sentry-sdk = "==2.*"
jsengine = "==1.0.*"
quickjs = "==1.*"
httpx = "==0.*"

[dev-packages]
pytest = "==7.*"
Expand Down
1,153 changes: 631 additions & 522 deletions Pipfile.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions recorder/danmaku/douyin/dylr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import jsengine
import pymongo.errors
import requests
import httpx
import websockets
from google.protobuf import json_format

Expand Down Expand Up @@ -78,7 +78,7 @@ def auto_get_cookie():
"User-Agent": UA,
"cookie": cookie
}
resp = requests.get(url, headers=header)
resp = httpx.get(url, headers=header)
ttwid = None
for c in resp.cookies:
if c.name == 'ttwid':
Expand Down
8 changes: 4 additions & 4 deletions recorder/source/douyin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

import requests
import httpx

from recorder import logger

Expand All @@ -9,7 +9,7 @@

def get_room_info(room_id):
try:
res = requests.get(
res = httpx.get(
# Credit: https://github.com/LyzenX/DouyinLiveRecorder/commit/c83a00c8c2d4f4aad25347f3163cd9ced212c99d
f'https://live.douyin.com/webcast/room/web/enter/?aid=6383&device_platform=web&browser_language=zh-CN'
f'&browser_platform=Win32&browser_name=Chrome&browser_version=109.0.0.0&web_rid={room_id}',
Expand All @@ -24,12 +24,12 @@ def get_room_info(room_id):
},
timeout=REQUEST_TIMEOUT
)
except requests.exceptions.RequestException:
except httpx.HTTPError:
return {}

try:
res = res.json()
except requests.exceptions.JSONDecodeError:
except ValueError:
logger.warning(f'Failed to get room info({room_id}), res: {res.text}')
return {}

Expand Down
12 changes: 6 additions & 6 deletions recorder/source/huya.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re
import subprocess

import requests
import httpx
import websockets
import websockets.exceptions

Expand All @@ -28,7 +28,7 @@

def parse_by_mini_program(sub_sid, preferred_cdn_type, preferred_format, ratio, min_start_time):
try:
res = requests.get('https://mp.huya.com/cache.php', params={
res = httpx.get('https://mp.huya.com/cache.php', params={
'm': 'Live',
'do': 'profileRoom',
'pid': sub_sid,
Expand All @@ -40,7 +40,7 @@ def parse_by_mini_program(sub_sid, preferred_cdn_type, preferred_format, ratio,
'Referer': 'https://servicewechat.com/wx74767bf0b684f7d3/244/page-frame.html', # 244?
'content-type': 'application/json'
}, timeout=REQUEST_TIMEOUT)
except requests.exceptions.RequestException:
except httpx.HTTPError:
return False

logger.debug(f'parse_by_mini_program({sub_sid}): {res.text}')
Expand Down Expand Up @@ -101,11 +101,11 @@ def get_living_info_response(binary_array):

def get_stream(room_id, **kwargs):
try:
res = requests.get('https://m.huya.com/{0}'.format(room_id), headers={
res = httpx.get('https://m.huya.com/{0}'.format(room_id), headers={
'User-Agent': 'Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) '
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Mobile Safari/537.36'
}, timeout=REQUEST_TIMEOUT)
except requests.exceptions.RequestException:
except httpx.HTTPError:
return False

sub_sid_result = sub_sid_pattern.findall(res.text)
Expand Down Expand Up @@ -173,7 +173,7 @@ def parse_stream_info(stream_info, preferred_cdn_type, preferred_format, ratio,


def get_replay(video_id):
res = requests.get('https://liveapi.huya.com/moment/getMomentContent', params={'videoId': video_id}).json()
res = httpx.get('https://liveapi.huya.com/moment/getMomentContent', params={'videoId': video_id}).json()

definitions = res['data']['moment']['videoInfo']['definitions']

Expand Down
6 changes: 3 additions & 3 deletions recorder/source/panda.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random

import requests
import httpx

from recorder.ffmpeg import USER_AGENTS
from recorder import logger
Expand All @@ -19,7 +19,7 @@ def get_stream(room_id, **kwargs):
}

try:
res = requests.post('https://api.pandalive.co.kr/v1/live/play', data={
res = httpx.post('https://api.pandalive.co.kr/v1/live/play', data={
'action': 'watch',
'userId': room_id,
}, headers={
Expand All @@ -29,7 +29,7 @@ def get_stream(room_id, **kwargs):
'user-agent': random.choice(USER_AGENTS),
'x-device-info': '{"t":"webPc","v":"1.0","ui":17784756}'
}, proxies=proxies).json()
except requests.exceptions.RequestException:
except (httpx.HTTPError, ValueError):
return False

logger.debug(f'panda.get_stream: {res}')
Expand Down

0 comments on commit 65b95cd

Please sign in to comment.