Last active
July 30, 2023 07:17
-
-
Save taozhiyu/425c7ef19e4f41be70afc804b558ead3 to your computer and use it in GitHub Desktop.
download 猫耳FM subtitles
This file contains hidden or 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
# !/usr/bin/python3 | |
# -*- coding: UTF-8 -*- | |
# 成品&说明: | |
# https://www.52pojie.cn/thread-1688300-1-1.html | |
import xml.etree.ElementTree as ETree | |
import requests | |
import re | |
def getSoundName(sound_id): | |
url = f'https://www.missevan.com/sound/getsound?soundid={sound_id}' | |
r = requests.get(url) | |
r.encoding = 'utf-8' | |
regex = r"[/\\:*?\"<>|]" | |
return re.sub(regex, "", r.json()['info']['sound']['soundstr'], 0) | |
def parse_XML_from_URL(sound_id): | |
dmURL = f'https://www.missevan.com/sound/getdm?soundid={sound_id}' | |
dmSTR = requests.get(dmURL).text | |
dmXML = ETree.fromstring(dmSTR) | |
dm_list = {} | |
for items in dmXML.findall("d"): | |
_stime, _mode, _size, _color, _date, _class, _uid, _dmid = items.attrib["p"].split(",") | |
_text = items.text | |
if _mode == "4": | |
if _dmid in dm_list: | |
raise "不是吧,弹幕的id竟然重复了???" | |
dm_list[_dmid] = {"stime": _stime, "text": _text} | |
dm_list = dict(sorted(dm_list.items(), key=lambda x: (float(x[1]["stime"]), float(x[0])))) | |
return dm_list | |
def add0(x): | |
x = str(x) | |
return "0" + x if len(x) == 1 else x | |
def genLRC(dm_list, ti): | |
lrc = f"""[ver:v1.0] | |
[nickname:涛之雨] | |
[ti:{ti}]""" | |
last_time = "" | |
for date, data in dm_list.items(): | |
if last_time == data["stime"]: | |
lrc += " " + data["text"] | |
continue | |
last_time = data["stime"] | |
s, ms = data["stime"].split(".") | |
s = int(s) | |
lrc += f'\n[{add0(s // 60)}:{add0(s % 60)}.{ms}]{data["text"]}' | |
return lrc | |
def main(): | |
soundId = input("请输入声音ID:") | |
soundName = getSoundName(soundId) | |
dmList = parse_XML_from_URL(soundId) | |
dmLRC = genLRC(dmList, soundName) | |
with open(soundName + ".lrc", "w+", encoding="utf-8") as f: | |
f.seek(0) | |
f.write(dmLRC) | |
print(f"{soundName}[@{soundId}]下载完成") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
后续版本懒得开源了,请直接查看: