OSDN Git Service

the first release of 0.5
[rec10/rec10-git.git] / rec10 / branches / 0.5 / src / epgdb.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 import os
6 import time
7 import sqlite3
8 import chdb
9 import datetime
10 import ts2epg
11 import xml2db
12 import n_gram
13 path=str(os.path.dirname(os.path.abspath(__file__)))+"/"
14 tmppath=path
15 def update(chtxt):
16     print chtxt
17     tnow=datetime.datetime.now()
18     try:
19         lastup=datetime.datetime.strptime(chdb.chtxtsearch(chtxt)[0]['update'],"%Y-%m-%d %H:%M:%S")
20     except:
21         lastup=datetime.datetime.strptime("2009-04-01 00:00:00","%Y-%m-%d %H:%M:%S")
22     dt=tnow-lastup
23     dt=dt.days*24*60+dt.seconds
24     if dt>2*60*60:
25         ts2epg.write(tmppath+"epgdate.xml",chdb.chtxtsearch(chtxt)['ch'])
26         xml2db.xml2db(tmppath+"epgdate.xml",path+"ch.db",chdb.chtxtsearch(chtxt)['bctype'])
27 def updatebc(bctype):
28     bctypel=chdb.bctypesearch(bctype)
29     print bctypel
30     i=1
31     while i<5:
32         try:
33             ts2epg.write(tmppath+"epgdate.xml",chdb.bctypesearch(bctype)['ch'])
34             xml2db.xml2db(tmppath+"epgdate.xml",path+"ch.db",bctype)
35             break
36         except:
37             i=i+1
38 def updateall():
39     update("hisch")
40     update("nhk-k")
41     update("nhk-s")
42     update("me-tere")
43     update("cbc")
44     update("aichi")
45     update("toukai")
46     update("chukyo")
47     update("bs-nhk-1")
48     
49 def search(titletxt):
50     dbpath=path+"ch.db"
51     db=sqlite3.connect(dbpath)
52     #print dbpath
53     ret=[]
54     for ch, title, start, stop in db.execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\""):
55         btime=start.replace(" +0900","")
56         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
57         etime=stop.replace(" +0900","")
58         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
59         try:
60             chtxt=chdb.ontvsearch(ch)['chtxt']
61         except:
62             chtxt="Unknown"
63             #print ch
64         ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)
65     db.close()
66     return ret
67 def searchtime(titletxt,time,deltatime):#時間以内のものだけを表示 deltatimeはhours
68     dbpath=path+"ch.db"
69     db=sqlite3.connect(dbpath)
70     #print dbpath
71     ret=[]
72     deltatime=int(deltatime)
73     for ch, title, start, stop in db.execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\""):
74         btime=start.replace(" +0900","")
75         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
76         etime=stop.replace(" +0900","")
77         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
78         bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
79         t=datetime.datetime.strptime(time,"%Y-%m-%d %H:%M:%S")
80         try:
81             chtxt=chdb.ontvsearch(ch)['chtxt']
82         except:
83             chtxt="Unknown"
84             #print ch
85         dt=bt-t
86         dt=dt.days*24*60*60+dt.seconds
87         dt=abs(dt)
88         if dt<deltatime*60*60:
89             ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)
90             print title
91     db.close()
92     return ret
93 def searchtime2(titletxt,timet,deltatime,chtxt):
94     time=datetime.datetime.strptime(timet,"%Y-%m-%d %H:%M:%S")
95     dbpath=path+"ch.db"
96     db=sqlite3.connect(dbpath)
97     #print dbpath
98     ret=[]
99     deltatime=int(deltatime)
100     beforetime=time.strftime("%Y%m%d%H%M%S")
101     aftertime=time+datetime.timedelta(hours=deltatime)
102     aftertime=aftertime.strftime("%Y%m%d%H%M%S")
103     dbexe="SELECT chdata.chtxt,title,start,stop FROM tv INNER JOIN chdata WHERE chdata.ontv=tv.channel AND start >= \""+beforetime+"\" AND start <= \""+aftertime+"\" AND chdata.chtxt=\""+chtxt+"\""
104     #print dbexe
105     one={}
106     tempt=0
107     for chtxtt, title, start, stop in db.execute(dbexe):
108         ret=[]
109         p=n_gram.bigram(titletxt,title)
110         btime=start
111         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
112         etime=stop
113         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
114         bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
115         et=datetime.datetime.strptime(etime,"%Y-%m-%d %H:%M:%S")
116         dt=bt-time
117         dt=24*60*60*dt.days+dt.seconds+1
118         p=p*90
119         dt=1000-1000*dt/(7*24*60*60)
120         if p>0:
121             p=p+dt
122             print dt
123         if p>tempt:
124             one=chtxtt+","+title.encode('utf-8')+","+btime+","+etime
125             tempt=p
126         if p>0:
127             print title+":"+str(p)+"点"
128     db.close()
129     ret.append(one)
130     print ret
131     return ret