OSDN Git Service

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