OSDN Git Service

put DB accessing functions together(dbSQLite.py).
[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 rec10d
7 import chdb
8 import datetime
9 import ts2epg
10 import xml2db
11 import n_gram
12 path=str(os.path.dirname(os.path.abspath(__file__)))+"/"
13 tmppath=path
14 def update(chtxt):
15     print chtxt
16     tnow=datetime.datetime.now()
17     try:
18         lastup=datetime.datetime.strptime(chdb.chtxtsearch(chtxt)[0]['update'],"%Y-%m-%d %H:%M:%S")
19     except:
20         lastup=datetime.datetime.strptime("2009-04-01 00:00:00","%Y-%m-%d %H:%M:%S")
21     dt=tnow-lastup
22     dt=dt.days*24*60+dt.seconds
23     if dt>2*60*60:
24         ts2epg.write(tmppath+"epgdate.xml",chdb.chtxtsearch(chtxt)['ch'])
25         xml2db.xml2db(tmppath+"epgdate.xml",path+"ch.db",chdb.chtxtsearch(chtxt)['bctype'])
26 def updatebc(bctype):
27     bctypel=chdb.bctypesearch(bctype)
28     print bctypel
29     i=1
30     while i<5:
31         try:
32             ts2epg.write(tmppath+"epgdate.xml",chdb.bctypesearch(bctype)['ch'])
33             xml2db.xml2db(tmppath+"epgdate.xml",path+"ch.db",bctype)
34             break
35         except Exception,inst:
36             print type(inst)
37             print inst
38             i=i+1
39 def updateall():
40     update("hisch")
41     update("nhk-k")
42     update("nhk-s")
43     update("me-tere")
44     update("cbc")
45     update("aichi")
46     update("toukai")
47     update("chukyo")
48     update("bs-nhk-1")
49     
50 def search(titletxt):
51     return rec10d.rec10db.select_by_title_tv(titletxt)
52 def searchtime2(titletxt,timet,deltatime,chtxt):
53     time=datetime.datetime.strptime(timet,"%Y-%m-%d %H:%M:%S")
54     ret=[]
55     deltatime=int(deltatime)
56     beforetime=time.strftime("%Y%m%d%H%M%S")
57     aftertime=time+datetime.timedelta(hours=deltatime)
58     aftertime=aftertime.strftime("%Y%m%d%H%M%S")
59     one={}
60     tempt=0
61     d=rec10d.rec10db.select_by_time_ngram_tv(beforetime,aftertime,chtxt)
62     print d
63     for chtxtt, title, start, stop in d:
64         print chtxtt+":"+title+":"+start+":"+stop
65         ret=[]
66         p=n_gram.bigram(titletxt,title)
67         btime=start
68         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
69         etime=stop
70         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
71         bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
72         et=datetime.datetime.strptime(etime,"%Y-%m-%d %H:%M:%S")
73         dt=bt-time
74         dt=24*60*60*dt.days+dt.seconds+1
75         p=p*90
76         dt=1000-1000*dt/(7*24*60*60)
77         if p>0:
78             p=p+dt
79             print dt
80         if p>tempt:
81             one=chtxtt+","+title.encode('utf-8')+","+btime+","+etime
82             tempt=p
83         if p>0:
84             print title+":"+str(p)+"点"
85     ret.append(one)
86     print ret
87     return ret