OSDN Git Service

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