Skip to content

Commit d086cdf

Browse files
author
Walter Schweitzer
committed
orig authored 2010
0 parents  commit d086cdf

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed

createXml.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import getopt, sys
2+
from xml.dom.minidom import Document
3+
from dataxml import globalxml, metadataAlbum, trackxml
4+
from csvfile import openFile
5+
6+
## for arg in sys.argv:
7+
try:
8+
file = sys.argv[1]
9+
## print file
10+
except IndexError, e:
11+
print "pleae pass in a csv file to be processed, arg 1"
12+
sys.exit()
13+
14+
rawData = openFile(file)
15+
## rawData = openFile('data.csv')
16+
## print rawData
17+
18+
# Create the minidom document
19+
doc = Document()
20+
21+
# Create the base element
22+
xml = doc.createElement("DATA")
23+
doc.appendChild(xml)
24+
25+
# Create the main root component
26+
main = doc.createElement("MUSICNET_COMPONENT")
27+
xml.appendChild(main)
28+
29+
## BUILDS THE GLOBAL COMPONENTS
30+
globalxml(doc, main, rawData)
31+
32+
## BUILD ALBUM LEVEL COMPONENTS
33+
metadataAlbum(doc, main, rawData)
34+
35+
## BUILD TRACK LEVEL COMPONENTS
36+
trackxml(doc, main, rawData)
37+
38+
# print xml
39+
## print doc.toprettyxml(indent=" ")
40+
41+
## FILE = open('file.xml', "w")
42+
## FILE.writelines(doc.toprettyxml(indent=" "))
43+
44+
## WRITE XML TO FILE
45+
for upc in rawData:
46+
num = upc[0]
47+
break
48+
49+
## directory = "/root/python/xml/"
50+
directory = "/root/python/xml/data/"
51+
newFile = directory + num + "/" + num + ".xml"
52+
## print newFile
53+
FILE = open(newFile, "w")
54+
FILE.writelines(doc.toprettyxml())
55+
FILE.close()
56+
57+

csvfile.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
##########################################
2+
## Name: readFile
3+
## params: file -filename to be read
4+
## return: file
5+
##########################################
6+
import os
7+
import csv
8+
9+
def openFile(file):
10+
data = csv.reader(open(file, "rb"))
11+
data_list = []
12+
data_list.extend(data)
13+
upc = []
14+
15+
for items in data_list:
16+
upc.append(items[0])
17+
18+
createDir(upc)
19+
20+
return data_list
21+
22+
def createDir(upc):
23+
dir = "/root/python/xml/data/"
24+
for item in upc:
25+
try:
26+
os.makedirs(dir + item)
27+
directory = "/root/python/xml/data/"
28+
newFile = directory + item + "/" + item + ".xml"
29+
FILE = open(newFile, "w")
30+
FILE.close()
31+
except OSError:
32+
if os.path.exists(dir + item):
33+
pass
34+
else:
35+
raise
36+
return

