Skip to content

Instantly share code, notes, and snippets.

@boboidream
Created August 16, 2018 17:01
Show Gist options
  • Save boboidream/44711883092b6d76169606c3c2118710 to your computer and use it in GitHub Desktop.
Save boboidream/44711883092b6d76169606c3c2118710 to your computer and use it in GitHub Desktop.
tools
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# tv.py
#
# Copyright 2017 Nocmt <[email protected]>
import re
import datetime
import argparse
# get argument
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.0.1')
parser.add_argument('-i', '--input', help="input filename")
parser.add_argument('-t', '--title', help="list title")
params = parser.parse_args()
i_file = params.input or 'test.txt'
file_title = params.title or '播放列表'
def tvshow():
print ('---欢迎使用电视直播源列表生成器---')
print ('---请确保zb.txt文件存在且格式正确!---\n')
print ('---开始读取!---')
now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
FileHandle = open(i_file, "r")
TvFile = open(now + ".xspf", "w")
FileList = FileHandle.readlines()
regex = re.compile(r"(.*?),(.*)", re.IGNORECASE)
L = 0
TvFile.write('<?xml version="1.0" encoding="UTF-8"?>\n<playlist xmlns="http://xspf.org/ns/0/" xmlns:vlc="http://www.videolan.org/vlc/playlist/ns/0/" version="1">\n\t<title>%s</title>\n\t<trackList>\n' % (file_title))
for File in FileList:
if not re.match(regex, File):
continue
File = File.replace('\n', '')
position = File.index(',')
title = File[:position]
url = File[position+1:]
url = url.replace('&', r'&')
print (str(L) + '.读写成功!标题:' + title + ', 地址:' + url)
track = '\t<track>\n\t\t<location>{0}</location>\n\t\t<title>{1}</title>\n\t\t<extension application="http://www.videolan.org/vlc/playlist/0">\n\t\t\t<vlc:id>{2}</vlc:id>\n\t\t\t<vlc:option>network-caching=1000</vlc:option>\n\t\t</extension>\n\t</track>\n'.format(url, title, L)
TvFile.write(track + '\n')
L += 1
TvFile.write('\t</trackList>\n\t<extension application="http://www.videolan.org/vlc/playlist/0">\n')
for i in range(0, L):
vlc = '\t\t\t<vlc:item tid="{0}"/>\n'.format(i)
TvFile.write(vlc)
TvFile.write('\t</extension>\n</playlist>')
FileHandle.close()
TvFile.close()
print('写入完成!请自行查看!')
if __name__ == '__main__':
tvshow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment