OSDN Git Service

enable to use iEPG.
[rec10/rec10-git.git] / Rec10WEB / trunk / src / rec10web.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 import cgi
6 import cgitb
7 import sqlite3
8 import os
9 import datetime
10 cgitb.enable()
11 path=str(os.path.dirname(os.path.abspath(__file__)))+"/"
12 dbpath=path+"ch.db"
13
14 def getRecDate():
15     db=sqlite3.connect(dbpath)
16     recdata="\"id\",\"type\",\"chtxt\",\"title\",\"btime\",\"etime\",\"deltaday\",\"opt\"\n"
17     recdata="id,type,chtxt,title,btime,etime,deltaday,opt\n"
18     for id, typet, chtxt, title, btime, etime, deltatime ,deltaday ,opt in db.execute("SELECT id, type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime"):
19         if deltaday==None:
20             deltaday="なし"
21         elif deltaday=="":
22             deltaday="なし"
23         if opt==None:
24             opt="なし"
25         if typet=="res":
26             typet="一回予約"
27         elif typet=="key":
28             typet="検索予約"
29         elif typet=="keyevery":
30             typet="隔週予約"
31         elif typet=="res":
32             typet="録画最終"
33         elif typet=="grid":
34             typet="外部委託"
35         elif typet=="ts2avi":
36             typet="EncQue"
37         btime=btime[5:16]
38         etime=etime[5:16]
39         btime=btime.replace("-","/")
40         etime=etime.replace("-","/")
41         if btime[0:5]==etime[0:5]:
42             etime=etime[6:]
43         s="\""+str(id)+"\",\""+typet+"\",\""+chtxt+"\",\""+title+"\",\""+btime+"\",\""+etime+"\",\""+deltaday+"\",\""+opt+"\"\n"
44         s=str(id)+","+typet+","+chtxt+","+title+","+btime+","+etime+","+deltaday+","+opt+"\n"
45         recdata=recdata+s
46     f=open("recdatum.csv","w")
47     f.write(recdata)
48     f.close()
49 def getchtxt():
50     db=sqlite3.connect(dbpath)
51     chopt=""
52     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata"):
53         chdisplay=""
54         for bctype,channel,display in db.execute("SELECT bctype,channel,display FROM ch WHERE channel=\""+ontv+"\""):
55             chdisplay=display
56         chopt=chopt+"<option  value="+chtxt+">"+display+"</option>\n                "
57     db.close()
58     return chopt
59 def reserv(type,keyword,chtxt,btime,etime,deltatime,opt,deltaday):
60     MaxT=2
61     MaxS=2
62     db=sqlite3.connect(dbpath)
63     bt="\'"+btime+"\'"
64     et="\'"+etime+"\'"
65     dbexe="SELECT chdata.bctype,rectime.chtxt,rectime.title FROM rectime INNER JOIN chdata ON rectime.chtxt=chdata.chtxt"
66     #dbexe="SELECT bctype,chtxt,title FROM rectime INNER JOIN chdata ON rectime.chtxt=chdata.chtxt"
67
68     dbexe=dbexe+" WHERE NOT( ( rectime.etime <= "+bt+" ) OR ( rectime.btime >= "+et+" ) )"
69     for bctypet, chtxtt, titlet in db.execute(dbexe):
70         if bctypet.find("cs")>-1:
71             MaxS=MaxS-1
72         elif bctypet.find("bs")>-1:
73             MaxS=MaxS-1
74         elif bctypet.find("te")>-1:
75             MaxT=MaxT-1
76     bctype=""
77     rett=""
78     for bctypet, chtxtt in db.execute("SELECT bctype,chtxt FROM chdata WHERE chtxt=\'"+chtxt+"\'"):
79         bctype=bctypet
80     if bctype.find("cs")>-1:
81         MaxS=MaxS-1
82     elif bctype.find("bs")>-1:
83         MaxS=MaxS-1
84     elif bctype.find("te")>-1:
85         MaxT=MaxT-1
86     if MaxS>-1 and MaxT>-1:
87         recline="\""+type+"\",\""+chtxt+"\",\""+keyword+"\",datetime(\""+btime+"\"),datetime(\""+etime+"\"),\""+deltatime+"\",\""+opt+"\",\""+deltaday+"\""
88         #print recline
89         db.execute('insert into rectime ("type","chtxt","title","btime","etime","deltatime","opt","deltaday") values ('+recline+')')
90         db.commit()
91     else:
92         rett="over"
93     db.close()
94     return rett
95 def station2chtxt(station):
96     db=sqlite3.connect(dbpath)
97     ret=""
98     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE station=\""+station+"\""):
99         ret=chtxt
100     db.close()
101     return ret
102 def station_name2chtxt(station):
103     db=sqlite3.connect(dbpath)
104     ret=""
105     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE station_name=\""+station+"\""):
106         ret=chtxt
107     db.close()
108     return ret
109 def readHtmlSrcSimple():
110     f = open(path+'rswi.htm')
111     ret=f.read()
112     f.close()
113     return ret
114
115 def readRecFinisSimple():
116     f = open(path+'fin.htm')
117     ret=f.read()
118     f.close()
119     return ret
120 def readRecFinisOver():
121     f = open(path+'finover.htm')
122     ret=f.read()
123     f.close()
124     return ret
125 def readRecSchedule():
126     f = open(path+'schedule.htm')
127     ret=f.read()
128     f.close()
129     return ret
130 f=cgi.FieldStorage()
131
132 btime=""
133 etime=""
134 title=""
135 chtxt=""
136 type="res"
137 bt=""
138 getRecDate()
139 if f.getfirst('mode')=="schedule":
140     getRecDate()
141
142 ###ここからiEPG用の読み出し
143 if f.getfirst('station_name'):
144     station=f.getfirst('station_name')
145     chtxt=station_name2chtxt(station)
146 if f.getfirst('station'):
147     station=f.getfirst('station')
148     chtxt=station2chtxt(station)
149 ###ここから外部からの読み出しの場合
150 if f.getfirst('type'):
151     type=f.getfirst('type')
152 if f.getfirst('title'):
153     title=f.getfirst('title')
154 if f.getfirst('chtxt'):
155     chtxt=f.getfirst('chtxt')
156 if f.getfirst('btime'):
157     btime=f.getfirst('btime')
158     bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
159 if f.getfirst('etime'):
160     etime=f.getfirst('etime')
161     et=datetime.datetime.strptime(etime,"%Y-%m-%d %H:%M:%S")
162     if bt !="":
163         delt=et-bt
164         dt=delt.days*24*60*60+delt.seconds
165         if dt<0:
166             dd=datetime.timedelta(days=1)
167             et=et+dd
168             etime=et.strftime("%Y-%m-%d %H:%M:%S")
169         if dt<-1*24*60*60:
170             d1=datetime.datetime(bt.year,0,0)
171             d2=datetime.datetime(bt.year+1,0,0)
172             dd=d2-d1
173             et=et+dd
174             etime=et.strftime("%Y-%m-%d %H:%M:%S")
175         
176 size=""
177 if f.getfirst('size'):
178     size=f.getfirst('size')
179 opts=""
180 if f.getfirst('opts'):
181     opts=f.getfirst('opts')
182 deltahour=""
183 if f.getfirst('deltahour',""):
184     deltahour=f.getfirst('deltahour',"")
185 deltaday=""
186 if f.getfirst('deltaday',""):
187     deltaday=f.getfirst('deltaday',"")
188 exect=""
189 if f.getfirst('exec',""):
190     exect=f.getfirst('exec',"")
191
192 htmdate= readHtmlSrcSimple()
193 htmdate=htmdate.replace("<!--text_for_replace_chtxt_input//-->",getchtxt())
194
195 htmdate=htmdate.replace("<!--btime-value//-->",btime)
196 htmdate=htmdate.replace("<!--etime-value//-->",etime)
197 htmdate=htmdate.replace("<!--type-value//-->",type)
198 htmdate=htmdate.replace("<!--ch-value//-->",chtxt)
199 htmdate=htmdate.replace("<!--title-value//-->",title)
200 htmdate=htmdate.replace("<!--size-value//-->",size)
201 htmdate=htmdate.replace("<!--opts-value//-->",opts)
202
203 if (chtxt != "")and(title != "")and(btime!="")and(etime!="")and(opts!="")and(exect=="yes"):
204     restxt=reserv(type, title, chtxt, btime, etime, deltahour, opts, deltaday)
205     if restxt=="":
206         htmdate=readRecFinisSimple()
207     elif restxt=="over":
208         htmdate=readRecFinisOver()
209     exect=""
210 #
211 if exect=="schedule":
212     htmdate=readRecSchedule()
213 print htmdate
214