OSDN Git Service

implement iepg data adding on web.
[rec10/rec10-git.git] / Rec10WEB / trunk / src / rec10web.py
index 45a2059..1edceeb 100644 (file)
@@ -3,7 +3,7 @@
 # Rec10 TS Recording Tools
 # Copyright (C) 2009 Yukikaze
 import cgi
-#import cgitb;cgitb.enable()
+import cgitb;cgitb.enable()
 import rec10web_dbMySQL
 import rec10web_dbSQLite
 import os
@@ -12,17 +12,19 @@ import datetime
 import rec10web_dbSQLite
 import ConfigParser
 import time
+
 path=str(os.path.dirname(os.path.abspath(__file__)))+"/"
 confp=ConfigParser.SafeConfigParser()
 Conf='config.ini'
 confp.read(path+Conf)
+global f
+f=cgi.FieldStorage()
 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':
@@ -95,7 +97,6 @@ def getchtxt():
 def edit_date(id):
     ret=database.select_by_id_rectime(id)
     return ret[0]
-
 def reserv_recday(keyword,chtxt,btime,etime,deltatime,opt,deltaday,day):
     """
     dayは月曜日を1日曜日を7とした数字
@@ -115,7 +116,7 @@ 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
-    tsnum=database.count_schedule_rectime(btime,etime)
+    tsnum=count_schedule(btime,etime)
     MaxT=MaxT-tsnum[0]
     MaxS=MaxS-tsnum[1]
     bctype=""
@@ -140,8 +141,10 @@ def count_schedule(btime,etime):
     return rec num (int return[0]:TE /int return[1]:BS/CS)
     """
     def cmpare(x,y):
-        xt=datetime.datetime(*time.strptime(x,"%Y-%m-%d %H:%M:%S")[:-3])
-        yt=datetime.datetime(*time.strptime(y,"%Y-%m-%d %H:%M:%S")[:-3])
+        #xt=datetime.datetime(*time.strptime(x,"%Y-%m-%d %H:%M:%S")[:-3])
+        #yt=datetime.datetime(*time.strptime(y,"%Y-%m-%d %H:%M:%S")[:-3])
+        xt=x
+        yt=y
         if xt>yt:
             return -1
         elif xt==yt:
@@ -151,83 +154,35 @@ def count_schedule(btime,etime):
     dls=database.schedule_rectime(btime,etime)
     print dls
     times=[]
+    btd=datetime.datetime(*time.strptime(btime,"%Y-%m-%d %H:%M:%S")[:-3])
+    etd=datetime.datetime(*time.strptime(etime,"%Y-%m-%d %H:%M:%S")[:-3])
     for dl in dls:
-        times.append(datetime.datetime(*time.strptime(dl[4],"%Y-%m-%d %H:%M:%S")[:-3]))
-        times.append(datetime.datetime(*time.strptime(dl[5],"%Y-%m-%d %H:%M:%S")[:-3]))
+        dd1=datetime.datetime(*time.strptime(dl[4],"%Y-%m-%d %H:%M:%S")[:-3])
+        dd2=datetime.datetime(*time.strptime(dl[5],"%Y-%m-%d %H:%M:%S")[:-3])
+        if dd1<btd:
+            dd1=btd
+        if dd2>etd:
+            dd2=etd
+        times.append(dd1)
+        times.append(dd2)
     print times
     times=list(set(times))
     print times
-    times=times.sort()
+    times.sort(cmpare)
+    times.reverse()
     print times
