OSDN Git Service

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