mas01mj@733
|
1 #!/usr/bin/python
|
mas01mj@733
|
2
|
mas01mj@733
|
3 import sqlite3
|
mas01mj@733
|
4 import csv
|
mas01mj@733
|
5 import sys
|
mas01mj@733
|
6 import urllib
|
mas01mj@733
|
7 import web
|
mas01mj@733
|
8 from pyadb import *
|
mas01mj@733
|
9 import simplejson as json
|
mas01mj@733
|
10 import tempfile
|
mas01mj@733
|
11 import subprocess
|
mas01mj@733
|
12
|
mas01mj@733
|
13 # For now
|
mas01mj@733
|
14
|
mas01mj@733
|
15 featureFile = "AWAL2010.adb"
|
mas01mj@733
|
16 trackFile = "awal_id32.csv"
|
mas01mj@733
|
17 audioDir = "/home/mjewell/Music/mp3s/"
|
mas01mj@733
|
18
|
mas01mj@733
|
19 urls = (
|
mas01mj@733
|
20 '/track/(.*)', 'trackSearch',
|
mas01mj@733
|
21 '/audio/(.*)', 'audioPlay',
|
mas01mj@733
|
22 '/search/(.*)', 'segmentSearch',
|
mas01mj@733
|
23 '/crossdomain.xml', 'crossDomain'
|
mas01mj@733
|
24 )
|
mas01mj@733
|
25
|
mas01mj@733
|
26 app = web.application(urls, globals())
|
mas01mj@733
|
27 db = Pyadb(path = featureFile, mode = "r")
|
mas01mj@733
|
28 dbfile = tempfile.NamedTemporaryFile(suffix = ".db")
|
mas01mj@733
|
29 mdb = web.database(dbn='sqlite', db="metadata.db")
|
mas01mj@733
|
30
|
mas01mj@733
|
31
|
mas01mj@733
|
32
|
mas01mj@733
|
33 def buildDatabase(csvfile):
|
mas01mj@733
|
34 print "Build DB"
|
mas01mj@733
|
35 mdb._db_cursor().connection.text_factory=str
|
mas01mj@733
|
36 mdb._db_cursor().connection.execute('DROP TABLE IF EXISTS media')
|
mas01mj@733
|
37 mdb._db_cursor().connection.execute('CREATE TABLE media (uid TEXT NOT NULL PRIMARY KEY, artist TEXT, album TEXT, track TEXT, tracknum INTEGER, filename TEXT, seconds INTEGER)')
|
mas01mj@733
|
38
|
mas01mj@733
|
39 trackReader = csv.reader(open(csvfile))
|
mas01mj@733
|
40 head = True
|
mas01mj@733
|
41 for row in trackReader:
|
mas01mj@733
|
42 if head:
|
mas01mj@733
|
43 head = False
|
mas01mj@733
|
44 continue
|
mas01mj@733
|
45 mdb.insert('media', uid=row[0], filename=row[1], artist=row[2], track=row[3], album=row[4], tracknum=row[5], seconds=row[6])
|
mas01mj@733
|
46
|
mas01mj@733
|
47
|
mas01mj@733
|
48 def retrieveTrackData(trackID):
|
mas01mj@733
|
49 results = mdb.select('media', dict(uid=trackID), where='uid = $uid')
|
mas01mj@733
|
50 res = dict()
|
mas01mj@733
|
51 try:
|
mas01mj@733
|
52 result = results[0]
|
mas01mj@733
|
53 except IndexError:
|
mas01mj@733
|
54 return False
|
mas01mj@733
|
55 for key in result.keys():
|
mas01mj@733
|
56 res[key] = result[key]
|
mas01mj@733
|
57
|
mas01mj@733
|
58 return res
|
mas01mj@733
|
59
|
mas01mj@733
|
60 class trackSearch:
|
mas01mj@733
|
61 def GET(self, trackID):
|
mas01mj@733
|
62 data = retrieveTrackData(trackID)
|
mas01mj@733
|
63 if data:
|
mas01mj@733
|
64 return json.dumps(dict(status = "ok", data = data))
|
mas01mj@733
|
65 else:
|
mas01mj@733
|
66 return json.dumps(dict(status = "error", message=str("Invalid key")))
|
mas01mj@733
|
67
|
mas01mj@733
|
68 class crossDomain:
|
mas01mj@733
|
69 def GET(self):
|
mas01mj@733
|
70 return """<?xml version="1.0"?>
|
mas01mj@733
|
71 <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
|
mas01mj@733
|
72 <cross-domain-policy>
|
mas01mj@733
|
73 <allow-access-from domain="*" />
|
mas01mj@733
|
74 </cross-domain-policy> """
|
mas01mj@733
|
75
|
mas01mj@733
|
76 class audioPlay:
|
mas01mj@733
|
77 def GET(self, trackID):
|
mas01mj@733
|
78 web.header("Content-Type", "audio/mpeg")
|
mas01mj@733
|
79 user_data = web.input()
|
mas01mj@733
|
80 track_data = retrieveTrackData(trackID)
|
mas01mj@733
|
81 tempMp3 = tempfile.NamedTemporaryFile(suffix = ".mp3")
|
mas01mj@733
|
82 tempWav = tempfile.NamedTemporaryFile(suffix = ".wav")
|
mas01mj@733
|
83 subprocess.call(["sox", audioDir+track_data['filename'], tempWav.name, "trim", user_data['start'], user_data['length'], "fade", "0.3", user_data['length']])
|
mas01mj@733
|
84 subprocess.call(["lame", "--quiet", tempWav.name, tempMp3.name])
|
mas01mj@733
|
85
|
mas01mj@733
|
86 return tempMp3.read()
|
mas01mj@733
|
87
|
mas01mj@733
|
88 class segmentSearch:
|
mas01mj@733
|
89 def GET(self, trackID):
|
mas01mj@733
|
90 params = web.input(db="AWAL",key="", ntracks=20, seqStart=0, seqLength=16, npoints=1, radius=1.0, hopSize=1, exhaustive=False, falsePositives=False, accumulation="track", distance="eucNorm", absThres=-6, relThres=10, durRatio=0, includeKeys=[], excludeKeys=[], resFmt="dict")
|
mas01mj@733
|
91
|
mas01mj@733
|
92 db.configQuery["ntracks"] = int(params.ntracks)
|
mas01mj@733
|
93 db.configQuery["npoints"] = int(params.npoints)
|
mas01mj@733
|
94 db.configQuery["seqStart"] = int(params.seqStart)
|
mas01mj@733
|
95 db.configQuery["seqLength"] = int(params.seqLength)
|
mas01mj@733
|
96 db.configQuery["hopSize"] = int(params.hopSize)
|
mas01mj@733
|
97 db.configQuery["radius"] = float(params.radius)
|
mas01mj@733
|
98 db.configQuery["absThres"] = float(params.absThres)
|
mas01mj@733
|
99 db.configQuery["relThres"] = float(params.relThres)
|
mas01mj@733
|
100 db.configQuery["durRatio"] = float(params.durRatio)
|
mas01mj@733
|
101 db.configQuery["resFmt"] = "list";
|
mas01mj@733
|
102 #db.configQuery["includeKeys"] = ["AWAL2000", "AWAL500", "AWAL1000"]
|
mas01mj@733
|
103
|
mas01mj@733
|
104 results = dict()
|
mas01mj@733
|
105 try:
|
mas01mj@733
|
106 results = db.query(key = trackID)
|
mas01mj@733
|
107 return json.dumps(dict(status = "ok", data = results.rawData))
|
mas01mj@733
|
108 except Exception:
|
mas01mj@733
|
109 return json.dumps(dict(status = "error", message=str("Fix inst")))
|
mas01mj@733
|
110
|
mas01mj@733
|
111 if __name__ == "__main__":
|
mas01mj@733
|
112 # Uncomment to rebuild the db at start
|
mas01mj@733
|
113 #buildDatabase(trackFile)
|
mas01mj@733
|
114 app.run()
|