cleanup
[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 def copy_footer(path):
52     footer_path = os.path.dirname(path) + "/header.tpl"
53     footer = open(footer_path, "r")
54     for line in footer:
55         sys.stdout.write(line)
56
57 parser = argparse.ArgumentParser(description='Generate an HTML containing information about the MP3 files.')
58 parser.add_argument('dir', help='The directory containing the MP3 files')
59
60 args = parser.parse_args()
61
62 cfg_dir = sys.argv[0]
63
64 copy_header(cfg_dir)
65
66 files = []
67 for root, dirnames, filenames in os.walk(args.dir):
68     for filename in fnmatch.filter(filenames, '*.mp3'):
69         files.append(os.path.join(root, filename))
70
71 for f in files:
72     tag = eyeD3.Tag()
73
74     tag.link(f)
75
76     print("\t<tr>")
77
78     print("\t\t<td>" + tag.getArtist().encode("UTF8") + "</td>\n")
79     print("\t\t<td>" + tag.getAlbum().encode("UTF-8") + "</td>")
80     print("\t\t<td>" + tag.getTitle().encode("UTF-8") + "</td>")
81
82     comments = tag.getComments()
83     strComments = ""
84     for c in comments:
85         strComments += c.comment
86
87     p = re.compile("URL: (.*)\r\nComments: (.*)\r\nCurators?: (.*)\r\nCopyright: (.*)")
88     m = p.match(strComments)
89
90     if m is None:
91         url = ""
92         curator = ""
93         copyright = ""
94     else:
95         if m.group(2) == "http://freemusicarchive.org/":
96             strComments = ""
97             url = "<a href=\"" + m.group(1) + "\">FMA</a>"
98         else:
99             strComments =  m.group(2)
100             url = "<a href=\"" + m.group(1) + "\">source</a>"
101
102         curator = m.group(3)
103
104         copyright = fma_copyright_to_html(f, m.group(4))
105
106     print("\t\t<td>" + url + "</td>")
107     print("\t\t<td>" + curator + "</td>")
108     print("\t\t<td>" + copyright + "</td>")
109     print("\t\t<td>" + strComments + "</td>")
110
111     print("\t</tr>")
112
113 copy_footer(cfg_dir)
114
115 print("</table>")
116 print ("            <ul>")
117 print ("<footer>")
118 print ("                    <li><a href=\"mailto:proxyradio@wpitchoune.net\">Contact</a></li>")
119 print ("            </ul>")
120 print( "</footer>")
121 print( "</body></html>")