Skip to main content

A package that transform bilibili's dynamic data to image

Project description

DynamicRender

用于将B站的动态渲染为图片(需要配合适配器将动态转化为特定格式)

使用

安装

pip install dynamicrender

1. 格式化动态

# 如果数据是grpc返回的数据
from google.protobuf.json_format import MessageToDict
from dynamicadaptor.DynamicConversion import formate_message
from bilirpc.api import get_dy_detail
import asyncio
from dynamicrender.Core import DyRender 

async def sample1():
    dynamic_grpc = await get_dy_detail("746530608345251842")
    dynamic: dict = MessageToDict(dynamic_grpc[0])
    dynamic_formate = formate_message("grpc", dynamic)
    render = DyRender()
    # DyRender 实例化时可以传两个参数
    #1. data_path: {str} 为静态文件所在的位置,当data_path内不存在Static目录,则会自动解压自带的Static到data_path内
    # 2.font_path: {str/dic}当font_path为str类型的时候必须为字体的路径,路径不存在的话会使用默认的字体
    # 不建议使用传dict的方法修改字体
    # 当font_path为dict的时候必须为如以下所示的dict:
    """
    {
     text: str
     extra_text: str
     emoji: str
    }
    三个都必须是字体的路径,text为文本主体的字体路径,extra_text为备选字体的路径,emoji为emoji字体的路径,
    
    注意!!!
    
    每款emoji字体的字体大小都有固定值,如果修改了emoji字体必须同时修改emoji字体的字体大小
    
    """
    result = await render.dyn_render(dynamic_formate)
    result.show()
    
asyncio.run(sample1())



# 如果是web返回的数据

import asyncio
import httpx
from dynamicrender.Core import DyRender
from dynamicadaptor.DynamicConversion import formate_message 

async def sample2():
    url = "https://api.bilibili.com/x/polymer/web-dynamic/v1/detail?timezone_offset=-480&id=746530608345251842"
    headers = {
        "Referer": "https://t.bilibili.com/746530608345251842"
    }
    result = httpx.get(url, headers=headers).json()
    dynamic_formate = formate_message("web", result["data"]["item"])
    render = DyRender()
    result = await render.dyn_render(dynamic_formate)
    result.show()


# asyncio.run(sample2())

示例

示例图片

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minidynamicrender-1.2.6.tar.gz (26.6 kB view hashes)

Uploaded Source

Built Distribution

minidynamicrender-1.2.6-py3-none-any.whl (29.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page