OSDN Git Service

the first release of 0.5
[rec10/rec10-git.git] / rec10 / branches / 0.5 / 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     CS2update=0
64     BSupdate=0
65     for bctype , chtxt in db.execute("SELECT bctype,chtxt FROM chdata WHERE updatetime < datetime(\'now\',\'localtime\',\'-6 hours\')"):
66         print bctype
67         if bctype=="cs" or bctype==u"cs":
68             if CSupdate==0:
69                 ret.append(u"cs")
70                 CSupdate=1
71                 print "csの番組表取得開始"
72                 db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') WHERE bctype = \""+bctype+"\"")
73         elif bctype=="cs2" or bctype==u"cs2":
74             if CS2update==0:
75                 ret.append(u"cs2")
76                 CS2update=1
77                 print "cs2の番組表取得開始"
78                 db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') WHERE bctype = \""+bctype+"\"")
79         elif bctype=="bs" or bctype==u"bs":
80             if BSupdate==0:
81                 ret.append(u"bs")
82                 BSupdate=1
83                 print "bsの番組表取得開始"
84                 db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') WHERE bctype = \""+bctype+"\"")
85         else:
86             ret.append(bctype)
87             print bctype
88             print "取得開始"
89             db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') WHERE bctype = \""+bctype+"\" AND chtxt = \""+chtxt+"\"")
90     #db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') WHERE updatetime < datetime(\'now\',\'localtime\',\'-2 hours\')")
91     db.commit()
92     db.close()
93     print ret
94     return ret
95 def viewall():
96     db=sqlite3.connect(dbpath)
97     for bctype, ontv, chtxt, ch, csch ,updatetime in db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata"):
98         print (bctype+","+ontv+","+chtxt+","+ch+","+csch+","+updatetime)
99     db.close()
100 #def updateall():
101
102