28
|
1 import os, re
|
|
2 from subprocess import check_call
|
|
3 from tempfile import NamedTemporaryFile
|
|
4 from pymongo import Connection
|
|
5
|
|
6 def makeCode(s):
|
|
7 #return 'code'
|
|
8 psFile = NamedTemporaryFile(suffix='.ps')
|
|
9 check_call(['barcode',
|
|
10 '-b', s,
|
|
11 '-E',
|
|
12 '-o', psFile.name])
|
|
13 svgFile = NamedTemporaryFile(suffix='.svg')
|
|
14 check_call(['pstoedit',
|
|
15 '-f', 'plot-svg',
|
|
16 '-yshift', '580',
|
|
17 '-xshift', '20',
|
|
18 psFile.name, svgFile.name])
|
|
19 lines = open(svgFile.name).readlines()
|
|
20 return ''.join(lines[2:])
|
|
21
|
|
22 def codeElem(s):
|
|
23 return '<div class="bc">%s</div>' % makeCode(s)
|
|
24
|
|
25 mpdPaths = Connection("bang", 27017)['barcodePlayer']['mpdPaths']
|
|
26 # {mpdPath:"music/path/to/album/or/song", "_id":12}
|
|
27 mpdPaths.ensure_index([('mpdPath', 1)])
|
|
28 def idForMpdPath(p):
|
|
29 match = mpdPaths.find_one({"mpdPath" : p})
|
|
30 if match:
|
|
31 return match['_id']
|
|
32
|
|
33 top = list(mpdPaths.find().sort([('_id', -1)]).limit(1))
|
|
34 newId = top[0]['_id'] + 1 if top else 0
|
|
35 mpdPaths.insert({"mpdPath" : p, "_id" : newId})
|
|
36 return newId
|
|
37
|
|
38
|
|
39 out = open("out.xhtml", "w")
|
|
40 out.write("""<?xml version="1.0" encoding="utf-8"?>
|
|
41 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
|
|
42 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
|
43 <html xmlns="http://www.w3.org/1999/xhtml">
|
|
44 <head>
|
|
45 <title>barcodes</title>
|
|
46 <link rel="stylesheet" href="barcode.css"/>
|
|
47 </head>
|
|
48 <body>
|
|
49 <div class="cards">
|
|
50 """)
|
|
51
|
|
52
|
|
53 mpdRoot = "/my/music"
|
|
54
|
|
55 paths = open("mpdPaths.txt").read().strip().splitlines()
|
|
56
|
|
57 cardsSeen = 0
|
|
58 for path in paths:
|
|
59 if os.path.isdir(os.path.join(mpdRoot, path)):
|
|
60 albumDir = path.split('/')[-1]
|
|
61 songFile = None
|
|
62 else:
|
|
63 albumDir, songFile = path.split('/')[-2:]
|
|
64
|
|
65 if '-' in albumDir:
|
|
66 artistName, albumName = albumDir.replace('_', ' ').split('-', 1)
|
|
67 else:
|
|
68 artistName, albumName = '', albumDir
|
|
69
|
|
70 if artistName in ['', 'Original Soundtrack', 'Various']:
|
|
71 artistName = albumName
|
|
72 albumName = ''
|
|
73
|
|
74 if songFile:
|
|
75 songName = re.sub(r'(^\d+\.)|(^\d+\s*-)', '', songFile)
|
|
76 songName = songName.rsplit('.',1)[0].replace('_', ' ')
|
|
77
|
|
78 out.write('<div class="card">')
|
|
79 out.write('<div class="artist">%s</div>' % artistName)
|
|
80 out.write('<div class="album">%s</div>' % albumName)
|
|
81
|
|
82 print (albumName, songName if songFile else '')
|
|
83 out.write(codeElem("music %s" % idForMpdPath(path)))
|
|
84
|
|
85 if songFile:
|
|
86 out.write('<div class="song">%s</div>' % songName)
|
|
87
|
|
88 out.write('</div>')
|
|
89 cardsSeen += 1
|
|
90 if cardsSeen % 8 == 0:
|
|
91 out.write('<div class="pgbr"/>')
|
|
92
|
|
93 out.write("""
|
|
94 </div>
|
|
95 </body>
|
|
96 </html>
|
|
97 """)
|