OSDN Git Service

implement new count.
[rec10/rec10-git.git] / Rec10WEB / trunk / src / rec10web.py
index 9594d13..ee77e1f 100644 (file)
@@ -3,20 +3,52 @@
 # Rec10 TS Recording Tools
 # Copyright (C) 2009 Yukikaze
 import cgi
-import cgitb
-import sqlite3
+import cgitb;cgitb.enable()
+import rec10web_dbMySQL
+import rec10web_dbSQLite
 import os
 import re
 import datetime
-cgitb.enable()
+import rec10web_dbSQLite
+import ConfigParser
+import time
 path=str(os.path.dirname(os.path.abspath(__file__)))+"/"
-dbpath=path+"ch.db"
+confp=ConfigParser.SafeConfigParser()
+Conf='config.ini'
+confp.read(path+Conf)
+def getpath(string):
+    global confp
+    return confp.get('path',string)
+def getdbpath(string):
+    global confp
+    return confp.get('db',string)
 
+def getdb():
+    retdb=""
+    if getdbpath('db')=='MySQL':
+        dbn=getdbpath("mysql_dbname")
+        dbh=getdbpath("mysql_host")
+        dbu=getdbpath("mysql_user")
+        dbpwd=getdbpath("mysql_passwd")
+        dbport=int(getdbpath("mysql_port"))
+        retdb=rec10web_dbMySQL.DB_MySQL(dbname=dbn,host=dbh,user=dbu,passwd=dbpwd,port=dbport)
+    else:
+        retdb=rec10web_dbSQLite.DB_SQLite(dbpath)
+    return retdb
 def getRecDate():
-    db=sqlite3.connect(dbpath)
-    recdata="\"id\",\"type\",\"chtxt\",\"title\",\"btime\",\"etime\",\"deltaday\",\"opt\"\n"
+    dl=database.select_all_rectime()
     recdata="id,type,chtxt,title,btime,etime,deltaday,opt\n"
-    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"):
+    #print dl
+    for d in dl:
+        id=d['id']
+        typet=d['type']
+        chtxt=d['chtxt']
+        title=d['title']
+        btime=d['btime']
+        etime=d['etime']
+        deltatime=d['deltatime']
+        deltaday=d['deltaday']
+        opt=d['opt']
         if deltaday==None:
             deltaday="なし"
         elif deltaday=="":
@@ -34,7 +66,10 @@ def getRecDate():
         elif typet=="grid":
             typet="外部委託"
         elif typet=="ts2avi":
-            typet="EncQue"
+            typet="縁故予約"
+        elif typet=="b252ts":
+            typet="復号予約"
+        #print btime
         btime=btime[5:16]
         etime=etime[5:16]
         btime=btime.replace("-","/")
@@ -48,43 +83,27 @@ def getRecDate():
     f.write(recdata)
     f.close()
 def getchtxt():
-    db=sqlite3.connect(dbpath)
+    dl=database.select_all_chdata()
     chopt=""
-    for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata"):
+    for bctype, ontv, chtxt, ch, csch ,updatetime in dl:
         chdisplay=""
-        for bctype,channel,display in db.execute("SELECT bctype,channel,display FROM ch WHERE channel=\""+ontv+"\""):
+        for bctype,channel,display in database.select_by_ontv_ch(ontv):
             chdisplay=display
-        chopt=chopt+"<option  value="+chtxt+">"+display+"</option>\n                "
-    db.close()
+        if chdisplay!="":
+            chopt=chopt+"<option  value="+chtxt+">"+chdisplay+"</option>\n                "
     return chopt
 def edit_date(id):
-    db=sqlite3.connect(dbpath)
-    ret={}
-    for idt,typet,chtxt,title,btime,etime,deltatime,opt,deltaday in db.execute("SELECT id,type,chtxt,title,btime,etime,deltatime,opt,deltaday From rectime WHERE id ="+id):
-        ret['id']=idt
-        ret['type']=typet
-        ret['chtxt']=chtxt
-        ret['title']=title
-        ret['btime']=btime
-        ret['etime']=etime
-        try:
-            ret['deltatime']=deltatime
-        except:
-            ret['deltatime']=""
-        ret['opt']=opt
-        try:
-            ret['deltaday']=daltaday
-        except:
-            ret['deltaday']=""
-    db.close()
-    return ret
+    ret=database.select_by_id_rectime(id)
+    return ret[0]
 
 def reserv_recday(keyword,chtxt,btime,etime,deltatime,opt,deltaday,day):
     """
     dayは月曜日を1日曜日を7とした数字
     """
