OSDN Git Service

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