-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add get_short_url to generate the b23.tv url from the bilibili.com url #720
Open
TimG233
wants to merge
5
commits into
Nemo2011:dev
Choose a base branch
from
TimG233:b23_short_url
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
acace35
refactor: remove unused & unneeded credential for get_real_url
TimG233 314dcb7
feat: add get_short_url method to generate the b23.tv url from the bi…
TimG233 995d7b2
feat: add docs related to get_short_url method and remove credential …
TimG233 8a4d575
refactor: add get_spi_buvid method for acquiring buvid3 and change pa…
TimG233 7d215a9
chore: 添加参数
z0z0r4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,24 +7,109 @@ | |
|
||
from .. import settings | ||
from .credential import Credential | ||
from .network import get_session, get_aiohttp_session | ||
from .network import get_session, get_aiohttp_session, HEADERS | ||
from ..exceptions import ApiException, ArgsException | ||
|
||
import re | ||
import random | ||
import string | ||
|
||
async def get_real_url(short_url: str, credential: Optional[Credential] = None) -> str: | ||
|
||
def acquire_buvid(credential: Optional[Credential] = None) -> str: | ||
""" | ||
获取短链接跳转目标,以进行操作。 | ||
从Credential中取出buvid3,若不存在则生成一个随机的buvid3。 | ||
|
||
Args: | ||
short_url(str): 短链接。 | ||
credential(Credential \| None): 凭据类。 | ||
|
||
Returns: | ||
buvid3的字符串 | ||
""" | ||
|
||
# return given buvid3 if possible | ||
if credential: | ||
buvid3 = credential.get_cookies()['buvid3'] | ||
|
||
if buvid3 is not None: | ||
return buvid3 | ||
|
||
# random generation | ||
buvid3_pattern = '8-4-4-4-17' # current buvid3 char-length pattern, might be changed later | ||
parts = buvid3_pattern.split('-') | ||
|
||
buvid3_rand_gen = ["".join(random.choices(string.digits + string.ascii_letters, k=int(part))) for part in parts] | ||
|
||
return "-".join(buvid3_rand_gen) + "infoc" | ||
|
||
|
||
async def get_short_url(real_url: str, credential: Optional[Credential] = None) -> str: | ||
""" | ||
获取目标链接的短链接。支持bilibili.com的相关链接。 | ||
|
||
Args: | ||
real_url(str): 目标链接。 | ||
|
||
credential(Credential \| None): 凭据类。 | ||
|
||
Returns: | ||
目标链接的b23.tv短链接。 | ||
|
||
尽管目标链接可能不存在,但仍然会生成短链接。 | ||
|
||
相同的目标链接重复调用此方法会获得不同的短链接。 | ||
""" | ||
|
||
# validate the starting part of url | ||
url_start_pattern = re.compile(pattern=r'^https?:\/\/(?:www\.)?bilibili\.com') | ||
|
||
if not re.match(pattern=url_start_pattern, string=real_url): | ||
raise ArgsException(msg=f"提供的real_url {real_url} 不符合格式。" | ||
f"支持的格式为bilibili.com的相关链接并含有http或https协议。") | ||
|
||
# POST request | ||
try: | ||
post_data = { | ||
'build': str(random.randint(6000000, 10000000)), | ||
'buvid': acquire_buvid(credential=credential), | ||
'oid': real_url, | ||
'platform': random.choice(['android', 'ios']), | ||
'share_channel': 'COPY', | ||
'share_id': 'public.webview.0.0.pv', | ||
'share_mode': str(random.randint(1, 10)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不了解的参数可以照旧,build 也是 |
||
} | ||
|
||
api_url = 'https://api.biliapi.net/x/share/click' | ||
|
||
if settings.http_client == settings.HTTPClient.HTTPX: | ||
resp_content = (await get_session().post(url=api_url, headers=HEADERS, data=post_data)).json() | ||
else: | ||
resp = await get_aiohttp_session().post( | ||
url=api_url, data=post_data, headers=HEADERS | ||
) | ||
resp_content = await resp.json() | ||
|
||
# the 'content' sometimes will not be in the returned content due to build version, real_url, or buvid (rarely) | ||
if 'content' not in resp_content['data']: | ||
raise ApiException(msg="生成短链接失败。若提供的目标链接格式确认正确,请反馈此bug以更新相应params") | ||
|
||
return resp_content['data']['content'] | ||
|
||
except Exception as e: | ||
raise e | ||
|
||
|
||
async def get_real_url(short_url: str) -> str: | ||
""" | ||
获取短链接跳转目标,以进行操作。 | ||
|
||
Args: | ||
short_url(str): 短链接。 | ||
|
||
Returns: | ||
目标链接(如果不是有效的链接会报错) | ||
|
||
返回值为原 url 类型 | ||
""" | ||
credential = credential if credential else Credential() | ||
|
||
try: | ||
if settings.http_client == settings.HTTPClient.HTTPX: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不建议随机,有spi接口可以用