Permalink
Browse files

Dynamically create musicdb.json.

  • Loading branch information...
1 parent 31d8eaa commit e9ea9794dafc97d3742d8d4fef50be84dc83c31d @beakdoctor committed Dec 16, 2011
Showing with 34 additions and 12 deletions.
  1. +8 −1 diapason.py
  2. +25 −10 createdb.py → gendb.py
  3. +1 −1 static/app/store/Songs.js
View
@@ -1,9 +1,12 @@
#!/usr/bin/env python
import web
+
+import gendb
urls = (
- '/', 'index'
+ '/', 'index',
+ '/musicdb.json', 'musicdb'
)
app = web.application(urls, globals())
@@ -13,5 +16,9 @@ class index:
def GET(self):
return render.index()
+class musicdb:
+ def GET(self):
+ return gendb.create_json("static/media").create_string()
+
if __name__ == "__main__":
app.run()
View
@@ -6,15 +6,24 @@
from mutagen.oggvorbis import OggVorbis
supported_formats = [".mp3", ".m4a", ".ogg"]
+
+class StringBuilder:
+ def __init__(self):
+ self.string_list = []
+ def append(self, s):
+ self.string_list.append(s)
+ def create_string(self):
+ return "".join(self.string_list)
+
-def create_json(root, indent_level=0):
- print "%s{name: '%s', children: [" % (indent(indent_level), esc(basename(root)))
+def create_json(root, builder=StringBuilder(), indent_level=0):
+ builder.append("%s{name: '%s', children: [" % (indent(indent_level), esc(basename(root))))
entries = os.listdir(root)
files = [f for f in entries if isfile(join(root, f))]
dirs = [d for d in entries if isdir(join(root, d))]
for d in dirs:
- create_json(join(root, d), indent_level+1)
+ create_json(join(root, d), builder, indent_level+1)
# Sort the files. In case the metadata doesn't contain tracknumber, this is useful.
files.sort()
@@ -24,13 +33,18 @@ def create_json(root, indent_level=0):
continue
url = join(root, f).encode("utf-8")
title, artist, album, track, length, bitrate = parse_metadata(url, def_title=f, def_track="%d" % (index+1))
- print "%s{name: '%s', artist: '%s', album: '%s', tracknumber: %s, leaf: true, length: %d, bitrate: %d, url: '%s'}," % \
- (indent(indent_level+1), esc(title), esc(artist), esc(album), track, length, bitrate, esc(url))
+ builder.append("%s{name: '%s', " % (indent(indent_level+1), esc(title)))
+ builder.append("artist: '%s', album: '%s', " % (esc(artist), esc(album)))
+ builder.append("tracknumber: %s, leaf: true, length: %d, " % (track, length))
+ builder.append("bitrate: %d, url: '%s'}," % (bitrate, esc(url)))
+
if (indent_level == 0):
- print "%s]}" % (indent(indent_level))
+ builder.append("%s]}" % (indent(indent_level)))
else:
- print "%s]}," % (indent(indent_level))
+ builder.append("%s]}," % (indent(indent_level)))
+
+ return builder
def parse_metadata(f, def_title="N/A", def_artist="N/A", def_album="N/A", def_track=1):
try:
@@ -59,6 +73,7 @@ def esc(s):
def main():
path = sys.argv[1]
- create_json(path, 0)
-
-main()
+ print create_json(path).create_string()
+
+if __name__ == "__main__":
+ main()
@@ -4,7 +4,7 @@ Ext.define('Music.store.Songs', {
autoLoad: true,
proxy: {
type: 'ajax',
- url: 'media/musicdb.json'
+ url: 'musicdb.json'
},
sorters: [
{property: 'tracknumber', direction: 'ASC'}

0 comments on commit e9ea979

Please sign in to comment.