Skip to content

Instantly share code, notes, and snippets.

@koukuko
Created February 25, 2021 12:43
Show Gist options
  • Save koukuko/33977742e4aec3bdc8e55c75a95d9f78 to your computer and use it in GitHub Desktop.
Save koukuko/33977742e4aec3bdc8e55c75a95d9f78 to your computer and use it in GitHub Desktop.
import obspython as obs
import urllib.request
import urllib.error
import json
data_source = 0
ref_id = 0
interval = 5
source_name = ""
# ------------------------------------------------------------
def update_text():
global data_source
global ref_id
global interval
global source_name
source = obs.obs_get_source_by_name(source_name)
text = ''
if source is not None:
try:
if data_source > 10000 and data_source < 20000:
url = "https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id={}".format(ref_id)
elif data_source > 20000 and data_source < 30000:
url = "https://api.bilibili.com/x/space/acc/info?mid={}&jsonp=jsonp".format(ref_id)
elif data_source > 30000 and data_source < 40000:
url = "https://api.bilibili.com/x/relation/stat?vmid={}&jsonp=jsonp".format(ref_id)
elif data_source > 40000 and data_source < 50000:
headers = {'referer': 'https://space.bilibili.com/'}
url = urllib.request.Request("https://api.bilibili.com/x/space/upstat?mid={}&jsonp=jsonp".format(ref_id), None, headers)
with urllib.request.urlopen(url) as response:
content = json.loads(response.read().decode('utf8'))
if content['code'] == 0:
if data_source == 10001:
text = content['data']['room_info']['title']
elif data_source == 10002:
text = content['data']['room_info']['area_name']
elif data_source == 10003:
text = content['data']['room_info']['online']
elif data_source == 10004:
text = content['data']['guard_info']['count']
elif data_source == 10005:
text = content['data']['anchor_info']['live_info']['level']
elif data_source == 10006:
text = content['data']['anchor_info']['live_info']['score']
elif data_source == 10007:
text = content['data']['anchor_info']['live_info']['rank']
elif data_source == 10008:
text = content['data']['anchor_info']['live_info']['upgrade_score']
elif data_source == 10009:
text = content['data']['battle_rank_entry_info']['rank_name']
elif data_source == 10010:
text = content['data']['hot_rank_info']['area_name']
elif data_source == 10011:
text = content['data']['hot_rank_info']['rank']
elif data_source == 20001:
text = content['data']['name']
elif data_source == 20002:
text = content['data']['coins']
elif data_source == 30001:
text = content['data']['following']
elif data_source == 30002:
text = content['data']['follower']
elif data_source == 40001:
text = content['data']['archive']['view']
elif data_source == 40002:
text = content['data']['article']['view']
elif data_source == 40003:
text = content['data']['likes']
else:
text = 'ERROR'
apply_text(text, source)
except urllib.error.URLError as err:
obs.script_log(obs.LOG_WARNING, "Error opening URL: " + err.reason)
obs.remove_current_callback()
obs.obs_source_release(source)
def apply_text(text, source):
settings = obs.obs_data_create()
obs.obs_data_set_string(settings, "text", str(text))
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
def refresh_pressed(props, prop):
update_text()
# ------------------------------------------------------------
def script_description():
return "定时将一个文本源的文本更换为指定文本\n\nBy TokimoriSeisa"
def script_update(settings):
global data_source
global ref_id
global interval
global source_name
data_source = obs.obs_data_get_int(settings, "data_source")
ref_id = obs.obs_data_get_int(settings, "ref_id")
interval = obs.obs_data_get_int(settings, "interval")
source_name = obs.obs_data_get_string(settings, "source")
obs.timer_remove(update_text)
if source_name != "" and data_source > 0:
obs.timer_add(update_text, interval * 1000)
def script_defaults(settings):
obs.obs_data_set_default_int(settings, "data_source", 0)
obs.obs_data_set_default_int(settings, "interval", 30)
def script_properties():
props = obs.obs_properties_create()
# data_source
ds = obs.obs_properties_add_list(props, "data_source", "数据源", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_INT )
obs.obs_property_list_add_int(ds, "<请选择数据源>", 0)
obs.obs_property_list_add_int(ds, "直播(ID填写直播间ID)", 0)
obs.obs_property_list_add_int(ds, "┠ 房间标题", 10001)
obs.obs_property_list_add_int(ds, "┠ 直播分区", 10002)
obs.obs_property_list_add_int(ds, "┠ 直播人气", 10003)
obs.obs_property_list_add_int(ds, "┠ 舰队数", 10004)
obs.obs_property_list_add_int(ds, "┠ 直播主等级", 10005)
obs.obs_property_list_add_int(ds, "┠ 直播主积分", 10006)
obs.obs_property_list_add_int(ds, "┠ 直播主等级排名", 10007)
obs.obs_property_list_add_int(ds, "┠ 直播主下一等级还需积分", 10008)
obs.obs_property_list_add_int(ds, "┠ 大乱斗段位", 10009)
obs.obs_property_list_add_int(ds, "┠ 限时热门榜所在分区", 10010)
obs.obs_property_list_add_int(ds, "┠ 限时热门榜排名", 10011)
obs.obs_property_list_add_int(ds, "空间(ID填写用户ID)", 0)
obs.obs_property_list_add_int(ds, "┠ 昵称", 20001)
obs.obs_property_list_add_int(ds, "┠ 硬币数", 20002)
obs.obs_property_list_add_int(ds, "┠ 关注数", 30001)
obs.obs_property_list_add_int(ds, "┠ 粉丝数", 30002)
#obs.obs_property_list_add_int(ds, "┠ 播放数", 40001)
#obs.obs_property_list_add_int(ds, "┠ 阅读数", 40002)
#obs.obs_property_list_add_int(ds, "┠ 点赞数", 40003)
# id
obs.obs_properties_add_int(props, "ref_id", "ID", 1, 2147483647, 1)
# interval
obs.obs_properties_add_int(props, "interval", "更新频率 (秒)", 5, 3600, 1)
# source
p = obs.obs_properties_add_list(props, "source", "文本源", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
sources = obs.obs_enum_sources()
if sources is not None:
for source in sources:
source_id = obs.obs_source_get_unversioned_id(source)
if source_id == "text_gdiplus" or source_id == "text_ft2_source":
name = obs.obs_source_get_name(source)
obs.obs_property_list_add_string(p, name, name)
obs.source_list_release(sources)
# button
obs.obs_properties_add_button(props, "button", "立即刷新", refresh_pressed)
return props
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment