OSDN Git Service

implement new log system.
[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-2010 Yukikaze
5 import datetime
6 import os
7 import shutil
8 import time
9 import os.path
10
11 import chdb
12 import n_gram
13 import rec10d
14 import ts2epg
15 import xml2db_dom
16 import configreader
17 import recdblist
18 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
19 tmppath = configreader.getpath("tmp")+"/"
20 if tmppath=="/":
21     tmppath=path
22 def update(chtxt):
23     recdblist.printutf8(chtxt)
24     tnow = datetime.datetime.now()
25     try:
26         lastup = datetime.datetime.strptime(chdb.chtxtsearch(chtxt)[0]['update'], "%Y-%m-%d %H:%M:%S")
27     except:
28         lastup = datetime.datetime.strptime("2009-04-01 00:00:00", "%Y-%m-%d %H:%M:%S")
29     dt = tnow-lastup
30     dt = dt.days * 24 * 60 + dt.seconds
31     if dt > 2 * 60 * 60:
32         ts2epg.write(os.path.join(tmppath, chtxt + "epgdata.xml"), chdb.chtxtsearch(chtxt)['ch'])
33         xml2db_dom.xml2db_dom(os.path.join(tmppath, chtxt + "epgdata.xml"), chdb.chtxtsearch(chtxt)['bctype'])
34 def updatebc(bctype):
35     bctypel = chdb.bctypesearch(bctype)
36     #print bctype
37     try:
38         ts2epg.write(tmppath + bctype + "epgdata.xml", chdb.bctypesearch(bctype)['ch'])
39         xml2db_dom.xml2db_dom(tmppath + bctype + "epgdata.xml", bctype)
40         shutil.copyfile(tmppath + bctype + "epgdata.xml", tmppath + bctype + "epgdata.bak.xml")
41         time.sleep(5)
42         rec10d.rec10db.update_by_bctype_epg_ch(bctype)
43     except Exception, inst:
44         recdblist.Commonlogex("Error","updatebc(epgdb.py)", str(type(inst)), str(inst))
45         xml2db_dom.xml2db_dom(tmppath + bctype + "epgdata.bak.xml", bctype)
46         rec10d.rec10db.update_status_by_bctype_epg_ch(bctype, "2")
47 def updatebc_bak(bctype):
48     """
49     update epg data by .bak file
50     """
51     xml2db_dom.xml2db_dom(tmppath + bctype + "epgdata.bak.xml", bctype)
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     beforetime = time - datetime.timedelta(hours=deltatime)
58     aftertime = time + datetime.timedelta(hours=deltatime)
59     beforetime = beforetime.strftime("%Y%m%d%H%M%S")
60     aftertime = aftertime.strftime("%Y%m%d%H%M%S")
61     one = ["", "", "", "","","",""]
62     tempt = 0
63     d = rec10d.rec10db.select_by_time_ngram_epg_timeline(beforetime, aftertime, chtxt)
64     for chtxtt, title, start, stop ,exp,longexp,category in d:
65         #recdblist.printutf8(chtxtt+":"+title+":"+start+":"+stop)
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         dt = bt-time
73         dt = abs(dt)
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         if p > tempt:
80             one = [chtxtt, title, btime, etime,exp,longexp,category]
81             tempt = p
82         if p > 0:
83             #recdblist.printutf8(title + ":" + str(p) + u"点")
84             ""
85     ret = one
86     #print ret
87     return ret
88 def searchtime_auto(keytxt, timet, deltatime):
89     time = datetime.datetime.strptime(timet, "%Y-%m-%d %H:%M:%S")
90     ret = []
91     deltatime = int(deltatime)
92     beforetime = time.strftime("%Y%m%d%H%M%S")
93     aftertime = time + datetime.timedelta(hours=deltatime)
94     aftertime = aftertime.strftime("%Y%m%d%H%M%S")
95     d = rec10d.rec10db.select_by_time_keyword_auto_suggest_epg_timeline(keytxt,beforetime, aftertime)
96     for chtxtt, title, start, stop ,exp,longexp,category in d:
97         btime = start
98         btime = btime[0:4] + "-" + btime[4:6] + "-" + btime[6:8] + " " + btime[8:10] + ":" + btime[10:12] + ":00"
99         etime = stop
100         etime = etime[0:4] + "-" + etime[4:6] + "-" + etime[6:8] + " " + etime[8:10] + ":" + etime[10:12] + ":00"
101         ret.append([chtxtt, title, btime, etime,exp,longexp,category])
102     return ret
103 def count_schedule(btime,etime):
104     """
105     return rec num (int return[0]:TE /int return[1]:BS/CS)
106     """
107     def cmpare(x, y):
108         xt = x
109         yt = y
110         if xt > yt:
111             return -1
112         elif xt == yt:
113             return 0
114         else:
115             return 1
116     dls = rec10d.rec10db.select_bytime_all_timeline(btime, etime)
117     times = []
118     btd = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
119     etd = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
120     for dl in dls:
121         dd1 = datetime.datetime.strptime(dl['btime'], "%Y-%m-%d %H:%M:%S")
122         dd2 = datetime.datetime.strptime(dl['etime'], "%Y-%m-%d %H:%M:%S")
123         if dd1 < btd:
124             dd1 = btd
125         if dd2 > etd:
126             dd2 = etd
127         times.append(dd1)
128         times.append(dd2)
129     times = list(set(times))
130     times.sort(cmpare)
131     times.reverse()
132     retcount = [0, 0]
133     for i in xrange(0, len(times)-2, 1):
134         bt1 = times[i]
135         et1 = times[i + 1]
136         btime1 = bt1.strftime("%Y-%m-%d %H:%M:%S")
137         etime1 = et1.strftime("%Y-%m-%d %H:%M:%S")
138         ret1 = rec10d.rec10db.count_schedule_timeline(btime1, etime1)
139         if retcount[0] < ret1[0]:
140             retcount[0] = ret1[0]
141         if retcount[1] < ret1[1]:
142             retcount[1] = ret1[1]
143     return retcount
144
145 def search_schedule(btime,etime):
146     """
147     return rec num (int return[0]:TE /int return[1]:BS/CS)
148     """
149     def cmpare(x, y):
150         xt = x
151         yt = y
152         if xt > yt:
153             return -1
154         elif xt == yt:
155             return 0
156         else:
157             return 1
158     dls = rec10d.rec10db.select_bytime_all_timeline(btime, etime)
159     times = []
160     btd = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
161     etd = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
162     for dl in dls:
163         dd1 = datetime.datetime.strptime(dl['btime'], "%Y-%m-%d %H:%M:%S")
164         dd2 = datetime.datetime.strptime(dl['etime'], "%Y-%m-%d %H:%M:%S")
165         if dd1 < btd:
166             dd1 = btd
167         if dd2 > etd:
168             dd2 = etd
169         times.append(dd1)
170         times.append(dd2)
171     times = list(set(times))
172     times.sort(cmpare)
173     times.reverse()
174     ret=[]
175     for i in xrange(0, len(times)-2, 1):
176         bt1 = times[i]
177         et1 = times[i + 1]
178         btime1 = bt1.strftime("%Y-%m-%d %H:%M:%S")
179         etime1 = et1.strftime("%Y-%m-%d %H:%M:%S")
180         ret1 = rec10d.rec10db.search_schedule_timeline(btime1, etime1)
181         ret.append(ret1)
182     ret = list(set(ret))
183     return ret
184
185 def count_epgschedule(epgbtime,epgetime):
186     """
187     return rec num (int return[0]:TE /int return[1]:BS/CS)
188     """
189     def cmpare(x, y):
190         xt = x
191         yt = y
192         if xt > yt:
193             return -1
194         elif xt == yt:
195             return 0
196         else:
197             return 1
198     dls = rec10d.rec10db.select_byepgtime_over_timeline(epgbtime, epgetime)
199     #print dls
200     times = []
201     btd = datetime.datetime.strptime(epgbtime, "%Y-%m-%d %H:%M:%S")
202     etd = datetime.datetime.strptime(epgetime, "%Y-%m-%d %H:%M:%S")
203     for dl in dls:
204         dd1 = datetime.datetime.strptime(dl['epgbtime'], "%Y-%m-%d %H:%M:%S")
205         dd2 = datetime.datetime.strptime(dl['epgetime'], "%Y-%m-%d %H:%M:%S")
206         if dd1 < btd:
207             dd1 = btd
208         if dd2 > etd:
209             dd2 = etd
210         times.append(dd1)
211         times.append(dd2)
212     times = list(set(times))
213     times.sort(cmpare)
214     times.reverse()
215     retcount = [0, 0]
216     for i in xrange(0, len(times)-1, 1):
217         bt1 = times[i]
218         et1 = times[i + 1]
219         epgbtime1 = bt1.strftime("%Y-%m-%d %H:%M:%S")
220         epgetime1 = et1.strftime("%Y-%m-%d %H:%M:%S")
221         ret1 = rec10d.rec10db.count_epgschedule_timeline(epgbtime1, epgetime1)
222         if retcount[0] < ret1[0]:
223             retcount[0] = ret1[0]
224         if retcount[1] < ret1[1]:
225             retcount[1] = ret1[1]
226     return retcount
227 def search_epgschedule(epgbtime,epgetime):
228     """
229     return rec num (int return[0]:TE /int return[1]:BS/CS)
230     """
231     def cmpare(x, y):
232         xt = x
233         yt = y
234         if xt > yt:
235             return -1
236         elif xt == yt:
237             return 0
238         else:
239             return 1
240     dls = rec10d.rec10db.select_byepgtime_all_timeline(epgbtime, epgetime)
241     times = []
242     btd = datetime.datetime.strptime(epgbtime, "%Y-%m-%d %H:%M:%S")
243     etd = datetime.datetime.strptime(epgetime, "%Y-%m-%d %H:%M:%S")
244     for dl in dls:
245         dd1 = datetime.datetime.strptime(dl['epgbtime'], "%Y-%m-%d %H:%M:%S")
246         dd2 = datetime.datetime.strptime(dl['epgetime'], "%Y-%m-%d %H:%M:%S")
247         if dd1 < btd:
248             dd1 = btd
249         if dd2 > etd:
250             dd2 = etd
251         times.append(dd1)
252         times.append(dd2)
253     times = list(set(times))
254     times.sort(cmpare)
255     times.reverse()
256     ret=[]
257     for i in xrange(0, len(times)-2, 1):
258         bt1 = times[i]
259         et1 = times[i + 1]
260         epgbtime1 = bt1.strftime("%Y-%m-%d %H:%M:%S")
261         epgetime1 = et1.strftime("%Y-%m-%d %H:%M:%S")
262         ret1 = rec10d.rec10db.search_epgschedule_timeline(epgbtime1, epgetime1)
263         ret.append(ret1)
264     ret = list(set(ret))
265     return ret