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