-    bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
-    et=datetime.datetime.strptime(etime,"%Y-%m-%d %H:%M:%S")
+    #bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
+    #et=datetime.datetime.strptime(etime,"%Y-%m-%d %H:%M:%S")
+    bt=datetime.datetime(*time.strptime(btime,"%Y-%m-%d %H:%M:%S")[:-3])
+    et=datetime.datetime(*time.strptime(etime,"%Y-%m-%d %H:%M:%S")[:-3])
     dt=int(day)-bt.isoweekday()
     if dt<0:
         dt=dt+7
@@ -96,24 +115,13 @@ def reserv_recday(keyword,chtxt,btime,etime,deltatime,opt,deltaday,day):
 def reserv(type,keyword,chtxt,btime,etime,deltatime,opt,deltaday):
     MaxT=2
     MaxS=2
-    db=sqlite3.connect(dbpath)
-    bt="\'"+btime+"\'"
-    et="\'"+etime+"\'"
-    dbexe="SELECT type,chdata.bctype,rectime.chtxt,rectime.title FROM rectime INNER JOIN chdata ON rectime.chtxt=chdata.chtxt"
-    #dbexe="SELECT bctype,chtxt,title FROM rectime INNER JOIN chdata ON rectime.chtxt=chdata.chtxt"
-
-    dbexe=dbexe+" WHERE NOT( ( rectime.etime <= "+bt+" ) OR ( rectime.btime >= "+et+" ) )"
-    for typet,bctypet, chtxtt, titlet in db.execute(dbexe):
-        if (typet=="rec") or (typet=="res") or (typet=="key") or (typet=="keyevery"):
-            if bctypet.find("cs")>-1:
-                MaxS=MaxS-1
-            elif bctypet.find("bs")>-1:
-                MaxS=MaxS-1
-            elif bctypet.find("te")>-1:
-                MaxT=MaxT-1
+    tsnum=database.count_schedule_rectime(btime,etime)
+    MaxT=MaxT-tsnum[0]
+    MaxS=MaxS-tsnum[1]
     bctype=""
     rett=""
-    for bctypet, chtxtt in db.execute("SELECT bctype,chtxt FROM chdata WHERE chtxt=\'"+chtxt+"\'"):
+    bctypegetdb=database.select_by_chtxt_chdata(chtxt)
+    for bctypet,ontvt,chtxtt,cht,cscht,updatetimet in bctypegetdb:
         bctype=bctypet
     if (type=="rec") or (type=="res") or (type=="key") or (type=="keyevery"):
         if bctype.find("cs")>-1:
@@ -123,41 +131,90 @@ def reserv(type,keyword,chtxt,btime,etime,deltatime,opt,deltaday):
         elif bctype.find("te")>-1:
             MaxT=MaxT-1
     if MaxS>-1 and MaxT>-1:
-        recline="\""+type+"\",\""+chtxt+"\",\""+keyword+"\",datetime(\""+btime+"\"),datetime(\""+etime+"\"),\""+deltatime+"\",\""+opt+"\",\""+deltaday+"\""
-        #print recline
-        db.execute('insert into rectime ("type","chtxt","title","btime","etime","deltatime","opt","deltaday") values ('+recline+')')
-        db.commit()
+        database.add_rectime(type=type,chtxt=chtxt,title=keyword,btime=btime,etime=etime,deltatime=deltatime,deltaday=deltaday,opt=opt)
     else:
         rett="over"
-    db.close()
     return rett
