├── Base.py ├── CCTV_EPG.xml ├── README.md ├── main.py └── tools.py /Base.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import urllib2,json,tools 3 | ''' 4 | 5 | 6 | 7 | 8 | 9 | CCTV 1 CN 10 | 11 | 12 | 星光大道2018-9 星光大道嘉年华(130分钟) 13 | 14 | 15 | 16 | ''' 17 | ''' 18 | { 'tv':{ 19 | {'channel':{ 20 | 'dispaly-name': 21 | 22 | } 23 | } 24 | } 25 | } 26 | 27 | ''' 28 | 29 | ####API网址 30 | 31 | Interface="http://api.cntv.cn/epg/epginfo" 32 | 33 | 34 | 35 | 36 | ################base##################### 37 | 38 | #向interface提交参数过去节目列表 json格式 39 | def createJsonWithTVname(name,date): 40 | 41 | file=urllib2.urlopen(Interface+"?"+"c="+name+"&serviceId=channel&d="+tools.timetofomat(date)+"&t=json") 42 | ###json 格式 43 | parser = json.load(file,encoding="utf-8") 44 | 45 | return parser[name] 46 | 47 | ###从tv节目列表中获取节目列表--->list 48 | def getProgramsWithTV(tvParser): 49 | 50 | islive="" 51 | 52 | programs=tvParser['program'] 53 | 54 | return islive,programs 55 | 56 | 57 | ###从节目列表获取节目开始结束时间---->str ,start,stop 58 | def getDateWithElement(ele,d): 59 | start=ele['showTime'] 60 | duration=ele["duration"] 61 | 62 | return tools.startime(start,d),tools.stoptime(start,duration,d) 63 | def getNameOfprograms(ele): 64 | name=ele['t'].encode("utf-8") 65 | return name -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EPG 2 | ## 利用央视网节目单json接口抓取CCTV1-CCTV15一周内节目单 3 | 可用于Perfect Player IPTV 等支持xml节目表的IPTV播放器 4 | ## 配置方法: 5 | ### 环境:Python,Openwrt 6 | ### 步骤: 7 | #### 将源程序上传到Openwrt web根目录 8 | #### 设置定时任务:crontab -e 9 | #### 设置每天12点更新:00 00 * * * /usr/local/bin/python main.py 10 | ## 使用方法: 11 | #### IPTV播放器设置节目单网络源 http://路由器IP/CCTV_EPG.xml即可获取节目表 12 | ### 注意: 13 | 运行脚本后会在当前目录生成 *.xml节目表,URL与实际所放的Web路径有关 14 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | import Base,tools,sys,re 3 | from xml.dom.minidom import Document 4 | reload(sys) 5 | sys.setdefaultencoding('utf8') 6 | 7 | 8 | TVs=["cctv1","cctv2","cctv3","cctv4","cctveurope","cctvamerica","cctv5","cctv5plus","cctv6","cctv7","cctv8","cctvjilu","cctv10","cctv11","cctv12","cctv13","cctvchild","cctv15"] 9 | ##TVs=["cctv1"] 10 | if __name__=="__main__": 11 | tvdoc=Document() 12 | ###tv根节点 13 | tv=tvdoc.createElement("tv") 14 | tv.setAttribute("generator-info-name","Generated by JayAi ") 15 | tv.setAttribute("generator-info-url","http://nas.codeasy.cn") 16 | tvdoc.appendChild(tv) 17 | ###写入节目列表 18 | for var in TVs: 19 | ###channel 标签 20 | channel=tvdoc.createElement("channel") 21 | channel.setAttribute("id",var) 22 | 23 | 24 | 25 | ###display-name 26 | display_name=tvdoc.createElement("display-name") 27 | display_name.setAttribute("lang","zh") 28 | ###display-name 标签中的值 29 | display_name_var=tvdoc.createTextNode(var.upper()) 30 | display_name.appendChild(display_name_var) 31 | ###添加到channel节点 32 | channel.appendChild(display_name) 33 | ###添加到根标签 34 | tv.appendChild(channel) 35 | ''' 36 | 37 | 38 | 生活早参考-特别节目(生活圈)2018-35 39 | 40 | 41 | ''' 42 | ##星期1 43 | days=tools.getweeklist() 44 | for var in TVs: 45 | 46 | for day in days: 47 | parser = Base.createJsonWithTVname(var, day) 48 | islive, pro = Base.getProgramsWithTV(parser) 49 | for ele in pro: 50 | start,stop=Base.getDateWithElement(ele,day) 51 | pname=Base.getNameOfprograms(ele) 52 | 53 | programme=tvdoc.createElement("programme") 54 | title = tvdoc.createElement("title") 55 | text=tvdoc.createTextNode(pname) 56 | 57 | programme.setAttribute("start",start+tools.TIME_ZONE) 58 | programme.setAttribute("stop", stop + tools.TIME_ZONE) 59 | programme.setAttribute("channel",var) 60 | 61 | title.setAttribute("lang","zh") 62 | 63 | title.appendChild(text) 64 | programme.appendChild(title) 65 | 66 | tv.appendChild(programme) 67 | 68 | with open("CCTV_EPG.xml","w") as f: 69 | repl = lambda x: ">%s\n\s*([^<]+)