OSDN Git Service

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