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.b25")):
91         os.remove(os.path.join(tmppath,"ch_"+str(ch)+".ts.b25"))
92     if os.path.exists(os.path.join(tmppath,"ch_"+str(ch)+".ts")):
93         os.remove(os.path.join(tmppath,"ch_"+str(ch)+".ts"))
94         return 1
95     else:
96         return 0
97
98 def addCh(ch,bctype,chtxthead):
99     prglist=getChProgNum(ch)##[prglist,logt]
100     for lstr in prglist[0]:
101         #print [bctype,chtxthead+"_"+lstr, ch, lstr]
102         try:
103             if int(lstr)<10000:
104                 if ch.find("bs")>-1:
105                     if int(lstr)<300:
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                 else:
109                     tnow=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
110                     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)
111         except Exception, inst:
112             recdblist.addCommonlogEX("Warning", "addCh (scan_ch.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
113     checkB25Purchased(ch, prglist[1])
114 def getChProgNum(ch):
115     pout=os.path.join(tmppath,"ch_"+str(ch)+".ts")
116     logt=tv2ts.tv2tsmix(pout, ch, "10")
117     retl=getTsProgNum(ch,pout)
118     try:
119         os.remove(pout)
120     except:
121         ""
122     return [retl,logt]
123 def getTsProgNum(ch,tspath):
124     pin=tspath
125     pout=os.path.join(tmppath,"chscan.txt")
126     chopt="-ch"
127     if ch.find("CS")>-1 or ch.find("BS")>-1 :
128         chopt="-cs"
129     elif int(ch)>99:
130         chopt="-bs"
131     doexe = tssplitterp + " "+chopt+" \""+ pin + "\" \""+ pout + "\""
132     doexe = "nice -n 18 " + doexe
133     os.environ['LANG']="ja_JP.UTF-8"
134     os.system(u"touch \""+pout+"\"")
135     txt = unicode(commands.getoutput(doexe.encode('utf-8')),'utf-8','ignore')
136     f=open(pout)
137     lines=f.readlines()
138     f.close()
139     ret=[]
140     for l in lines:
141         try:
142             l=l.replace(" ","")
143             l=l.replace("\n","")
144             ret.append(str(int(l)))
145         except:
146             ""
147     recdblist.addLog(pin, doexe, u"TsSplitログ-コマンド")
148     recdblist.addLog(pin, txt, u"TsSplitログ-詳細")
149     time.sleep(1)
150     try:
151         ""
152         os.remove(pout)
153     except:
154         ""
155     return ret
156 def getB25UnparchasedList(logtxt):
157     warning = 0
158     ret=[]
159     for strt in logtxt.splitlines():
160         if strt.find("unpurchased ECM")>-1:
161             warning=1
162         if strt.find("total TS packet")>-1 and warning>0:
163             warning=0
164         if strt.find("channel")>-1 and warning>0:
165             txt=strt.replace("channel:","")
166             txt=txt.replace("\n","")
167             txt=txt.replace(" ","")
168             ret.append(txt)
169     return ret
170 def checkB25Purchased(ch,logtxt):
171     chlists=chdb.searchAllCHFromCh(ch)
172     upl=getB25UnparchasedList(logtxt)
173     if len(upl)>0 and len(chlists)>0:
174         for chl in chlists:
175             if chl != None:
176                 if chl['csch'] in upl:
177                     chdb.changeCHShow(chl['chtxt'],"0")