+def count_schedule(btime,etime):
+    """
+    return rec num (int return[0]:TE /int return[1]:BS/CS)
+    """
+    #typet,bctypet, chtxtt,titlet,btime,etime
+    def rectime_count(btimeb,etimee,liststemp):
+        ##btimeからetimeまでの中に入る数を数えるとともに
+        #リストのbtime,etimeを変更する
+        #[[tenum,bscsnum],retlists]
+        #typet,bctypet, chtxtt,titlet,btime,etime
+        bt=datetime.datetime(*time.strptime(btimeb,"%Y-%m-%d %H:%M:%S")[:-3])
+        et=datetime.datetime(*time.strptime(etimee,"%Y-%m-%d %H:%M:%S")[:-3])
+        lists=liststemp
+        count=[0,0]#te,bscs
+        bttemp
+        retlists=[]
+        for list in lists:
+            btt=datetime.datetime(*time.strptime(list[4],"%Y-%m-%d %H:%M:%S")[:-3])
+            ett=datetime.datetime(*time.strptime(list[5],"%Y-%m-%d %H:%M:%S")[:-3])
+            #NOT( ( rectime.etime <= ? ) OR ( rectime.btime >= ? )
+            if not ((ett<= bt) or (btt >= et)):
+                if list[1].find("cs")>-1:
+                    count[1]=count[1]+1
+                elif list[1].find("bs")>-1:
+                    count[1]=count[1]+1
+                elif list[1].find("te")>-1:
+                    count[0]=count[0]+1
+            """if ettemp>ett:
+                ettemp=et"""
+            if btt<et and ett>et:
+                btt=et
+            if btt>=et:
+                btimett=btt.strftime("%Y-%m-%d %H:%M:%S")
+                etimett=ett.strftime("%Y-%m-%d %H:%M:%S")
+                retlists.append([list[0],list[1],list[2],list[3],btimett,etimett])
+        return [count,retlists]
+            #btime=dbt.strftime("%Y-%m-%d %H:%M:%S")
+            #etime=det.strftime("%Y-%m-%d %H:%M:%S")
+    dls=database.schedule_rectime(btime,etime)
+    btemp=datetime.datetime(*time.strptime(dis[0][4],"%Y-%m-%d %H:%M:%S")[:-3])
+    etemp=datetime.datetime(*time.strptime(dis[0][5],"%Y-%m-%d %H:%M:%S")[:-3])
+    retcount=[0,0]
+    while retend==0:
+        btemp=datetime.datetime(*time.strptime(dis[0][4],"%Y-%m-%d %H:%M:%S")[:-3])
+        etemp=datetime.datetime(*time.strptime(dis[0][5],"%Y-%m-%d %H:%M:%S")[:-3])
+        for dl in dls:
+            btemptt=datetime.datetime(*time.strptime(dl[4],"%Y-%m-%d %H:%M:%S")[:-3])
+            etemptt=datetime.datetime(*time.strptime(dl[5],"%Y-%m-%d %H:%M:%S")[:-3])
+            if btemp>btemptt:
+                btemp=btemptt
+                etemp=etemptt
+            elif btemp==btemptt:
+                if etemp>etemptt:
+                    etemp=etemptt
+        bt2=btemp.strftime("%Y-%m-%d %H:%M:%S")
+        et2=etemp.strftime("%Y-%m-%d %H:%M:%S")
+        ret2=rectime_count(bt2,et2,dls)
+        if retcount[0]<ret2[0][0]:
+            retcount[0]=ret2[0][0]
+        if retcount[1]<ret2[0][1]:
+            retcount[1]=ret2[0][1]
+        dls=ret2[1]
+        if len(dis)==0:
+            break
+        elif dis[0][4]==btime:
+            break
+    return retcount
 def change_reserv(id,type,keyword,chtxt,btime,etime,deltatime,opt,deltaday):
     """
     idはテキスト
     """
     MaxT=2
     MaxS=2
