370cb1fbb2a3eaf3cf139da7b58f53ea9f52aac4
[radio.git] / mp3tohtml.py
1 #!/usr/bin/python
2
3 # Generate an HTML page containing information about MP3s in a
4 # directory.
5 #
6 # Usage: mp3tohtml.py <mp3_dir>
7 #
8 # Last version of this tool can be get from the GIT repository:
9 # http://git.wpitchoune.net/radio.git
10 #
11 # (c) 2016 Jean-Philippe Orsini  <jeanfi@gmail.com>
12
13 import eyeD3
14 import glob
15 import os.path
16 import re
17 import sys
18
19 def fma_copyright_to_html(copyright):
20     p = re.compile("(.*): (.*)")
21     m = p.match(copyright)
22     if m is not None:
23         name = m.group(1)
24         name = name.replace("Creative Commons", "CC")
25         name = name.replace("Attribution", "BY")
26         name = name.replace("NonCommercial", "NC")
27         name = name.replace("NoDerivatives", "ND")
28         name = name.replace("ShareAlike", "SA")
29         return "<a href=\"" + m.group(2) + "\">" + name + "</a>"
30     else:
31         return copyright
32
33 if len(sys.argv) != 2:
34     sys.stderr.write("Usage: mp3tohtml.py <mp3_dir>\n")
35     exit(1)
36
37 header_path = os.path.dirname(sys.argv[0]) + "/header.tpl"
38 header = open(header_path, "r")
39 for line in header:
40     sys.stdout.write(line)
41
42 files = glob.glob(sys.argv[1] + "/**/*mp3")
43
44 for f in files:
45     tag = eyeD3.Tag()
46
47     tag.link(f)
48
49     print "\t<tr>"
50
51     try:
52         artist = tag.getArtist()
53         print "\t\t<td>" + artist + "</td>"
54     except UnicodeEncodeError:
55         print "\t\t<td></td>"
56
57     try:
58         print "\t\t<td>" + tag.getAlbum() + "</td>"
59     except UnicodeEncodeError:
60         print "\t\t<td></td>"
61
62     try:
63         print "\t\t<td>" + tag.getTitle() + "</td>"
64     except UnicodeEncodeError:
65         print "\t\t<td></td>"
66
67
68     comments = tag.getComments()
69     strComments = ""
70     for c in comments:
71         strComments += c.comment
72
73     p = re.compile("URL: (.*)\r\nComments: (.*)\r\nCurators?: (.*)\r\nCopyright: (.*)")
74     m = p.match(strComments)
75
76     if m is None:
77         url = ""
78         curator = ""
79         copyright = ""
80     else:
81         if m.group(2) == "http://freemusicarchive.org/":
82             strComments = ""
83             url = "<a href=\"" + m.group(1) + "\">FMA</a>"
84         else:
85             strComments =  m.group(2)
86             url = "<a href=\"" + m.group(1) + "\">source</a>"
87
88         curator = m.group(3)
89
90         copyright = fma_copyright_to_html(m.group(4))
91
92     print "\t\t<td>" + url + "</td>"
93     print "\t\t<td>" + curator + "</td>"
94     print "\t\t<td>" + copyright + "</td>"
95     print "\t\t<td>" + strComments + "</td>"
96
97     print "\t</tr>"
98
99 print "</table>"
100 print "<footer>"
101 print "            <ul>"
102 print "                    <li><a href=\"mailto:proxyradio@wpitchoune.net\">Contact</a></li>"
103 print "            </ul>"
104 print "</footer>"
105 print "</body></html>"