OSDN Git Service

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