OSDN Git Service

implement personal epg updating(rc1).
[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",chdb.chtxtsearch(chtxt)['bctype'])
27 #def update_bak(chtxt):
28 #    """
29 #    update epgdata by .bak file in chtxt channnel
30 #    """
31
32 def updatebc(bctype):
33     bctypel=chdb.bctypesearch(bctype)
34     print bctypel
35     try:
36         ts2epg.write(tmppath+bctype+"epgdate.xml",chdb.bctypesearch(bctype)['ch'])
37         xml2db.xml2db(tmppath+bctype+"epgdate.xml",bctype)
38         shutil.copyfile(tmppath+bctype+"epgdate.xml", tmppath+bctype+"epgdate.bak.xml")
39         rec10d.rec10db.update_by_bctype_chdata(bctype)
40     except Exception,inst:
41         print type(inst)
42         print inst
43         xml2db.xml2db(tmppath+bctype+"epgdate.bak.xml",bctype)
44         if rec10d.rec10db.select_by_bctype_chdata(bctype)[0][6]!="0":
45             rec10d.rec10db.update_status_by_bctype_chdata(bctype,"2")
46 def updatebc_bak(bctype):
47     """
48     update epg data by .bak file
49     """
50     xml2db.xml2db(tmppath+bctype+"epgdate.bak.xml",bctype)
51 def updateall_bak():
52     updatebc_bak("te13")
53     updatebc_bak("te20")
54     updatebc_bak("te22")
55     updatebc_bak("te18")
56     updatebc_bak("te19")
57     updatebc_bak("te21")
58     updatebc_bak("te23")
59     updatebc_bak("cs1")
60     updatebc_bak("cs2")
61     updatebc_bak("bs")
62 def updateall():
63     updatebc("te13")
64     updatebc("te20")
65     updatebc("te22")
66     updatebc("te18")
67     updatebc("te19")
68     updatebc("te21")
69     updatebc("te23")
70     updatebc("cs1")
71     updatebc("cs2")
72     updatebc("bs")
73 #def search(titletxt):
74 ##   return rec10d.rec10db.select_by_title_tv(titletxt)
75 def searchtime2(titletxt,timet,deltatime,chtxt):
76     time=datetime.datetime.strptime(timet,"%Y-%m-%d %H:%M:%S")
77     ret=[]
78     deltatime=int(deltatime)
79     beforetime=time.strftime("%Y%m%d%H%M%S")
80     aftertime=time+datetime.timedelta(hours=deltatime)
81     aftertime=aftertime.strftime("%Y%m%d%H%M%S")
82     one=["","","",""]
83     tempt=0
84     d=rec10d.rec10db.select_by_time_ngram_tv(beforetime,aftertime,chtxt)
85     for chtxtt, title, start, stop in d:
86         #print chtxtt+":"+title+":"+start+":"+stop
87         p=n_gram.bigram(titletxt,title)
88         btime=start
89         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
90         etime=stop
91         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
92         bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
93         dt=bt-time
94         dt=24*60*60*dt.days+dt.seconds+1
95         p=p*90
96         dt=1000-1000*dt/(7*24*60*60)
97         if p>0:
98             p=p+dt
99             print dt
100         if p>tempt:
101             one=[chtxtt,title.encode('utf-8'),btime,etime]
102             tempt=p
103         if p>0:
104             print title+":"+str(p)+"点"
105     ret=one
106     print ret
107     return ret