Closed Chenshennan closed 11 months ago
#!/usr/bin/env python3
import re
import pytz
import requests
import argparse
from lxml import html
from datetime import datetime, timezone, timedelta
tz = pytz.timezone('Asia/Shanghai')
parser = argparse.ArgumentParser()
parser.description='This is a python script to generate Chinese IPTV EPG file in xml format'
parser.add_argument("-f", "--file", help="default=guide.xml", dest="path", default="guide.xml")
args = parser.parse_args()
cctv_channel = ['cctv1','cctv2','cctv3','cctv4','cctv5','cctv5plus','cctv6', \
'cctv7','cctv8','cctvjilu','cctv10','cctv11','cctv12','cctv13','cctvchild', \
'cctv15','cctv16','cctv17']
sat_channel = ['anhui','bingtuan','btv1','btvchild','btvjishi','cetv1','cetv2', \
'cetv3','cetv4','chongqing','dongfang','dongnan','gansu','guangdong', \
'guangxi','guizhou','hebei','heilongjiang','henan','hubei','hunan', \
'jiangsu','jiangxi','jilin','liaoning','neimenggu','ningxia','qinghai', \
'sdetv','shandong','shan1xi','shan3xi','shenzhen','sichuan','tianjin', \
'travel','xiamen','xinjiang','xizang','yanbian','yunnan','zhejiang']
def getChannelName(fhandle, channelID):
#change channelID list to str cids
cids = ''
for x in channelID:
cids = cids + x + ','
session = requests.Session()
api = "http://api.cntv.cn/epg/epginfo?c=%s" % (cids)
epgdata = session.get(api).json()
for n in range(len(channelID)):
program = epgdata[channelID[n]]['program']
#write channel id info
fhandle.write(' <channel id="%s">\n' % channelID[n])
fhandle.write(' <display-name lang="cn">%s</display-name>\n' % epgdata[channelID[n]]['channelName'])
fhandle.write(' <icon src="https://cdn.jsdelivr.net/gh/BurningC4/tvg-logo@master/%s.png" />\n' % channelID[n])
fhandle.write(' <url>https://tv.cntv.cn</url>\n')
fhandle.write(' </channel>\n')
def getChannelEPG(fhandle, channelID):
#change channelID list to str cids
cids = ''
for x in channelID:
cids = cids + x + ','
for n in range(len(channelID)):
epgdate1 = datetime.now(tz).strftime('%Y%m%d')
epgdate2 = (datetime.now(tz) + timedelta(days=1)).strftime('%Y%m%d')
epgdate3 = (datetime.now(tz) + timedelta(days=2)).strftime('%Y%m%d')
epgdate = [ epgdate1 , epgdate2 , epgdate3 ]
for date in epgdate:
session = requests.Session()
api = "http://api.cntv.cn/epg/epginfo?c=%s&d=%s" % (cids, date)
epgdata = session.get(api).json()
program = epgdata[channelID[n]]['program']
for detail in program:
#write program
st = datetime.fromtimestamp(detail['st']).strftime('%Y%m%d%H%M')+'00'
et = datetime.fromtimestamp(detail['et']).strftime('%Y%m%d%H%M')+'00'
fhandle.write(' <programme start="%s" stop="%s" channel="%s">\n' % (st, et, channelID[n]))
fhandle.write(' <title lang="zh">%s</title>\n' % detail['t'])
fhandle.write(' </programme>\n')
with open(args.path,'w',encoding="utf-8") as fhandle:
fhandle.write('<?xml version="1.0" encoding="utf-8" ?>\n')
fhandle.write('<tv>\n')
getChannelName(fhandle, cctv_channel)
getChannelName(fhandle, sat_channel)
getChannelEPG(fhandle, cctv_channel)
getChannelEPG(fhandle, sat_channel)
fhandle.write('</tv>')
收到,感谢
我尝试使用https://github.com/SoPudge/kodi_iptv_epg 去抓取节目单,但是导入Kodi不识别,能否告知你有修改哪些部分么? 附件时候我导出的xml文件 3Q guide.xml.zip