dataxml.py

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
##########################################
2+
## Name: readFile
3+
## params: file -filename to be read
4+
## return: file
5+
##########################################
6+
def globalxml(doc, main, rawData):
7+
8+
## upc = []
9+
for items in rawData:
10+
## upc.append(items[0])
11+
upc = items[0]
12+
labelCode = items[4]
13+
corpCode = items[5]
14+
artistName = items[6]
15+
16+
17+
code = doc.createElement("MUSICNET_COMPONENT_CODE")
18+
main.appendChild(code)
19+
upcData = doc.createTextNode(upc)
20+
code.appendChild(upcData)
21+
22+
component = doc.createElement("COMPONENT_TYPE")
23+
componentData = doc.createTextNode("ALBUM")
24+
main.appendChild(component)
25+
component.appendChild(componentData)
26+
27+
corp_code = doc.createElement("CORP_CODE")
28+
corpData = doc.createTextNode(corpCode)
29+
main.appendChild(corp_code)
30+
corp_code.appendChild(corpData)
31+
32+
label_code = doc.createElement("LABEL_CODE")
33+
labelData = doc.createTextNode(labelCode)
34+
main.appendChild(label_code)
35+
label_code.appendChild(labelData)
36+
37+
artist = doc.createElement("ARTIST")
38+
main.appendChild(artist)
39+
type = doc.createElement("TYPE")
40+
value = doc.createElement("VALUE")
41+
typeData = doc.createTextNode("ARTIST")
42+
valueData = doc.createTextNode(artistName)
43+
artist.appendChild(type)
44+
artist.appendChild(value)
45+
type.appendChild(typeData)
46+
value.appendChild(valueData)
47+
48+
49+
return
50+
51+
def metadataAlbum(doc, main, rawData):
52+
for items in rawData:
53+
upc = items[0]
54+
tracks = items[3]
55+
album = items[7]
56+
releaseDate = items[12]
57+
genre = items[13]
58+
parentAdvisory = items[14]
59+
duration = items[16]
60+
digitalavail = items[17]
61+
break
62+
63+
## artist = doc.createElement("ARTIST")
64+
65+
## main.appendChild(artist)
66+
## artist.appendChild(doc.createElement("TYPE"))
67+
## artist.appendChild(doc.createElement("VALUE"))
68+
69+
vals = ["TRACKS","TITLE","GENRE","RELEASE_DATE","PARENTAL_ADVISORY",
70+
"RELATED_UPC","DURATION"]
71+
72+
for x in vals:
73+
meta = main.appendChild(doc.createElement("METADATA"))
74+
type = meta.appendChild(doc.createElement("TYPE"))
75+
value = meta.appendChild(doc.createElement("VALUE"))
76+
typeData = doc.createTextNode(x)
77+
if x == "TRACKS":
78+
tracksData = doc.createTextNode(tracks)
79+
value.appendChild(tracksData)
80+
if x == "TITLE":
81+
titleData = doc.createTextNode(album)
82+
value.appendChild(titleData)
83+
if x == "GENRE":
84+
genreData = doc.createTextNode(genre)
85+
value.appendChild(genreData)
86+
if x == "RELATED_UPC":
87+
upcData = doc.createTextNode(upc)
88+
value.appendChild(upcData)
89+
if x == "PARENTAL_ADVISORY":
90+
parentData = doc.createTextNode(parentAdvisory)
91+
value.appendChild(parentData)
92+
if x == "RELEASE_DATE":
93+
releaseData = doc.createTextNode(releaseDate)
94+
value.appendChild(releaseData)
95+
if x == "DURATION":
96+
durationData = doc.createTextNode(duration)
97+
value.appendChild(durationData)
98+
99+
type.appendChild(typeData)
100+
101+
if digitalavail == "WW-MN":
102+
print "digital avail == 1"
103+
else:
104+
print "digital avail != 1"
105+
106+
107+
return
108+
109+
def trackxml(doc, main, rawData):
110+
## print "TRACK LEVEL: work to do here"
111+
vals = ["TITLE","GENRE","DISC_NUMBER","TRACK_NUMBER","ISRC","DURATION","PARENTAL_ADVISORY","RELEASE_DATE","RELATED_UPC","ARTIST"]
112+
113+
for item in rawData:
114+
upc = item[0]
115+
track = item[2]
116+
117+
component = doc.createElement("MUSICNET_COMPONENT")
118+
main.appendChild(component)
119+
120+
code = doc.createElement("MUSICNET_COMPONENT_CODE")
121+
component.appendChild(code)
122+
codeData = doc.createTextNode(upc)
123+
code.appendChild(codeData)
124+
125+
componentType = doc.createElement("COMPONENT_TYPE")
126+
component.appendChild(componentType)
127+
componentData = doc.createTextNode("TRACK")
128+
componentType.appendChild(componentData)
129+
130+
corp_code = doc.createElement("CORP_CODE")
131+
component.appendChild(corp_code)
132+
133+
label_code = doc.createElement("LABEL_CODE")
134+
component.appendChild(label_code)
135+
136+
for x in vals:
137+
metadata = doc.createElement("METADATA")
138+
type = doc.createElement("TYPE")
139+
value = doc.createElement("VALUE")
140+
141+
component.appendChild(metadata)
142+
metadata.appendChild(type)
143+
metadata.appendChild(value)
144+
145+
typeData = doc.createTextNode(x)
146+
if x == "TRACK_NUMBER":
147+
trackData = doc.createTextNode(track)
148+
value.appendChild(trackData)
149+
type.appendChild(typeData)
150+
151+
return
152+

run.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
clear
2+
echo "Hi, $USER!"
3+
echo "Startup.. Python about to process files"
4+
5+
FILES="*"
6+
## python ../createXml.py $file
7+
for file in csv/*.csv;
8+
do
9+
echo "Processing $file file..........";
10+
python createXml.py $file
11+
12+
done
13+
14+
echo "Finished, $USER!"
15+
echo "XML Creation Completed!"

0 commit comments

Comments
 (0)