OSDN Git Service

Import Rec10 0.3.1
[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 os
6 import sqlite3
7 import chdb
8 import datetime
9 import ts2epg
10 import xml2db
11 #import japanesenkf
12 #japanesenkf.overrideEncodings()
13 path=str(os.path.dirname(os.path.abspath(__file__)))+"/"
14 tmppath=path
15 def update(chtxt):
16     print chtxt
17     tnow=datetime.datetime.now()
18     try:
19         lastup=datetime.datetime.strptime(chdb.chtxtsearch(chtxt)[0]['update'],"%Y-%m-%d %H:%M:%S")
20     except:
21         lastup=datetime.datetime.strptime("2009-04-01 00:00:00","%Y-%m-%d %H:%M:%S")
22     dt=tnow-lastup
23     dt=dt.days*24*60+dt.seconds
24     #print dt
25     if dt>2*60*60:
26         ts2epg.write(tmppath+"epgdate.xml",chdb.chtxtsearch(chtxt)['ch'])
27         xml2db.xml2db(tmppath+"epgdate.xml",path+"ch.db",chdb.chtxtsearch(chtxt)['bctype'])
28         #os.remove(path+"epgdate.xml")
29 def updatebc(bctype):
30     #print bctype
31     #tnow=datetime.datetime.now()
32     bctypel=chdb.bctypesearch(bctype)
33     print bctypel
34     ts2epg.write(tmppath+"epgdate.xml",chdb.bctypesearch(bctype)['ch'])
35     xml2db.xml2db(tmppath+"epgdate.xml",path+"ch.db",bctype)
36     #os.remove(path+"epgdate.xml")
37 def updateall():
38     update("hisch")
39     update("nhk-k")
40     update("nhk-s")
41     update("me-tere")
42     update("cbc")
43     update("aichi")
44     update("toukai")
45     update("chukyo")
46     update("bs-nhk-1")
47     
48 def search(titletxt):
49     dbpath=path+"ch.db"
50     db=sqlite3.connect(dbpath)
51     #print dbpath
52     ret=[]
53     for ch, title, start, stop in db.execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\""):
54         btime=start.replace(" +0900","")
55         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
56         etime=stop.replace(" +0900","")
57         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
58         try:
59             chtxt=chdb.ontvsearch(ch)['chtxt']
60         except:
61             chtxt="Unknown"
62             #print ch
63         ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)
64
65     #r=db.execute("SELECT * FROM tv")
66     #for row in r:
67     #   print unicode(row)
68     #row=r.fetchone()
69     #print r
70     db.close()
71     return ret
72 def searchtime(titletxt,time,deltatime):#時間以内のものだけを表示 deltatimeはhours
73     dbpath=path+"ch.db"
74     db=sqlite3.connect(dbpath)
75     #print dbpath
76     ret=[]
77     deltatime=int(deltatime)
78     for ch, title, start, stop in db.execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\""):
79         btime=start.replace(" +0900","")
80         btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
81         etime=stop.replace(" +0900","")
82         etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
83         bt=datetime.datetime.strptime(btime,"%Y-%m-%d %H:%M:%S")
84         t=datetime.datetime.strptime(time,"%Y-%m-%d %H:%M:%S")
85         try:
86             chtxt=chdb.ontvsearch(ch)['chtxt']
87         except:
88             chtxt="Unknown"
89             #print ch
90         dt=bt-t
91         dt=dt.days*24*60*60+dt.seconds
92         dt=abs(dt)
93         if dt<deltatime*60*60:
94             ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)
95
96     #r=db.execute("SELECT * FROM tv")
97     #for row in r:
98     #   print unicode(row)
99     #row=r.fetchone()
100     #print r
101     db.close()
102     return ret
103