-    #
-    """
-    #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")
-    dis=database.schedule_rectime(btime,etime)
-    print dis
-    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]
-    retend=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 dis:
-            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,dis)
-        print bt2+":"+et2
-        print ret2[0]
-        if retcount[0]<ret2[0][0]:
-            retcount[0]=ret2[0][0]
-        if retcount[1]<ret2[0][1]:
-            retcount[1]=ret2[0][1]
-        dis=ret2[1]
-        if len(dis)==0:
-            break
-        elif dis[0][4]==btime:
-            break
-    return retcount"""
+    for i in xrange(0, len(times)-2, 1):
+        bt1=times[i]
+        et1=times[i+1]
+        btime1=bt1.strftime("%Y-%m-%d %H:%M:%S")
+        etime1=et1.strftime("%Y-%m-%d %H:%M:%S")
+        ret1=database.count_schedule_rectime(btime1,etime1)
+        if retcount[0]<ret1[0]:
+            retcount[0]=ret1[0]
+        if retcount[1]<ret1[1]:
+            retcount[1]=ret1[1]
+    return retcount
 def change_reserv(id,type,keyword,chtxt,btime,etime,deltatime,opt,deltaday):
     """
     idはテキスト
@@ -256,13 +211,11 @@ def change_reserv(id,type,keyword,chtxt,btime,etime,deltatime,opt,deltaday):
     return rett
 def station2chtxt(station):
     ret=""
-    for bctype, ontv, chtxt, ch, csch ,updatetime in database.select_by_station_chdata(station):
-        ret=chtxt
-    return ret
-def station_name2chtxt(station):
-    ret=""
-    for bctype, ontv, chtxt, ch, csch ,updatetime in database.select_by_station_name_chdata(station):
-        ret=chtxt
+    sta=database.select_by_station_iepg(station):
+    if len(sta)>0:
+        ret=sta[0][1]
+    else:
+        ret="n\/a"
     return ret
 def readHtmlSrcSimple():
     f = open(path+'rswi.htm')
@@ -290,27 +243,83 @@ def readRecSchedule():
     ret=f.read()
     f.close()
     return ret
+def readiEPGadd():
+    f = open(path+'iepg_add.htm')
+    ret=f.read()
+    f.close()
+    return ret
+def readiEPGfin():
+    f = open(path+'iepg_fin.htm')
+    ret=f.read()
+    f.close()
+    return ret
+def readiEPGshow():
+    f = open(path+'iepg_show.htm')
+    ret=f.read()
+    f.close()
+    return ret
+def mode_iepg_add(station):
+    htmdate=readiEPGadd()
+    htmdate=htmdate.replace("<!--text_for_replace_chtxt_input//-->",getchtxt())
+    htmdate=htmdate.replace("<!--ch-value//-->","")
+    htmdate=htmdate.replace("<!--iepg_title-value//-->",station)
+    return htmdate
+def mode_iepg_fin():
+    htmdate=readiEPGfin()
+    return htmdate
+def mode_iepg_show():
+    htmdate=readiEPGshow()
+    lsts=database.select_all_iepg()
+    s=""
+    for lst in lsts:
+        s=s+"{name:\""+lst[0]+"\",chtxt:\""+lst[1]+"\"},\n"
+    return htmdate
+def mode_schedule():
+    htmdate=readRecSchedule()
+    return htmdate
+def mode_edit():
+    editnum=exect.replace("edit:","")
+    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("<!--size-value//-->",size)
+    htmdate=htmdate.replace("<!--opts-value//-->",iddata["opt"])
+    htmdate=htmdate.replace("<!--exec-value//-->","edit-exec:"+editnum)
+    return htmdate
 dbpath=path+"ch.db"
 database=getdb()
-f=cgi.FieldStorage()
-
 btime=""
 etime=""
 title=""
 chtxt=""
 type="res"
 bt=""
+station_name=""
+mode=""
 getRecDate()
 if f.getfirst('mode')=="schedule":
     getRecDate()
-
 ###ここからiEPG用の読み出し
 if f.getfirst('station_name'):
     station=f.getfirst('station_name')
-    chtxt=station_name2chtxt(station)
+    chtxt=station2chtxt(station)
+    if chtxt=="n\/a":
+        mode="iepg-add"
+        station_name=station
+        exect="iepg-add"
 if f.getfirst('station'):
     station=f.getfirst('station')
     chtxt=station2chtxt(station)
+    if chtxt=="n\/a":
+        mode="iepg-add"
+        station_name=station
+        exect="iepg-add"
 ###ここから外部からの読み出しの場合
 if f.getfirst('type'):
     type=f.getfirst('type')
@@ -320,11 +329,9 @@ 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(*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(*time.strptime(etime,"%Y-%m-%d %H:%M:%S")[:-3])
     if bt !="":
         delt=et-bt
@@ -354,6 +361,16 @@ if f.getfirst('deltaday',""):
 exect=""
 if f.getfirst('exec',""):
     exect=f.getfirst('exec',"")
+if exect=="schedule":
+    mode="schedule"
+elif re.search('edit:',exect):
+    mode="edit"
+elif exect=="iepg-add":
+    mode="iepg-add"
+elif exect=="iepg-show":
+    mode="iepg-show"
+elif exect=="iepg-add_yes"
+    mode="iepg-add-yes"
 htmdate= readHtmlSrcSimple()
 htmdate=htmdate.replace("<!--text_for_replace_chtxt_input//-->",getchtxt())
 htmdate=htmdate.replace("<!--btime-value//-->",btime)
@@ -400,7 +417,6 @@ if (chtxt != "")and(title != "")and(btime!="")and(etime!="")and(opts!="")and(exe
     elif restxt=="over":
         htmdate=readRecFinisOver()
     exect=""
-#
 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)
@@ -410,22 +426,19 @@ if (chtxt != "")and(title != "")and(btime!="")and(etime!="")and(opts!="")and(re.
     elif restxt=="over":
         htmdate=readRecFinisOver()
     exect=""
-if exect=="schedule":
-    htmdate=readRecSchedule()
-if re.search('edit:',exect):
-    editnum=exect.replace("edit:","")
-    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("<!--size-value//-->",size)
-    htmdate=htmdate.replace("<!--opts-value//-->",iddata["opt"])
-    htmdate=htmdate.replace("<!--exec-value//-->","edit-exec:"+editnum)
+if mode=="schedule":
+    htmdate=mode_normal()
+elif mode=="edit":
+    htmdate=mode_edit()
+elif mode=="iepg-add":
+    htmdate=mode_iepg_add(f.getfirst('iepg_station'))
+elif mode=="iepg-add-yes":
+    if f.getfirst('iepg_station') and f.getfirst('iepg_chtxt'):
+        database.add_iepg(f.getfirst('iepg_station'),f.getfirst('iepg_chtxt'))
+    htmdate=mode_iepg_fin()
+elif mode=="iepg-show":
+    htmdate=mode_iepg_show()
 print "Content-Type: text/html\n"
 print htmdate
 
+