forked from pwhelan/mixxx-crateport
/
crateport.py
executable file
·264 lines (208 loc) · 6.08 KB
/
crateport.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
#
import sqlite3
import os
import sys
import xml.dom
import xml.dom.minidom
import platform
import fileinput
import tarfile
from optparse import OptionParser
def generateCrateXML(crates):
dom = xml.dom.getDOMImplementation()
document = dom.createDocument(None, None, None)
ncrates = document.createElement('crates')
document.appendChild(ncrates)
for cratename in crates:
ncrate = document.createElement('crate')
ncrates.appendChild(ncrate)
ncrate.setAttribute('name', cratename)
for track in crates[cratename]:
ntrack = document.createElement('track')
ncrate.appendChild(ntrack)
for key in track.keys():
ntrack.setAttribute(key, unicode(track[key]))
return document.toxml()
def listCrates(conn):
cursor = conn.cursor()
cratelist = []
cursor.execute("SELECT id, name FROM crates")
row = cursor.fetchone()
while row:
cratelist.append(row[1])
row = cursor.fetchone()
return cratelist
def getCrates(conn):
cursor = conn.cursor()
crates = {}
cursor.execute("SELECT id, name FROM crates")
row = cursor.fetchone()
while row:
crates[row['name']] = []
cur2 = conn.cursor()
cur2.execute("""
SELECT
library.artist AS artist,
library.title AS title,
track_locations.location,
track_locations.filename
FROM crate_tracks
INNER JOIN library
ON crate_tracks.track_id = library.id
INNER JOIN track_locations
ON library.location = track_locations.id
WHERE
crate_tracks.crate_id = ?
""", (str(row['id']),))
track = cur2.fetchone()
while track:
crates[row['name']].append(track)
track = cur2.fetchone()
row = cursor.fetchone()
return crates
# just the filenames, so we can generate a tar file.
def filenamesfromCrates(conn):
cursor = conn.cursor()
crates = {}
files = []
cursor.execute("SELECT id, name FROM crates")
row = cursor.fetchone()
while row:
crates[row['name']] = []
cur2 = conn.cursor()
cur2.execute("""
SELECT
track_locations.location,
track_locations.filename
FROM crate_tracks
INNER JOIN library
ON crate_tracks.track_id = library.id
INNER JOIN track_locations
ON library.location = track_locations.id
WHERE
crate_tracks.crate_id = ?
""", (str(row['id']),))
track = cur2.fetchone()
while track:
files.append(track[0])
track = cur2.fetchone()
row = cursor.fetchone()
return files
def findTrack(conn, ntrack):
location = ntrack.getAttribute('location')
artist = ntrack.getAttribute('artist')
title = ntrack.getAttribute('title')
filename = ntrack.getAttribute('filename')
cursor = conn.cursor()
cursor.execute("""
SELECT
l.id,
l.filetype
FROM library l
INNER JOIN track_locations tl
ON l.location = tl.id
WHERE
(tl.location = ?)
""", (location,))
track = cursor.fetchone()
if track != None:
return track
cursor.execute("""
SELECT
l.id,
l.filetype
FROM library l
INNER JOIN track_locations tl
ON l.location = tl.id
WHERE
(tl.filename = ?)
""", (filename,))
track = cursor.fetchone()
if track != None:
return track
cursor.execute("""
SELECT
l.id,
l.filetype
FROM library l
WHERE
(l.artist = ? AND l.title = ?)
""", (artist, title))
track = cursor.fetchone()
if track != None:
return track
return None
def importCrateXML(conn, dcrate):
cursor = conn.cursor()
ncrates = dcrate.documentElement
if ncrates.tagName != 'crates':
raise Exception('Not a Crates XML File')
for ncrate in ncrates.childNodes:
if ncrate.tagName != 'crate':
raise Exception('Not a Crate')
try:
cursor.execute("INSERT INTO crates(name) VALUES(?)",
(ncrate.getAttribute('name'),))
print "Creating new Crate:", ncrate.getAttribute('name')
except sqlite3.IntegrityError:
print "Already Created:", ncrate.getAttribute('name')
cursor.execute("SELECT id FROM crates WHERE name = ?",
(ncrate.getAttribute('name'),))
crate = cursor.fetchone()
for ntrack in ncrate.childNodes:
if ncrate.tagName != 'crate':
raise Exception('Not a Crate')
track = findTrack(conn, ntrack)
if track != None:
try:
print "Adding a Track"
cursor.execute("""
INSERT INTO crate_tracks(crate_id, track_id)
VALUES(?, ?)
""", (str(crate['id']), track['id']))
except sqlite3.IntegrityError:
print "Track already in crate"
def main():
home = os.path.expanduser('~')
uname = platform.uname()
if uname[0] == 'Darwin':
cfgdir = home + '/Library/Application Support/Mixxx'
elif uname[0] == 'Linux':
cfgdir = home + '/.mixxx'
defdb = cfgdir + '/mixxxdb.sqlite'
opt = OptionParser(description='Import and Export Crates from Mixxx')
opt.add_option('-i', '--import', dest='export', action='store_false')
opt.add_option('-e', '--export', dest='export', action='store_true')
opt.add_option('-d', '--dbname', dest='dbname', default=defdb)
opt.add_option('-l', '--list', dest='listcrates', action='store_true', default=False)
opt.add_option('-t', '--tar', dest='tarcrates', action='store_true', default=False)
(options, args) = opt.parse_args()
if options.export == None: options.export = True;
conn = sqlite3.connect(options.dbname)
conn.row_factory = sqlite3.Row
if options.listcrates == True:
print "list of crates:"
for cratename in listCrates(conn):
print cratename
sys.exit(0)
# simple streaming tar to stdout... dont send anything else to stdout if we do this, as it will mess up the tar file
if options.tarcrates == True:
tar = tarfile.open(fileobj=sys.stdout,mode='w|')
# list set stuff to make list unique to get rid of doubled up file names.
for filename in list(set(filenamesfromCrates(conn))):
tar.add(filename)
tar.close()
sys.exit(0)
if options.export == True:
output = open(args[0], "w") if len(args) > 0 else sys.stdout
crates = getCrates(conn)
output.write(generateCrateXML(crates).encode('utf8') + "\n")
else:
input = open(args[0], "r") if len(args) > 0 else sys.stdin
crates = xml.dom.minidom.parse(input)
importCrateXML(conn, crates)
conn.commit()
conn.close()
if __name__ == '__main__':
main()