OSDN Git Service

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