Quick actions

cmd+k|ctrl+k

Navigation

Languages

Bilibili_iframe_convert

Snippet info

Language

Python

Visibility

public

Author

nina

Created

2025-02-12T04:40:28.292245Z

Updated

2025-02-12T04:42:23.625858Z

import re
import sys

def convert_iframe_code(input_code):
   # print(f"Received iframe code: {input_code}")  # 打印接收到的 iframe 代码,用于调试
    
    # 更新正则表达式,允许匹配多种 iframe 格式
    pattern = r'<iframe.*?src=["\']\/\/player\.bilibili\.com\/player\.html\?isOutside=true&aid=(\d+)&bvid=(\w+)&cid=(\d+)&p=(\d+).*?["\'].*?>.*?<\/iframe>'
    match = re.search(pattern, input_code)

    if match:
        aid = match.group(1)
        bvid = match.group(2)
        cid = match.group(3)
        p = match.group(4)

        # 构造新的 iframe 代码
        new_code = f'<iframe src="http://player.bilibili.com/player.html?isOutside=true&aid={aid}&bvid={bvid}&cid={cid}&p={p}&autoplay=false" ' \
                   f'scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" ' \
                   f'style="width: 640px; height: 430px; max-width: 100%"></iframe>'
        return new_code
    else:
        raise ValueError("Invalid iframe code")

# 从命令行参数中获取输入
if __name__ == '__main__':
    if len(sys.argv) > 1:
        input_code = sys.argv[1]
        try:
            output_code = convert_iframe_code(input_code)
            print(output_code)
        except ValueError as e:
            print(e)
    else:
        print("Please provide the iframe code as an argument.")
INFO