OSDN Git Service

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