Skip to content

Commit

Permalink
fix: get_real_url
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo2011 committed Jan 14, 2024
1 parent f5a5d2a commit e1b379c
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions bilibili_api/utils/short.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
"""
from typing import Optional

import httpx

from .. import settings
from .credential import Credential
from .network import get_session
from .network import get_session, get_aiohttp_session


async def get_real_url(short_url: str, credential: Optional[Credential] = None) -> str:
Expand All @@ -27,29 +25,14 @@ async def get_real_url(short_url: str, credential: Optional[Credential] = None)
返回值为原 url 类型
"""
credential = credential if credential else Credential()
config = {}
config["method"] = "GET"
config["url"] = short_url
config["follow_redirects"] = False
if settings.proxy:
config["proxies"] = {"all://": settings.proxy}

try:
resp = await get_session().head(url=str(short_url), follow_redirects=True)
if settings.http_client == settings.HTTPClient.HTTPX:
resp = await get_session().head(url=str(short_url), follow_redirects=True)
else:
resp = await get_aiohttp_session().head(url=str(short_url), allow_redirects=True)
u = resp.url

return str(u)
except Exception as e:
raise e


async def get_headers(short_url: str) -> httpx.Headers:
"""
获取链接的 headers。
"""
config = {}
config["method"] = "GET"
config["url"] = short_url
config["follow_redirects"] = False
if settings.proxy:
config["proxies"] = {"all://": settings.proxy}
resp = await get_session().head(url=short_url, follow_redirects=False)
return resp.headers

0 comments on commit e1b379c

Please sign in to comment.