OSDN Git Service

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