OSDN Git Service

bcfd0fee3b872f9343bfdb6db17c293129237b17
[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 getchtxt():
15     db=sqlite3.connect(dbpath)
16     chopt=""
17     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata"):
18         chdisplay=""
19         for bctype,channel,display in db.execute("SELECT bctype,channel,display FROM ch WHERE channel=\""+ontv+"\""):
20             chdisplay=display
21         chopt=chopt+"<option  value="+chtxt+">"+display+"</option>\n                "
22     db.close()
23     return chopt
24 def reserv(type,keyword,chtxt,btime,etime,deltatime,opt,deltaday):
25     MaxT=2
26     MaxS=2
27     db=sqlite3.connect(dbpath)
28     bt="\'"+btime+"\'"
29     et="\'"+etime+"\'"
30     dbexe="SELECT chdata.bctype,rectime.chtxt,rectime.title FROM rectime INNER JOIN chdata ON rectime.chtxt=chdata.chtxt"
31     #dbexe="SELECT bctype,chtxt,title FROM rectime INNER JOIN chdata ON rectime.chtxt=chdata.chtxt"
32
33     dbexe=dbexe+" WHERE NOT( ( rectime.etime <= "+bt+" ) OR ( rectime.btime >= "+et+" ) )"
34     for bctypet, chtxtt, titlet in db.execute(dbexe):
35         if bctypet.find("cs")>-1:
36             MaxS=MaxS-1
37         elif bctypet.find("bs")>-1:
38             MaxS=MaxS-1
39         elif bctypet.find("te")>-1:
40             MaxT=MaxT-1
41     bctype=""
42     rett=""
43     for bctypet, chtxtt in db.execute("SELECT bctype,chtxt FROM chdata WHERE chtxt=\'"+chtxt+"\'"):
44         bctype=bctypet
45     if bctype.find("cs")>-1:
46         MaxS=MaxS-1
47     elif bctype.find("bs")>-1:
48         MaxS=MaxS-1
49     elif bctype.find("te")>-1:
50         MaxT=MaxT-1
51     if MaxS>-1 and MaxT>-1:
52         recline="\""+type+"\",\""+chtxt+"\",\""+keyword+"\",datetime(\""+btime+"\"),datetime(\""+etime+"\"),\""+deltatime+"\",\""+opt+"\",\""+deltaday+"\""
53         #print recline
54         db.execute('insert into rectime ("type","chtxt","title","btime","etime","deltatime","opt","deltaday") values ('+recline+')')
55         db.commit()
56     else:
57         rett="over"
58     db.close()
59     return rett
60 def station2chtxt(station):
61     db=sqlite3.connect(dbpath)
62     ret=""
63     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE station=\""+station+"\""):
64         ret=chtxt
65     db.close()
66     return ret
67 def readHtmlSrcSimple():
68     f = open(path+'rswi.htm')
69     ret=f.read()
70     f.close()
71     return ret
72
73 def readRecFinisSimple():
74     f = open(path+'fin.htm')
75     ret=f.read()
76     f.close()
77     return ret
78 def readRecFinisOver():
79     f = open(path+'finover.htm')
80     ret=f.read()
81     f.close()
82     return ret
83 f=cgi.FieldStorage()
84
85 btime=""
86 etime=""
87 title=""
88 chtxt=""
89 type="res"
90 bt=""
91 ###ここからiEPG用の読み出し
92 if f.getfirst('station'):
93     station=f.getfirst('station')
94     chtxt=station2chtxt(station)
95 ###ここから外部からの読み出しの場合
96 if f.getfirst('type'):
97     type=f.getfirst('type')
98 if f.getfirst('title'):
99     title=f.getfirst('title')
100 if f.getfirst('chtxt'):
101     chtxt=f.getfirst('chtxt')
102 if f.getfirst('btime'):
103     btime=f.getfirst('btime')
104     bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
105 if f.getfirst('etime'):
106     etime=f.getfirst('etime')
107     et=datetime.datetime.strptime(etime,"%Y-%m-%d %H:%M:%S")
108     if bt !="":
109         delt=et-bt
110         dt=delt.days*24*60*60+delt.seconds
111         if dt<0:
112             dd=datetime.timedelta(days=1)
113             et=et+dd
114             etime=et.strftime("%Y-%m-%d %H:%M:%S")
115         if dt<-1*24*60*60:
116             d1=datetime.datetime(bt.year,0,0)
117             d2=datetime.datetime(bt.year+1,0,0)
118             dd=d2-d1
119             et=et+dd
120             etime=et.strftime("%Y-%m-%d %H:%M:%S")
121         
122 size=""
123 if f.getfirst('size'):
124     size=f.getfirst('size')
125 opts=""
126 if f.getfirst('opts'):
127     opts=f.getfirst('opts')
128 deltahour=""
129 if f.getfirst('deltahour',""):
130     deltahour=f.getfirst('deltahour',"")
131 deltaday=""
132 if f.getfirst('deltaday',""):
133     deltaday=f.getfirst('deltaday',"")
134 exect=""
135 if f.getfirst('exec',""):
136     exect=f.getfirst('exec',"")
137     
138 htmdate= readHtmlSrcSimple()
139 htmdate=htmdate.replace("<!--text_for_replace_chtxt_input//-->",getchtxt())
140
141 htmdate=htmdate.replace("<!--btime-value//-->",btime)
142 htmdate=htmdate.replace("<!--etime-value//-->",etime)
143 htmdate=htmdate.replace("<!--type-value//-->",type)
144 htmdate=htmdate.replace("<!--ch-value//-->",chtxt)
145 htmdate=htmdate.replace("<!--title-value//-->",title)
146 htmdate=htmdate.replace("<!--size-value//-->",size)
147 htmdate=htmdate.replace("<!--opts-value//-->",opts)
148
149 if (chtxt != "")and(title != "")and(btime!="")and(etime!="")and(opts!="")and(exect=="yes"):
150     restxt=reserv(type, title, chtxt, btime, etime, deltahour, opts, deltaday)
151     if restxt=="":
152         htmdate=readRecFinisSimple()
153     elif restxt=="over":
154         htmdate=readRecFinisOver()
155     exect=""
156 #
157 print htmdate
158