Created
September 10, 2016 01:54
-
-
Save dixyes/7be49ca8ae288f453469c1a309f8d3ed to your computer and use it in GitHub Desktop.
a dplayer json - bilibili xml converter
This file contains 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
#!/bin/env python | |
#bili xml <==> Dplayer json converter | |
import sys,json | |
# struct and vars | |
xmlhead0="<i><chatserver>" | |
xmlhead1="</chatserver><chatid>" | |
xmlhead2="</chatid><mission>0</mission><maxlimit>" | |
xmlhead3="</maxlimit><source></source><ds></ds><de></de><max_count>" | |
xmlhead4="</max_count><count>" | |
xmlhead5="</count>" | |
taghead="<d p=\"" | |
tagtail="</d>" | |
xmltail="</i>" | |
btypemask={ | |
'right':"1", | |
'bottom':"4", | |
'top':"5", | |
'1':"right", | |
'4':"bottom", | |
'5':"top" | |
} | |
def covj(xmlfp,jsonfp,**opt): | |
data="" | |
try: | |
data=xmlfp.read() | |
data=data.replace("</i>","") | |
data=data.split('<d p=\"') | |
id=opt.get("id",data[0].split("<chatid>")[1].split("</chatid>")[0]) | |
data=data[1:len(data)] | |
dmp=[] | |
for danmaku in data : | |
danmaku=danmaku.replace("\">",",").replace("</d>","").split(",") | |
danmakujson={ | |
"time":float(danmaku[0]), | |
"text":danmaku[8], | |
"author":"DIYGod", | |
"type":btypemask.get(danmaku[1],"right"), | |
"color":("#"+hex(int(danmaku[3])+0xff000000)[4:])[:7], | |
"player":[id], | |
"post_time":danmaku[4] | |
} | |
dmp.append(danmakujson) | |
print("done reading danmaku pool :"+str(len(dmp))+" added") | |
del(data) | |
jsonfp.write(json.dumps({'code':1,'danmaku':dmp})) | |
return True | |
except: | |
print("Error:"+str(sys.exc_info()[1])) | |
return False | |
def covx(jsonfp,xmlfp,**opt): | |
try: | |
#print(sds) | |
dmp=json.loads(jsonfp.read(), encoding="utf-8").get("danmaku",[]) | |
id=opt.get("id","") | |
xmlfp.write(xmlhead0+opt.get("chatserver","domain")+xmlhead1+id+xmlhead2+"1000"+xmlhead3+"1000"+xmlhead4+str(len(dmp))+xmlhead5) | |
for danmaku in dmp: | |
time=str(danmaku.get("time",0)) | |
type=btypemask.get(danmaku.get("type","right"),"1") | |
size="25" | |
if len(danmaku.get("color","#ffffff")) == 7: | |
color=str(int(danmaku.get("color","#ffffff")[1:7],16)) | |
if len(danmaku.get("color","#ffffff")) == 4: | |
color=str(int(danmaku.get("color")[1:2]+danmaku.get("color")[1:2]+danmaku.get("color")[2:3]+danmaku.get("color")[2:3]+danmaku.get("color")[3:4]+danmaku.get("color")[3:4],16)) | |
else: | |
color="16777215" | |
post_time=str(danmaku.get("post_time",0)) | |
comment=danmaku.get("text","").encode("utf-8") | |
tag=taghead+time+","+type+","+size+","+color+","+post_time+","+"0,0,0\">"+comment+tagtail | |
xmlfp.write(tag) | |
xmlfp.write(xmltail) | |
print("done saving danmaku pool") | |
except: | |
print("fail saving danmaku pool: "+str(sys.exc_info()[1])) | |
def main(): | |
option="normal" | |
jindex=-1 | |
xindex=-1 | |
if len(sys.argv) > 1 : | |
args=sys.argv[1:] | |
for i in range(0,len(args)): | |
if args[i].endswith(".json"): | |
jpath=args[i] | |
jindex=i | |
continue | |
if args[i].endswith(".xml"): | |
xpath=args[i] | |
xindex=i | |
continue | |
else : | |
option+=","+args[i] | |
if jindex==-1 and xindex!=-1 : | |
try: | |
covj(open(xpath),open(xpath.split(".")[0]+".json","w"),id=xpath.split(".")[0]) | |
except: | |
print("Error:"+str(sys.exc_info()[1])) | |
pass | |
return | |
if jindex!=-1 and xindex==-1: | |
try: | |
covx(open(jpath),open(jpath.split(".")[0]+".xml","w"),id=jpath.split(".")[0]) | |
except: | |
print("Error:"+str(sys.exc_info()[1])) | |
pass | |
return | |
if jindex>xindex : | |
try: | |
covj(open(xpath),open(jpath,"w"),id=jpath.split(".")[0]) | |
except: | |
print("Error:"+str(sys.exc_info()[1])) | |
pass | |
return | |
else: | |
try: | |
covx(open(jpath),open(xpath,"w"),id=xpath.split(".")[0]) | |
except: | |
print("Error:"+str(sys.exc_info()[1])) | |
pass | |
return | |
if jindex==-1 and xindex==-1: | |
print("not enough args") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment