OSDN Git Service

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