3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
8 import xml.parsers.expat
10 #import xml.sax as sax
11 #from xml.sax.saxutils import DefaultHandler
17 def xml2db(xmlpath,dbpath,bctypetemp):#bctypeは放送種別で'TE'(地デジ)BS,CSがある。地デジの場合は te数字 が入る
22 #db.execute('drop table tv')
23 #db.execute('drop table ch')
25 p=xml.parsers.expat.ParserCreate()
27 p.StartElementHandler=start_element
28 p.EndElementHandler=end_element
29 p.CharacterDataHandler=char_data
30 parseDocument(p,xmlpath)
33 global db,bctype,fpath
35 db=sqlite3.connect(fpath)
38 db.execute("delete from tv where bctype = \'"+bctype+"\'")
42 db.execute("delete from ch where bctype = \'"+bctype+"\'")
46 db.execute('create table tv (bctype TEXT,channel TEXT NOT NULL,start TEXT,stop TEXT,title TEXT,desc TEXT)')
50 db.execute('create table ch (bctype TEXT,channel TEXT NOT NULL,display TEXT)')
53 def writetvDB(bctype,channel,start,stop,title,desc):#tvdateは(放送種別,チャンネル,start,stop,title,desc)の形式
56 db.execute('insert into tv values (?,?,?,?,?,?)',(bctype,channel,start,stop,title,desc))
58 print "xml2db-error(writetvDB):"+"'insert into tv values '"+title
59 def writetvDB_bef(datetv):#tvdateは(放送種別,チャンネル,start,stop,title,desc)の形式
62 db.execute('insert into tv values '+datetv)
64 print "xml2db-error(writetvDB):"+"'insert into tv values '"+datetv
65 def writechDB(datech):#tvdateは(チャンネル,display)の形式
67 db.execute('insert into ch values '+datech)
68 def parseDocument(p,xmlfile):
72 def start_element(name,attrs):
73 global flags,lastflag,xmldate
76 for (key,val) in attrs.iteritems():
77 xmldate[name+":"+key]=val
78 def end_element(name):
79 global flags,lastflag,xmldate,db
82 xmld="(\""+bctype+"\",\""+xmldate['channel:id']+"\",\""+xmldate['display-name']+"\")"
85 xmldate['channel:id']=""
86 xmldate['display-name']=""
89 elif name=="programme":
90 ubctype=unicode(bctype)
91 uch=unicode(xmldate['programme:channel'])
92 ustart=unicode(xmldate['programme:start'].replace(" +0900",""))
93 ustop=unicode(xmldate['programme:stop'].replace(" +0900",""))
94 utitle=unicode(xmldate['title'].replace(",","-"))
95 udesc=unicode(xmldate['desc'])
96 writetvDB(ubctype,uch,ustart,ustop,utitle,udesc)
103 r=db.execute("SELECT * FROM tv")
111 global flags,lastflag,xmldate
112 data=data.replace("\n","")
113 data=data.replace(" ","")
114 data=data.replace("\"","")
115 data=data.replace("\'","")
117 datat=zenhan.toHankaku(data)
120 data=datat.encode('utf-8')
121 if xmldate.has_key(lastflag):
122 if xmldate[lastflag]!=data:
123 xmldate[lastflag]+=data
125 xmldate[lastflag]=data