cleanup
[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 argparse;
14 import eyeD3
15 import glob
16 import os.path
17 import re
18 import sys
19
20 def fma_copyright_to_html(path, copyright):
21     if copyright == "":
22         sys.stderr.write(path + ": no FMA copyright.\n")
23         return ""
24
25     p = re.compile("(.*): (.*)")
26     m = p.match(copyright)
27     if m is not None:
28         name = m.group(1)
29         name = name.replace("Creative Commons", "CC")
30         name = name.replace("Attribution", "BY")
31         name = name.replace("NonCommercial", "NC")
32         name = name.replace("Noncommercial", "NC")
33         name = name.replace("NoDerivatives", "ND")
34         name = name.replace("ShareAlike", "SA")
35         name = name.replace("Share Alike", "SA")
36         name = name.replace("United States", "US")
37         return "<a href=\"" + m.group(2) + "\">" + name + "</a>"
38     else:
39         sys.stderr.write(path + ": invalid FMA copyright: "
40                          + copyright + ".\n");
41         return copyright
42
43 def copy_header(path):
44     header_path = os.path.dirname(path) + "/header.tpl"
45     header = open(header_path, "r")
46     for line in header:
47         sys.stdout.write(line)
48
49 parser = argparse.ArgumentParser(description='Generate an HTLM containing information about the MP3 files.')
50 parser.add_argument('dir', help='The directory containing the MP3 files')
51
52 args = parser.parse_args()
53
54 copy_header(sys.argv[0])
55
56 files = glob.iglob(args.dir + "/**/*mp3")
57
58 for f in files:
59     tag = eyeD3.Tag()
60
61     tag.link(f)
62
63     print("\t<tr>")
64
65     print("\t\t<td>" + tag.getArtist().encode("UTF8") + "</td>\n")
66     print("\t\t<td>" + tag.getAlbum().encode("UTF-8") + "</td>")
67     print("\t\t<td>" + tag.getTitle().encode("UTF-8") + "</td>")
68
69     comments = tag.getComments()
70     strComments = ""
71     for c in comments:
72         strComments += c.comment
73
74     p = re.compile("URL: (.*)\r\nComments: (.*)\r\nCurators?: (.*)\r\nCopyright: (.*)")
75     m = p.match(strComments)
76
77     if m is None:
78         url = ""
79         curator = ""
80         copyright = ""
81     else:
82         if m.group(2) == "http://freemusicarchive.org/":
83             strComments = ""
84             url = "<a href=\"" + m.group(1) + "\">FMA</a>"
85         else:
86             strComments =  m.group(2)
87             url = "<a href=\"" + m.group(1) + "\">source</a>"
88
89         curator = m.group(3)
90
91         copyright = fma_copyright_to_html(f, m.group(4))
92
93     print("\t\t<td>" + url + "</td>")
94     print("\t\t<td>" + curator + "</td>")
95     print("\t\t<td>" + copyright + "</td>")
96     print("\t\t<td>" + strComments + "</td>")
97
98     print("\t</tr>")
99
100 print("</table>")
101 print ("            <ul>")
102 print ("<footer>")
103 print ("                    <li><a href=\"mailto:proxyradio@wpitchoune.net\">Contact</a></li>")
104 print ("            </ul>")
105 print( "</footer>")
106 print( "</body></html>")