-    db=sqlite3.connect(dbpath)
-    bt="\'"+btime+"\'"
-    et="\'"+etime+"\'"
-    idn=int(id)
-    dbexe="SELECT id,type,chdata.bctype,rectime.chtxt,rectime.title FROM rectime INNER JOIN chdata ON rectime.chtxt=chdata.chtxt"
-    #dbexe="SELECT bctype,chtxt,title FROM rectime INNER JOIN chdata ON rectime.chtxt=chdata.chtxt"
-
-    dbexe=dbexe+" WHERE NOT( ( rectime.etime <= "+bt+" ) OR ( rectime.btime >= "+et+" ) )"
-    for idt,typet,bctypet, chtxtt, titlet in db.execute(dbexe):
-        if idt != idn:
-            if (typet=="rec") or (typet=="res") or (typet=="key") or (typet=="keyevery"):
-                if bctypet.find("cs")>-1:
-                    MaxS=MaxS-1
-                elif bctypet.find("bs")>-1:
-                    MaxS=MaxS-1
-                elif bctypet.find("te")>-1:
-                    MaxT=MaxT-1
+    tsnum=count_schedule(btime, etime)
+    MaxT=MaxT-tsnum[0]
+    MaxS=MaxS-tsnum[1]
     bctype=""
     rett=""
-    for bctypet, chtxtt in db.execute("SELECT bctype,chtxt FROM chdata WHERE chtxt=\'"+chtxt+"\'"):
-        bctype=bctypet
+    for dl in database.select_by_chtxt_chdata(chtxt):
+        bctype=dl[0]
     if (type=="rec") or (type=="res") or (type=="key") or (type=="keyevery"):
         if bctype.find("cs")>-1:
             MaxS=MaxS-1
@@ -166,28 +223,20 @@ def change_reserv(id,type,keyword,chtxt,btime,etime,deltatime,opt,deltaday):
         elif bctype.find("te")>-1:
             MaxT=MaxT-1
     if MaxS>-1 and MaxT>-1:
-        db.execute('delete from rectime where id='+id)
-        recline="\""+type+"\",\""+chtxt+"\",\""+keyword+"\",datetime(\""+btime+"\"),datetime(\""+etime+"\"),\""+deltatime+"\",\""+opt+"\",\""+deltaday+"\""
-        #print recline
-        db.execute('insert into rectime ("type","chtxt","title","btime","etime","deltatime","opt","deltaday") values ('+recline+')')
-        db.commit()
+        database.del_by_id_rectime(id)
+        database.add_rectime(type=type, chtxt=chtxt, title=keyword, btime=btime, etime=etime, deltatime=deltatime, deltaday=deltaday, opt=opt)
     else:
         rett="over"
-    db.close()
     return rett
 def station2chtxt(station):
-    db=sqlite3.connect(dbpath)
     ret=""
-    for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE station=\""+station+"\""):
+    for bctype, ontv, chtxt, ch, csch ,updatetime in database.select_by_station_chdata(station):
         ret=chtxt
-    db.close()
     return ret
 def station_name2chtxt(station):
-    db=sqlite3.connect(dbpath)
     ret=""
-    for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE station_name=\""+station+"\""):
+    for bctype, ontv, chtxt, ch, csch ,updatetime in database.select_by_station_name_chdata(station):
         ret=chtxt
-    db.close()
     return ret
 def readHtmlSrcSimple():
     f = open(path+'rswi.htm')
@@ -215,6 +264,8 @@ def readRecSchedule():
     ret=f.read()
     f.close()
     return ret
+dbpath=path+"ch.db"
+database=getdb()
 f=cgi.FieldStorage()
 
 btime=""
@@ -243,10 +294,12 @@ if f.getfirst('chtxt'):
     chtxt=f.getfirst('chtxt')
 if f.getfirst('btime'):
     btime=f.getfirst('btime')
-    bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
+    #bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
+    bt=datetime.datetime(*time.strptime(btime,"%Y-%m-%d %H:%M:%S")[:-3])
 if f.getfirst('etime'):
     etime=f.getfirst('etime')
-    et=datetime.datetime.strptime(etime,"%Y-%m-%d %H:%M:%S")
+    #et=datetime.datetime.strptime(etime,"%Y-%m-%d %H:%M:%S")
+    et=datetime.datetime(*time.strptime(etime,"%Y-%m-%d %H:%M:%S")[:-3])
     if bt !="":
         delt=et-bt
         dt=delt.days*24*60*60+delt.seconds
