OSDN Git Service

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