OSDN Git Service

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