OSDN Git Service

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