OSDN Git Service

fix many encoding errors
[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
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.getpath("tmp")+"/"
21 if tmppath=="/":
22     tmppath=path
23 def update(chtxt):
24     recdblist.printutf8(chtxt)
25     tnow = datetime.datetime.now()
26     try:
27         lastup = datetime.datetime.strptime(chdb.chtxtsearch(chtxt)[0]['update'], "%Y-%m-%d %H:%M:%S")
28     except:
29         lastup = datetime.datetime.strptime("2009-04-01 00:00:00", "%Y-%m-%d %H:%M:%S")
30     dt = tnow-lastup
31     dt = dt.days * 24 * 60 + dt.seconds
32     if dt > 2 * 60 * 60:
33         ts2epg.write(os.path.join(tmppath, chtxt + "epgdata.xml"), chdb.chtxtsearch(chtxt)['ch'])
34         xml2db_dom.xml2db_dom(os.path.join(tmppath, chtxt + "epgdata.xml"), chdb.chtxtsearch(chtxt)['bctype'])
35 def updatebc(bctype):
36     bctypel = chdb.bctypesearch(bctype)
37     print bctype
38     try:
39         ts2epg.write(tmppath + bctype + "epgdata.xml", chdb.bctypesearch(bctype)['ch'])
40         xml2db_dom.xml2db_dom(tmppath + bctype + "epgdata.xml", bctype)
41         shutil.copyfile(tmppath + bctype + "epgdata.xml", tmppath + bctype + "epgdata.bak.xml")
42         time.sleep(5)
43         rec10d.rec10db.update_by_bctype_epg_ch(bctype)
44     except Exception, inst:
45         print "error occurs in updatebc(epgdb)"
46         print type(inst)
47         print inst
48         xml2db_dom.xml2db_dom(tmppath + bctype + "epgdata.bak.xml", bctype)
49         rec10d.rec10db.update_status_by_bctype_epg_ch(bctype, "2")
50 def updatebc_bak(bctype):
51     """
52     update epg data by .bak file
53     """
54     xml2db_dom.xml2db_dom(tmppath + bctype + "epgdata.bak.xml", bctype)
55 def searchtime2(titletxt, timet, deltatime, chtxt):
56     time = datetime.datetime.strptime(timet, "%Y-%m-%d %H:%M:%S")
57     ret = []
58     deltatime = int(deltatime)
59     beforetime = time.strftime("%Y%m%d%H%M%S")
60     aftertime = time + datetime.timedelta(hours=deltatime)
61     aftertime = aftertime.strftime("%Y%m%d%H%M%S")
62     one = ["", "", "", "","","",""]
63     tempt = 0
64     d = rec10d.rec10db.select_by_time_ngram_epg_timeline(beforetime, aftertime, chtxt)
65     for chtxtt, title, start, stop ,exp,longexp,category in d:
66         #recdblist.printutf8(chtxtt+":"+title+":"+start+":"+stop)
67         p = n_gram.bigram(titletxt, title)
68         btime = start
69         btime = btime[0:4] + "-" + btime[4:6] + "-" + btime[6:8] + " " + btime[8:10] + ":" + btime[10:12] + ":00"
70         etime = stop
71         etime = etime[0:4] + "-" + etime[4:6] + "-" + etime[6:8] + " " + etime[8:10] + ":" + etime[10:12] + ":00"
72         bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
73         dt = bt-time
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     ret = one
85     print ret
86     return ret
87 def searchtime_auto(keytxt, timet, deltatime):
88     time = datetime.datetime.strptime(timet, "%Y-%m-%d %H:%M:%S")
89     ret = []
90     deltatime = int(deltatime)
91     beforetime = time.strftime("%Y%m%d%H%M%S")
92     aftertime = time + datetime.timedelta(hours=deltatime)
93     aftertime = aftertime.strftime("%Y%m%d%H%M%S")
94     d = rec10d.rec10db.select_by_time_keyword_auto_suggest_epg_timeline(keytxt,beforetime, aftertime)
95     for chtxtt, title, start, stop ,exp,longexp,category in d:
96         btime = start
97         btime = btime[0:4] + "-" + btime[4:6] + "-" + btime[6:8] + " " + btime[8:10] + ":" + btime[10:12] + ":00"
98         etime = stop
99         etime = etime[0:4] + "-" + etime[4:6] + "-" + etime[6:8] + " " + etime[8:10] + ":" + etime[10:12] + ":00"
100         ret.append([chtxtt, title, btime, etime,exp,longexp,category])
101     return ret
102 def count_schedule(btime,etime):
103     """
104     return rec num (int return[0]:TE /int return[1]:BS/CS)
105     """
106     def cmpare(x, y):
107         xt = x
108         yt = y
109         if xt > yt:
110             return -1
111         elif xt == yt:
112             return 0
113         else:
114             return 1
115     dls = rec10d.rec10db.select_bytime_all_timeline(btime, etime)
116     times = []
117     btd = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
118     etd = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
119     for dl in dls:
120         dd1 = datetime.datetime.strptime(dl['btime'], "%Y-%m-%d %H:%M:%S")
121         dd2 = datetime.datetime.strptime(dl['etime'], "%Y-%m-%d %H:%M:%S")
122         if dd1 < btd:
123             dd1 = btd
124         if dd2 > etd:
125             dd2 = etd
126         times.append(dd1)
127         times.append(dd2)
128     times = list(set(times))
129     times.sort(cmpare)
130     times.reverse()
131     retcount = [0, 0]
132     for i in xrange(0, len(times)-2, 1):
133         bt1 = times[i]
134         et1 = times[i + 1]
135         btime1 = bt1.strftime("%Y-%m-%d %H:%M:%S")
136         etime1 = et1.strftime("%Y-%m-%d %H:%M:%S")
137         ret1 = rec10d.rec10db.count_schedule_timeline(btime1, etime1)
138         if retcount[0] < ret1[0]:
139             retcount[0] = ret1[0]
140         if retcount[1] < ret1[1]:
141             retcount[1] = ret1[1]
142     return retcount