OSDN Git Service

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