-
Notifications
You must be signed in to change notification settings - Fork 0
/
artist.py
121 lines (106 loc) · 3.2 KB
/
artist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python
import xml.sax as sax
import xml.sax.handler as handler
from optparse import OptionParser
import sys
import pprint
import re
import codecs
import os
import MySQLdb
import traceback
from my_radio_module import normalize
class Artist:
def __init__(self,
alias = None ,
assoc = None ,
backgnd = None ,
birthname = None ,
born = None ,
caption = None ,
died = None ,
genre = None ,
image = None ,
instrument = None ,
label = None ,
members = None ,
name = None ,
nname = None ,
origin = None ,
pastmemb = None ,
url = None ,
voice = None ,
years = None ,
xml = None,
id = None ):
self.alias = alias
self.assoc = assoc
self.backgnd = backgnd
self.birthname = birthname
self.born = born
self.caption = caption
self.died = died
self.genre = genre
self.image = image
self.instrument = instrument
self.label = label
self.members = members
self.name = name
self.nname = nname
self.origin = origin
self.pastmemb = pastmemb
self.url = url
self.voice = voice
self.years = years
self.xml = xml
self.id = id
class ParaHandler( handler.ContentHandler ):
def __init__(self,verbose = False):
handler.ContentHandler.__init__(self)
self.paras = []
self.curpara = ""
self.cursection = ""
self.inpara = False
self.insection = False
def getParas( self ):
return self.paras
def startElement( self,name,attrs):
self.curpara = ""
if name == "Section":
self.insection = True
self.cursection = ""
else:
self.insection = False
if name == "Paragraph":
self.inpara = True
else:
self.inpara = False
def characters( self, content):
if self.inpara:
self.curpara += content
elif self.insection:
self.cursection += content
def endElement( self,name):
if name == "Paragraph":
self.paras.append((self.cursection, self.curpara))
elif name == "Section":
self.insection = False
self.cursection = ""
conn = MySQLdb.connect( host="localhost", user=None, passwd=None, db="MYRADIO", charset="utf8" )
cursor = conn.cursor()
def fetch_artist( name ):
sel = "SELECT * FROM MYRADIO.ARTISTS WHERE NAME = '%s'"%(normalize(name))
cursor.execute(sel)
return [ Artist(*f) for f in cursor.fetchall() ]
def fetch_paragraphs( name ):
alist = fetch_artist( name )
paras = []
handler = ParaHandler()
for a in alist:
xml = sax.parse(open(a.xml), handler)
return handler.getParas()
if __name__ == '__main__':
for a in fetch_artist("Bud Powell"):
print a.nname,a.xml
for p in fetch_paragraphs("Bud Powell"):
print p