OSDN Git Service

This is the first branches of rec10 0.4
[rec10/rec10-git.git] / rec10 / branches / 0.4 / src / xml2db.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 #import sys
6 #import os
7 import xml.parsers.expat
8 import sqlite3
9 #import xml.sax as sax
10 #from xml.sax.saxutils import DefaultHandler
11 flags={}
12 lastflag=""
13 xmldate={}
14 db=""
15 bctype=""
16 def xml2db(xmlpath,dbpath,bctypetemp):#bctypeは放送種別で'TE'(地デジ)BS,CSがある。地デジの場合は te数字 が入る
17     global db,bctype
18     bctype=bctypetemp
19     #print path
20     createDB(dbpath)
21     #db.execute('drop table tv')
22     #db.execute('drop table ch')
23
24     p=xml.parsers.expat.ParserCreate()
25     p.buffer_text=True
26     p.StartElementHandler=start_element
27     p.EndElementHandler=end_element
28     p.CharacterDataHandler=char_data
29     parseDocument(p,xmlpath)
30
31 def createDB(path):
32     global db,bctype,fpath
33     fpath=path
34     db=sqlite3.connect(fpath)
35     print fpath
36     try:
37         db.execute("delete from tv where bctype = \'"+bctype+"\'")
38     except:
39         test=""
40     try:
41         db.execute("delete from ch where bctype = \'"+bctype+"\'")
42     except:
43         test=""
44     try:
45         db.execute('create table tv (bctype TEXT,channel TEXT NOT NULL,start TEXT,stop  TEXT,title TEXT,desc  TEXT)')
46     except:
47         test=""
48     try:
49         db.execute('create table ch (bctype TEXT,channel TEXT NOT NULL,display TEXT)')
50     except:
51         test=""
52     #db.commit()
53     #db.close()
54     #return db
55 def writetvDB(datetv):#tvdateは(放送種別,チャンネル,start,stop,title,desc)の形式
56     global db
57     #db=sqlite3.connect(fpath)
58     #print datetv
59     db.execute('insert into tv values '+datetv)
60     #db.commit
61     #return db
62     #db.commit()
63     #db.close()
64 def writechDB(datech):#tvdateは(チャンネル,display)の形式
65     global db
66     #db=sqlite3.connect(fpath)
67     #print datech
68     db.execute('insert into ch values '+datech)
69     #db.commit
70     #return db
71     #db.commit()
72     #db.close()
73 def parseDocument(p,xmlfile):
74     f=open(xmlfile,'r')
75     p.ParseFile(f)
76     f.close()
77 def start_element(name,attrs):
78     global flags,lastflag,xmldate
79     flags[name]=1
80     lastflag=name
81     for (key,val) in attrs.iteritems():
82         xmldate[name+":"+key]=val
83 def end_element(name):
84     global flags,lastflag,xmldate,db
85     if flags[name]==1: 
86         if name=="channel":
87             xmld="(\'"+bctype+"\',\'"+xmldate['channel:id']+"\',\'"+xmldate['display-name']+"\')"
88             xmld=unicode(xmld)
89             writechDB(xmld)
90             xmldate['channel:id']=""
91             xmldate['display-name']=""
92             #xmldate={}
93             #print xmld
94         elif name=="programme":
95             
96             xmld="(\'"+bctype+"\',\'"+xmldate['programme:channel']+"\',\'"+xmldate['programme:start'].replace(" +0900","")+"\',\'"+xmldate['programme:stop'].replace(" +0900","")+"\',\'"+xmldate['title'].replace(",","-")+"\',\'"+xmldate['desc']+"\')"
97             xmld=unicode(xmld)
98             writetvDB(xmld)
99             #print xmld
100             xmldate['title']=""
101             xmldate['desc']=""
102             #xmldate[]
103         if name=="tv":
104             """
105             r=db.execute("SELECT * FROM tv")
106             for row in r:
107                 print row
108             """
109             db.commit()
110             db.close()
111     flags[name]=0
112 def char_data(date):
113     global flags,lastflag,xmldate
114     #string=str(date)
115     date=date.replace("\n","")
116     date=date.replace(" ","")
117     date=date.replace("(","[")
118     date=date.replace(")","]")
119     date=date.replace("(","[")
120     date=date.replace(")","]")
121     date=date.replace("\"","")
122     date=date.replace("\'","")
123     date=date.encode('utf-8')
124     #date=string
125     if xmldate.has_key(lastflag):
126         if xmldate[lastflag]!=date:
127             xmldate[lastflag]+=date
128     else:
129         xmldate[lastflag]=date
130     #if lastflag=='category':
131     #print date
132     #print xmldate
133
134
135
136