OSDN Git Service

add tsid channel information for complete BS support.
[rec10/rec10-git.git] / rec10 / trunk / src / scan_ch.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2012 Yukikaze
5 import os
6 import os.path
7 import time
8 import commands
9 import traceback
10 import datetime
11
12 import chdb
13 import rec10d
14 import configreader
15 import recdblist
16 import tv2ts
17 tmppath = configreader.getConfPath("tmp")+"/"
18 if not os.path.exists(tmppath):
19     os.mkdir(tmppath)
20 tssplitterp =configreader.getConfPath("java")+" -jar "+configreader.getConfPath("jTsSplitter")
21 def searchCh():
22     if rec10d.rec10db.select_installed_in_status()<2:
23         rec10d.rec10db.change_installed_in_status(2)
24         #地上デジタル
25         for i in xrange(13,52):#62
26             recdblist.printutf8ex(u"チャンネルスキャン:地上デジタル CH "+str(i), 200, 200)
27             if checkTs(str(i))>0:
28                 addCh(str(i),u"te"+str(i),str(i))
29         time.sleep(1)
30         #BSデジタル
31         recdblist.printutf8ex(u"チャンネルスキャン:BSデジタル ", 200, 200)
32         for i in xrange(100,299):#240
33             recdblist.printutf8ex(u"チャンネルスキャン:BSデジタル CH "+str(i), 200, 200)
34             if checkTs(str(i))>0:
35                 addCh(str(i),u"bs","BS")
36         for i2 in xrange(0,11):
37             chi=u"BS"+str(2*i2+1)
38             recdblist.printutf8ex(u"チャンネルスキャン:BSデジタル CH "+chi, 200, 200)
39             for ts_offset in xrange(0,3):
40                 for ts_idx in xrange(0,7):
41                     t_tsid=16384+chi*16+ts_offset*512+ts_idx
42                     recdblist.printutf8ex(u"TransportID "+str(t_tsid), 200, 200)
43                     if checkTs(chi,str(t_tsid))>0:
44                         addCh(str(chi),u"bs","BS",str(t_tsid))
45         time.sleep(1)
46         #スカパーe2!
47         #http://www5e.biglobe.ne.jp/~kazu_f/digital-sat/trapon-nsat110.html
48         #
49         #CS1ネットワーク
50         if checkTs("CS2")>0:
51             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS2 ", 200, 200)
52             addCh("CS2","cs1","CS")
53         if checkTs("CS8")>0:
54             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS8", 200, 200)
55             addCh("CS8","cs1","CS")
56         if checkTs("CS10")>0:
57             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS10", 200, 200)
58             addCh("CS10","cs1","CS")
59         #CS2ネットワーク
60         if checkTs("CS4")>0:
61             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS4", 200, 200)
62             addCh("CS4","cs2","CS")
63         if checkTs("CS6")>0:
64             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS6", 200, 200)
65             addCh("CS6","cs2","CS")
66         if checkTs("CS12")>0:
67             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS12", 200, 200)
68             addCh("CS12","cs2","CS")
69         if checkTs("CS14")>0:
70             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS14", 200, 200)
71             addCh("CS14","cs2","CS")
72         if checkTs("CS16")>0:
73             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS16", 200, 200)
74             addCh("CS16","cs2","CS")
75         if checkTs("CS18")>0:
76             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS18", 200, 200)
77             addCh("CS18","cs2","CS")
78         if checkTs("CS20")>0:
79             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS20", 200, 200)
80             addCh("CS20","cs2","CS")
81         if checkTs("CS22")>0:
82             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS22", 200, 200)
83             addCh("CS22","cs2","CS")
84         if checkTs("CS24")>0:
85             recdblist.printutf8ex(u"チャンネルスキャン:CSSデジタル CH:CS24", 200, 200)
86             addCh("CS24","cs2","CS")
87         rec10d.rec10db.change_installed_in_status(100)
88 def checkTs(ch,tsid="0"):
89     if os.path.exists(os.path.join(tmppath,"ch_"+str(ch)+".ts")):
90         os.remove(os.path.join(tmppath,"ch_"+str(ch)+".ts"))
91     if os.path.exists(os.path.join(tmppath,"ch_"+str(ch)+".ts.b25")):
92         os.remove(os.path.join(tmppath,"ch_"+str(ch)+".ts.b25"))
93     tv2ts.tv2b25ts(os.path.join(tmppath,"ch_"+str(ch)+".ts"), ch, "1",tsid)
94     if os.path.exists(os.path.join(tmppath,"ch_"+str(ch)+".ts.b25")):
95         os.remove(os.path.join(tmppath,"ch_"+str(ch)+".ts.b25"))
96     if os.path.exists(os.path.join(tmppath,"ch_"+str(ch)+".ts")):
97         os.remove(os.path.join(tmppath,"ch_"+str(ch)+".ts"))
98         return 1
99     else:
100         return 0
101
102 def addCh(ch,bctype,chtxthead,tsid="0"):
103     prglist=getChProgNum(ch)##[prglist,logt]
104     for lstr in prglist[0]:
105         #print [bctype,chtxthead+"_"+lstr, ch, lstr]
106         try:
107             if int(lstr)<1000000:
108                 if ch.find("bs")>-1:
109                     if int(lstr)<300:
110                         tnow=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
111                         rec10d.rec10db.add_epg_ch(bctype,chtxthead+"_"+lstr, ch, lstr,tsid, u"2010-01-01 00:00:00", u"2010-01-01 00:00:00",tnow)
112                 else:
113                     tnow=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
114                     rec10d.rec10db.add_epg_ch(bctype,chtxthead+"_"+lstr, ch, lstr,tsid, u"2010-01-01 00:00:00", u"2010-01-01 00:00:00",tnow)
115         except Exception, inst:
116             recdblist.addCommonlogEX("Warning", "addCh (scan_ch.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
117     checkB25Purchased(ch, prglist[1])
118 def getChProgNum(ch,tsid="0"):
119     pout=os.path.join(tmppath,"ch_"+str(ch)+".ts")
120     if tsid != "0":
121         pout=os.path.join(tmppath,"ch_"+str(ch)+"_"+tsid+".ts")
122     logt=tv2ts.tv2tsmix(pout, ch, "10",tsid)
123     retl=getTsProgNum(ch,pout)
124     try:
125         os.remove(pout)
126     except:
127         ""
128     return [retl,logt]
129 def getTsProgNum(ch,tspath):
130     pin=tspath
131     pout=os.path.join(tmppath,"chscan.txt")
132     chopt="-ch"
133     if ch.find("CS")>-1 or ch.find("BS")>-1 :
134         chopt="-cs"
135     elif int(ch)>99:
136         chopt="-bs"
137     doexe = tssplitterp + " "+chopt+" \""+ pin + "\" \""+ pout + "\""
138     doexe = "nice -n 18 " + doexe
139     os.environ['LANG']="ja_JP.UTF-8"
140     os.system(u"touch \""+pout+"\"")
141     txt = unicode(commands.getoutput(doexe.encode('utf-8')),'utf-8','ignore')
142     f=open(pout)
143     lines=f.readlines()
144     f.close()
145     ret=[]
146     for l in lines:
147         try:
148             l=l.replace(" ","")
149             l=l.replace("\n","")
150             ret.append(str(int(l)))
151         except:
152             ""
153     recdblist.addLog(pin, doexe, u"TsSplitログ-コマンド")
154     recdblist.addLog(pin, txt, u"TsSplitログ-詳細")
155     time.sleep(1)
156     try:
157         ""
158         os.remove(pout)
159     except:
160         ""
161     return ret
162 def getB25UnparchasedList(logtxt):
163     warning = 0
164     ret=[]
165     for strt in logtxt.splitlines():
166         if strt.find("unpurchased ECM")>-1:
167             warning=1
168         if strt.find("total TS packet")>-1 and warning>0:
169             warning=0
170         if strt.find("channel")>-1 and warning>0:
171             txt=strt.replace("channel:","")
172             txt=txt.replace("\n","")
173             txt=txt.replace(" ","")
174             ret.append(txt)
175     return ret
176 def checkB25Purchased(ch,logtxt):
177     chlists=chdb.searchAllCHFromCh(ch)
178     upl=getB25UnparchasedList(logtxt)
179     if len(upl)>0 and len(chlists)>0:
180         for chl in chlists:
181             if chl != None:
182                 if chl['csch'] in upl:
183                     chdb.changeCHShow(chl['chtxt'],"0")