@@ -260,7 +313,6 @@ if f.getfirst('etime'):
             dd=d2-d1
             et=et+dd
             etime=et.strftime("%Y-%m-%d %H:%M:%S")
-        
 size="S"
 if f.getfirst('size'):
     size=f.getfirst('size')
@@ -276,8 +328,6 @@ if f.getfirst('deltaday',""):
 exect=""
 if f.getfirst('exec',""):
     exect=f.getfirst('exec',"")
-
-
 htmdate= readHtmlSrcSimple()
 htmdate=htmdate.replace("<!--text_for_replace_chtxt_input//-->",getchtxt())
 htmdate=htmdate.replace("<!--btime-value//-->",btime)
@@ -287,8 +337,6 @@ htmdate=htmdate.replace("<!--ch-value//-->",chtxt)
 htmdate=htmdate.replace("<!--title-value//-->",title)
 htmdate=htmdate.replace("<!--size-value//-->",size)
 htmdate=htmdate.replace("<!--opts-value//-->",opts)
-
-
 #ここから曜日検索の設定
 if f.getfirst('type')=="keyday":
     rett=""
@@ -313,16 +361,12 @@ if f.getfirst('type')=="keyday":
     dayt=f.getfirst('day_Sun')
     if dayt:
         rett=rett+reserv_recday(title, chtxt, btime, etime, deltahour, opts, deltaday, dayt)
-
     if rett=="":
         htmdate=readRecFinisSimple()
     else:
         htmdate=readRecFinisOver()
     chtxt=""
     exect=""
-
-    
-
 if (chtxt != "")and(title != "")and(btime!="")and(etime!="")and(opts!="")and(exect=="yes"):
     restxt=reserv(type, title, chtxt, btime, etime, deltahour, opts, deltaday)
     if restxt=="":
@@ -333,6 +377,7 @@ if (chtxt != "")and(title != "")and(btime!="")and(etime!="")and(opts!="")and(exe
 #
 if (chtxt != "")and(title != "")and(btime!="")and(etime!="")and(opts!="")and(re.search('edit-exec:',exect)):
     editnum=exect.replace("edit-exec:","")
+    #restxt=change_reserv(id, type, keyword, chtxt, btime, etime, deltatime, opt, deltaday)
     restxt=change_reserv(editnum,type, title, chtxt, btime, etime, deltahour, opts, deltaday)
     if restxt=="":
         htmdate=readRecFinisSimple()
@@ -346,12 +391,12 @@ if re.search('edit:',exect):
     htmdate= readHtmlEdit()
     iddata=edit_date(editnum)
     htmdate=htmdate.replace("<!--text_for_replace_chtxt_input//-->",getchtxt())
-    htmdate=htmdate.replace("<!--ch-value//-->",iddata["chtxt"])
-    htmdate=htmdate.replace("<!--btime-value//-->",iddata["btime"])
-    htmdate=htmdate.replace("<!--etime-value//-->",iddata["etime"])
-    htmdate=htmdate.replace("<!--type-value//-->",iddata["type"])
-    htmdate=htmdate.replace("<!--ch-value//-->",iddata["chtxt"])
-    htmdate=htmdate.replace("<!--title-value//-->",iddata["title"])
+    htmdate=htmdate.replace("<!--ch-value//-->",iddata['chtxt'])
+    htmdate=htmdate.replace("<!--btime-value//-->",iddata['btime'])
+    htmdate=htmdate.replace("<!--etime-value//-->",iddata['etime'])
+    htmdate=htmdate.replace("<!--type-value//-->",iddata['type'])
+    htmdate=htmdate.replace("<!--ch-value//-->",iddata['chtxt'])
+    htmdate=htmdate.replace("<!--title-value//-->",iddata['title'])
     htmdate=htmdate.replace("<!--size-value//-->",size)
     htmdate=htmdate.replace("<!--opts-value//-->",iddata["opt"])
     htmdate=htmdate.replace("<!--exec-value//-->","edit-exec:"+editnum)