mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-05-10 17:43:40 +08:00
33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
import re
|
|
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
def replace_content_markers(markdown: str, video_id: str, platform: str = 'bilibili') -> str:
|
|
"""
|
|
替换 *Content-04:16*、Content-04:16 或 Content-[04:16] 为超链接,跳转到对应平台视频的时间位置
|
|
"""
|
|
# 匹配三种形式:*Content-04:16*、Content-04:16、Content-[04:16]
|
|
pattern = r"(?:\*?)Content-(?:\[(\d{2}):(\d{2})\]|(\d{2}):(\d{2}))"
|
|
|
|
def replacer(match):
|
|
mm = match.group(1) or match.group(3)
|
|
ss = match.group(2) or match.group(4)
|
|
total_seconds = int(mm) * 60 + int(ss)
|
|
|
|
if platform == 'bilibili':
|
|
url = f"https://www.bilibili.com/video/{video_id}?t={total_seconds}"
|
|
elif platform == 'youtube':
|
|
url = f"https://www.youtube.com/watch?v={video_id}&t={total_seconds}s"
|
|
elif platform == 'douyin':
|
|
url = f"https://www.douyin.com/video/{video_id}"
|
|
return f"[原片 @ {mm}:{ss}]({url})"
|
|
else:
|
|
return f"({mm}:{ss})"
|
|
|
|
return f"[原片 @ {mm}:{ss}]({url})"
|
|
|
|
return re.sub(pattern, replacer, markdown)
|