OSDN Git Service

Import Rec10 0.3.1
[rec10/rec10-git.git] / rec10 / trunk / src / chdb.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5  # モジュール属性 argv を取得するため
6 import os
7 import sqlite3
8
9 dbpath=str(os.path.dirname(os.path.abspath(__file__)))+"/"+"ch.db"
10 #db=sqlite3.connect(dbpath)
11
12 def ontvsearch(ontvin):
13     db=sqlite3.connect(dbpath)
14     ret=[]
15     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE ontv = \""+ontvin+"\""):
16         rett={'bctype':bctype,'ontv':ontv,'chtxt':chtxt,'ch':ch,'csch':csch,'update':updatetime}
17         ret.append(rett)
18     db.close()
19     return ret[0]
20 def chtxtsearch(chtxtin):
21     db=sqlite3.connect(dbpath)
22     ret=[]
23     print chtxtin
24     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE chtxt = \""+chtxtin+"\""):
25         rett={'bctype':bctype,'ontv':ontv,'chtxt':chtxt,'ch':ch,'csch':csch,'update':updatetime}
26         #print rett
27         ret.append(rett)
28     db.close()
29     return ret[0]
30 def bctypesearch(bctypein):
31     db=sqlite3.connect(dbpath)
32     ret=[]
33     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE bctype = \""+bctypein+"\""):
34         rett={'bctype':bctype,'ontv':ontv,'chtxt':chtxt,'ch':ch,'csch':csch,'update':updatetime}
35         #print rett
36         ret.append(rett)
37     db.close()
38     return ret[0]
39 def chsearch(chin):
40     db=sqlite3.connect(dbpath)
41     ret=[]
42     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE ch = \""+chin+"\""):
43         rett={'bctype':bctype,'ontv':ontv,'chtxt':chtxt,'ch':ch,'csch':csch,'update':updatetime}
44         #print rett
45         ret.append(rett)
46     db.close()
47     return ret[0]
48 def getall():
49     db=sqlite3.connect(dbpath)
50     ret=[]
51     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata"):
52         rett={'bctype':bctype,'ontv':ontv,'chtxt':chtxt,'ch':ch,'csch':csch,'update':updatetime}
53         #print rett
54         ret.append(rett)
55     db.close()
56     print ret
57     return ret[0]
58
59 def update():
60     db=sqlite3.connect(dbpath)
61     ret=[]
62     CSupdate=0
63     BSupdate=0
64     for bctype , chtxt in db.execute("SELECT bctype,chtxt FROM chdata WHERE updatetime < datetime(\'now\',\'localtime\',\'-6 hours\')"):
65         #print bctype
66         if bctype=="cs":
67             if CSupdate==0:
68                 ret.append("cs")
69                 CSupdate=1
70                 print "csの番組表取得開始"
71                 db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') WHERE bctype = \""+bctype+"\"")
72         elif bctype=="bs":
73             if BSupdate==0:
74                 ret.append("bs")
75                 BSupdate=1
76                 print "bsの番組表取得開始"
77                 db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') WHERE bctype = \""+bctype+"\"")
78         else:
79             ret.append(bctype)
80             print bctype
81             print "取得開始"
82             db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') WHERE bctype = \""+bctype+"\" AND chtxt = \""+chtxt+"\"")
83     #db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') WHERE updatetime < datetime(\'now\',\'localtime\',\'-2 hours\')")
84     db.commit()
85     db.close()
86     print ret
87     return ret
88 def viewall():
89     db=sqlite3.connect(dbpath)
90     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata"):
91         print (bctype+","+ontv+","+chtxt+","+ch+","+csch+","+updatetime)
92     db.close()
93 #def updateall():
94
95