OSDN Git Service

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