OSDN Git Service

fix epg refreshing bugs.(use xml which used before with no error)
[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 import shutil
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+chtxt+"epgdate.xml",chdb.chtxtsearch(chtxt)['ch'])
26         xml2db.xml2db(tmppath+chtxt+"epgdate.xml",path+"ch.db",chdb.chtxtsearch(chtxt)['bctype'])
27 def updatebc(bctype):
28     bctypel=chdb.bctypesearch(bctype)
29     print bctypel
30     try:
31         ts2epg.write(tmppath+bctype+"epgdate.xml",chdb.bctypesearch(bctype)['ch'])
32         xml2db.xml2db(tmppath+bctype+"epgdate.xml",path+"ch.db",bctype)
33         shutil.copyfile(tmppath+bctype+"epgdate.xml", tmppath+bctype+"epgdate.bak.xml")
34     except Exception,inst:
35         print type(inst)
36         print inst
37         xml2db.xml2db(tmppath+bctype+"epgdate.bak.xml",path+"ch.db",bctype)
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     return rec10d.rec10db.select_by_title_tv(titletxt)
51 def searchtime2(titletxt,timet,deltatime,chtxt):
52     time=datetime.datetime.strptime(timet,"%Y-%m-%d %H:%M:%S")
53     ret=[]
54     deltatime=int(deltatime)
55     beforetime=time.strftime("%Y%m%d%H%M%S")
56     aftertime=time+datetime.timedelta(hours=deltatime)
57     aftertime=aftertime.strftime("%Y%m%d%H%M%S")
58     one=["","","",""]
59     tempt=0
60     d=rec10d.rec10db.select_by_time_ngram_tv(beforetime,aftertime,chtxt)
61     print d
62     for chtxtt, title, start, stop in d:
63         #print chtxtt+":"+title+":"+start+":"+stop
64         p=n_gram.bigram(titletxt,title)
65         btime=start
66         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
67         etime=stop
68         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
69         bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
70         et=datetime.datetime.strptime(etime,"%Y-%m-%d %H:%M:%S")
71         dt=bt-time
72         dt=24*60*60*dt.days+dt.seconds+1
73         p=p*90
74         dt=1000-1000*dt/(7*24*60*60)
75         if p>0:
76             p=p+dt
77             print dt
78         if p>tempt:
79             one=[chtxtt,title.encode('utf-8'),btime,etime]
80             tempt=p
81         if p>0:
82             print title+":"+str(p)+"点"
83     ret=one
84     print ret
85     return ret