├── 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" % x.group(1).strip() if len(x.group(1).strip()) != 0 else x.group(0)
70 | pretty_str = re.sub(r'>\n\s*([^<]+)', repl, tvdoc.toprettyxml(indent="\t",encoding="UTF-8"))
71 | f.write(pretty_str)
72 |
--------------------------------------------------------------------------------
/tools.py:
--------------------------------------------------------------------------------
1 | #coding=utf-8
2 | import time,datetime
3 |
4 |
5 | TIME_ZONE=" +0800"
6 | ################ str="xx:xx"
7 | '''
8 | start="20180222044300 +0000" stop="20180222071500 +0000"
9 | '''
10 | ##获取日期字符串20180222044300
11 | def timeTostrOfsec(hs,d,flag=0):
12 |
13 | tmp=d+datetime.timedelta(days=flag)
14 |
15 | return timetofomat(tmp)+hs.replace(":","")+"00"
16 |
17 | ##获取结束时间
18 | def stoptime(startime,duration,d):
19 | duration/=60
20 | list=startime.split(":")
21 | hour=int(list[0])
22 | min=int(list[1])
23 |
24 | min+=duration
25 | hour=hour+(min/60)
26 |
27 | flag=hour
28 |
29 | min%=60
30 | hour%=24
31 |
32 | hs="%02d:%02d"%(hour,min)
33 |
34 | return timeTostrOfsec(hs,d,flag=flag/24)
35 | ##获取开始时间
36 | def startime(hs,d):
37 | return timeTostrOfsec(hs,d)
38 |
39 | ####获取星期几
40 | def isToday():
41 |
42 | today=datetime.datetime.now()
43 |
44 | var=today.weekday()
45 |
46 | return var
47 |
48 | def timetofomat(day):
49 | return day.strftime("%Y%m%d")
50 |
51 |
52 | def getweeklist():
53 | days=[]
54 | monday=datetime.datetime.now()-datetime.timedelta(days=isToday())
55 | for var in range(0,7):
56 |
57 | nextday=monday+datetime.timedelta(days=var)
58 | days.append(nextday)
59 |
60 | return days
61 |
62 | if __name__=="__main__":
63 |
64 | print stoptime("23:00",4800,datetime.datetime.now())
--------------------------------------------------------------------------------