OSDN Git Service

7479192b805f7b31782f801bc60f3e7a9be7348f
[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 datetime
6 import os
7 import shutil
8 import time
9
10 import chdb
11 import n_gram
12 import rec10d
13 import ts2epg
14 import xml2db_dom
15 import configreader
16 import recdblist
17 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
18 tmppath = configreader.getpath("tmp")+"/"
19 if tmppath=="/":
20     tmppath=path
21 def update(chtxt):
22     recdblist.printutf8(chtxt)
23     tnow = datetime.datetime.now()
24     try:
25         lastup = datetime.datetime.strptime(chdb.chtxtsearch(chtxt)[0]['update'], "%Y-%m-%d %H:%M:%S")
26     except:
27         lastup = datetime.datetime.strptime("2009-04-01 00:00:00", "%Y-%m-%d %H:%M:%S")
28     dt = tnow-lastup
29     dt = dt.days * 24 * 60 + dt.seconds
30     if dt > 2 * 60 * 60:
31         ts2epg.write(tmppath + chtxt + "epgdata.xml", chdb.chtxtsearch(chtxt)['ch'])
32         xml2db_dom.xml2db_dom(tmppath + chtxt + "epgdata.xml", chdb.chtxtsearch(chtxt)['bctype'])
33 def updatebc(bctype):
34     bctypel = chdb.bctypesearch(bctype)
35     print bctype
36     try:
37         ts2epg.write(tmppath + bctype + "epgdata.xml", chdb.bctypesearch(bctype)['ch'])
38         xml2db_dom.xml2db_dom(tmppath + bctype + "epgdata.xml", bctype)
39         shutil.copyfile(tmppath + bctype + "epgdata.xml", tmppath + bctype + "epgdata.bak.xml")
40         time.sleep(5)
41         rec10d.rec10db.update_by_bctype_epg_ch(bctype)
42     except Exception, inst:
43         print "error occurs in updatebc(epgdb)"
44         print type(inst)
45         print inst
46         xml2db_dom.xml2db_dom(tmppath + bctype + "epgdata.bak.xml", bctype)
47         rec10d.rec10db.update_status_by_bctype_epg_ch(bctype, "2")
48 def updatebc_bak(bctype):
49     """
50     update epg data by .bak file
51     """
52     xml2db_dom.xml2db_dom(tmppath + bctype + "epgdata.bak.xml", bctype)
53 def searchtime2(titletxt, timet, deltatime, chtxt):
54     time = datetime.datetime.strptime(timet, "%Y-%m-%d %H:%M:%S")
55     ret = []
56     deltatime = int(deltatime)
57     beforetime = time.strftime("%Y%m%d%H%M%S")
58     aftertime = time + datetime.timedelta(hours=deltatime)
59     aftertime = aftertime.strftime("%Y%m%d%H%M%S")
60     one = ["", "", "", ""]
61     tempt = 0
62     d = rec10d.rec10db.select_by_time_ngram_epg_timeline(beforetime, aftertime, chtxt)
63     for chtxtt, title, start, stop ,exp,longexp,category in d:
64         #recdblist.printutf8(chtxtt+":"+title+":"+start+":"+stop)
65         p = n_gram.bigram(titletxt, title)
66         btime = start
67         btime = btime[0:4] + "-" + btime[4:6] + "-" + btime[6:8] + " " + btime[8:10] + ":" + btime[10:12] + ":00"
68         etime = stop
69         etime = etime[0:4] + "-" + etime[4:6] + "-" + etime[6:8] + " " + etime[8:10] + ":" + etime[10:12] + ":00"
70         bt = datetime.datetime.strptime(btime, "%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         if p > tempt:
78             one = [chtxtt, title, btime, etime,exp,longexp,category]
79             tempt = p
80         if p > 0:
81             recdblist.printutf8(title + ":" + str(p) + u"点")
82     ret = one
83     print ret
84     return ret
85 def searchtime_auto(keytxt, timet, deltatime):
86     time = datetime.datetime.strptime(timet, "%Y-%m-%d %H:%M:%S")
87     ret = []
88     deltatime = int(deltatime)
89     beforetime = time.strftime("%Y%m%d%H%M%S")
90     aftertime = time + datetime.timedelta(hours=deltatime)
91     aftertime = aftertime.strftime("%Y%m%d%H%M%S")
92     d = rec10d.rec10db.select_by_time_keyword_auto_suggest_epg_timeline(keytxt,beforetime, aftertime)
93     for chtxtt, title, start, stop ,exp,longexp,category in d:
94         btime = start
95         btime = btime[0:4] + "-" + btime[4:6] + "-" + btime[6:8] + " " + btime[8:10] + ":" + btime[10:12] + ":00"
96         etime = stop
97         etime = etime[0:4] + "-" + etime[4:6] + "-" + etime[6:8] + " " + etime[8:10] + ":" + etime[10:12] + ":00"
98         ret.append([chtxtt, title, btime, etime,exp,longexp,category])
99     return ret