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