普通TXT格式与XSPF播放列表格式间的转换 - Python

开始折腾VLC播放器+直播源的时候,找不到一款合适的转换工具,只能自己动手了..代码很远古,存在诸多缺陷,除了保证语法的正确性请忽略各种规范..

##-------------------------------------------------------------#
#   普通格式转XSPF by 黑鸟博客
##-------------------------------------------------------------#
def xspf():
    filepathname = tkinter.filedialog.askopenfilename(filetypes=[("TXT通用列表","*.txt"),("列表","*.dpl *.m3u"),("All Files","*")])     
    filepath = os.path.dirname(filepathname)       
    filenametmp = os.path.splitext(filepathname)[0]   
    filename = os.path.split(filenametmp)[1]   
    fileexttmp = os.path.splitext(filepathname)[1]
    filetmpbf = '-1'
    if fileexttmp == '.txt':
        fileone = open(filepathname, "r" ,encoding="UTF-8")   
        filetmpbf = fileone.read()
    elif fileexttmp == '':
        print("用户取消打开文件")
        return
    else:
        mBox.showinfo(title='异常',message='文件格式不支持!目前支持txt,xspf,m3u,dpl!')
        return
    #本文由 黑鸟博客 guihet.com 制作,转载请注明出处     
    timenow = datetime.datetime.now().strftime("%m%d%H%M")
    pathxspf = filepath + '/' + filename + '-' + timenow +".xspf"   
    filevlc = open(pathxspf, 'w', encoding = 'UTF-8')   
    filevlc.write('<?xml version="1.0" encoding="UTF-8"?>\n')
    filevlc.write('<playlist xmlns="http://xspf.org/ns/0/" xmlns:vlc="http://www.videolan.org/vlc/playlist/ns/0/" version="1">\n')
    filevlc.write('\t<title>Playlist</title>\n')
    filevlc.write('\t<trackList>\n')
    filetmp = filetmpbf  
    try:
        pa = re.compile(r'.*?,.*?$', re.M)                                                                         
        fileonelist = pa.findall(filetmp)               
        count = 0      
        for item in fileonelist:   
            #本文由 黑鸟博客 guihet.com 制作,转载请注明出处               
            zby = re.match(r'^(.*?),(.*?)$', item)    
            title = zby.group(1)       
            url = zby.group(2)     
            url = url.replace('&', r'&amp;')               
            filevlc.write('\t\t<track>\n')
            filevlc.write('\t\t\t<title>%s</title>\n' % title)
            filevlc.write('\t\t\t<location>%s</location>\n' % url)
            filevlc.write('\t\t\t<extension application="http://www.videolan.org/vlc/playlist/0">\n')
            filevlc.write('\t\t\t\t<vlc:id>%d</vlc:id>\n' % count)
            filevlc.write('\t\t\t</extension>\n\t\t</track>\n')
            count += 1
        filevlc.write('\t</trackList>\n')
        filevlc.write('\t<extension application="http://www.videolan.org/vlc/playlist/0">\n')
        for i in range(0, count):
            filevlc.write('\t\t\t<vlc:item tid="%d"/>\n' % i)        
        filevlc.write('\t</extension>\n')
        filevlc.write('</playlist>\n') 
        mBox.showinfo(title='完成',message='成功写入 XSPF 文件!')
    except Exception as e:
        mBox.showinfo(title='异常',message='写入失败!请检查源文件格式!')   
    if fileexttmp == '.txt':
        fileone.close()
    else:
        pass
    print('本列表由 黑鸟博客 guihet.com 制作')
    filevlc.close()

以下是逆向转

 
##-------------------------------------------------------------#
#   XSPF转普通格式的字符串  by 黑鸟博客
##-------------------------------------------------------------#
def xspftotxt(livepath = '-1'):   
    livestr = ''
    fileone = open(livepath, "r" ,encoding="UTF-8")
    filetmp = fileone.read()
    try:
        pa = re.compile(r'<track>.*?</track>', re.S)
        fileonelist = pa.findall(filetmp)
        count = 0
        for item in fileonelist:
            title = 'title'
            url = 'url'    
            #本文由 黑鸟博客 guihet.com 制作,转载请注明出处    
            pb = re.compile(r'<title>.*?</title>') 
            fileonelistb = pb.findall(item)
            for itemb in fileonelistb:
                zby = re.match(r'<title>(.*?)</title>', itemb)
                title = zby.group(1)        
            #本文由 黑鸟博客 guihet.com 制作,转载请注明出处   
            pc = re.compile(r'<location>.*?</location>') 
            fileonelistc = pc.findall(item)
            for itemc in fileonelistc:
                zby = re.match(r'<location>(.*?)</location>', itemc)
                url = zby.group(1)   
                
            livestr = livestr + title + ',' + url + '\n' 
    except Exception as e: 
        print('提取XSPF文件到字符串,错误!!')
        eturn '-1'  
    print('本列表由 黑鸟博客 guihet.com 制作')  
    return livestr
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注