OSDN Git Service

use n-gram to search key schedule.
[rec10/rec10-git.git] / rec10 / trunk / 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     ts2epg.write(tmppath+"epgdate.xml",chdb.bctypesearch(bctype)['ch'])
31     xml2db.xml2db(tmppath+"epgdate.xml",path+"ch.db",bctype)
32 def updateall():
33     update("hisch")
34     update("nhk-k")
35     update("nhk-s")
36     update("me-tere")
37     update("cbc")
38     update("aichi")
39     update("toukai")
40     update("chukyo")
41     update("bs-nhk-1")
42     
43 def search(titletxt):
44     dbpath=path+"ch.db"
45     db=sqlite3.connect(dbpath)
46     #print dbpath
47     ret=[]
48     for ch, title, start, stop in db.execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\""):
49         btime=start.replace(" +0900","")
50         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
51         etime=stop.replace(" +0900","")
52         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
53         try:
54             chtxt=chdb.ontvsearch(ch)['chtxt']
55         except:
56             chtxt="Unknown"
57             #print ch
58         ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)
59     db.close()
60     return ret
61 def searchtime(titletxt,time,deltatime):#時間以内のものだけを表示 deltatimeはhours
62     dbpath=path+"ch.db"
63     db=sqlite3.connect(dbpath)
64     #print dbpath
65     ret=[]
66     deltatime=int(deltatime)
67     for ch, title, start, stop in db.execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\""):
68         btime=start.replace(" +0900","")
69         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
70         etime=stop.replace(" +0900","")
71         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
72         bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
73         t=datetime.datetime.strptime(time,"%Y-%m-%d %H:%M:%S")
74         try:
75             chtxt=chdb.ontvsearch(ch)['chtxt']
76         except:
77             chtxt="Unknown"
78             #print ch
79         dt=bt-t
80         dt=dt.days*24*60*60+dt.seconds
81         dt=abs(dt)
82         if dt<deltatime*60*60:
83             ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)
84             print title
85     db.close()
86     return ret
87 def searchtime2(titletxt,timet,deltatime,chtxt):
88     time=datetime.datetime.strptime(timet,"%Y-%m-%d %H:%M:%S")
89     dbpath=path+"ch.db"
90     db=sqlite3.connect(dbpath)
91     #print dbpath
92     ret=[]
93     deltatime=int(deltatime)
94     beforetime=time.strftime("%Y%m%d%H%M%S")
95     aftertime=time+datetime.timedelta(hours=deltatime)
96     aftertime=aftertime.strftime("%Y%m%d%H%M%S")
97     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+"\""
98     #print dbexe
99     one={}
100     tempt=0
101     for chtxtt, title, start, stop in db.execute(dbexe):
102         """btime=start.replace(" +0900","")
103         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
104         etime=stop.replace(" +0900","")
105         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
106         bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
107         t=datetime.datetime.strptime(time,"%Y-%m-%d %H:%M:%S")
108         try:
109             chtxt=chdb.ontvsearch(ch)['chtxt']
110         except:
111             chtxt="Unknown"
112             #print ch
113         dt=bt-t
114         dt=dt.days*24*60*60+dt.seconds
115         dt=abs(dt)
116         if dt<deltatime*60*60:
117             ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)
118
119             """
120         ret=[]
121         p=n_gram.bigram(titletxt,title)
122         btime=start
123         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
124         etime=stop
125         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
126         bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
127         et=datetime.datetime.strptime(etime,"%Y-%m-%d %H:%M:%S")
128         dt=bt-time
129         dt=24*60*60*dt.days+dt.seconds+1
130         p=p*90
131         dt=1000-1000*dt/(7*24*60*60)
132         if p>0:
133             p=p+dt
134             print dt
135         if p>tempt:
136             one=chtxtt+","+title.encode('utf-8')+","+btime+","+etime
137             tempt=p
138         if p>0:
139             print title+":"+str(p)+"点"
140     db.close()
141     ret.append(one)
142     print ret
143     return ret