refactoring, cleanup
[mp3tohtml.git] / mp3tohtml / mp3tohtml.py
1 #!/usr/bin/python2
2
3 # http://wpitchoune.net/mp3tohtml
4 # Released under the terms of the GPLv2 license.
5 # (c) 2016 Jean-Philippe Orsini <jeanfi@gmail.com>
6
7 import eyeD3
8 import fnmatch
9 import glob
10 import os
11 import re
12 import sys
13
14 def fma_copyright_to_html(path, copyright):
15     if copyright == "":
16         sys.stderr.write(path + ": no FMA copyright.\n")
17         return ""
18
19     p = re.compile("(.*): (.*)")
20     m = p.match(copyright)
21     if m is not None:
22         name = m.group(1)
23         name = name.replace("Creative Commons", "CC")
24         name = name.replace("Attribution", "BY")
25         name = name.replace("NonCommercial", "NC")
26         name = name.replace("Noncommercial", "NC")
27         name = name.replace("NoDerivatives", "ND")
28         name = name.replace("ShareAlike", "SA")
29         name = name.replace("Share Alike", "SA")
30         name = name.replace("United States", "US")
31         return "<a href=\"" + m.group(2) + "\">" + name + "</a>"
32     else:
33         sys.stderr.write(path + ": invalid FMA copyright: "
34                          + copyright + ".\n");
35         return copyright
36
37 def copy_header(path):
38     header_path = path + "/header.tpl"
39     try:
40         header = open(header_path, "r")
41         for line in header:
42             sys.stdout.write(line)
43         header.close()
44     except IOError as e:
45         sys.stderr.write("Failed to read header file "
46                          + header_path
47                          + ": " + e.strerror + "\n")
48         raise
49
50 def copy_footer(path):
51     footer_path = path + "/footer.tpl"
52     try:
53         footer = open(footer_path, "r")
54         for line in footer:
55             sys.stdout.write(line)
56         footer.close()
57     except IOError as e:
58         sys.stderr.write("Failed to read footer file "
59                          + footer_path
60                          + ": " + e.strerror + "\n")
61         raise
62
63 def generate(mp3_dir, cfg_dir):
64     copy_header(cfg_dir)
65
66     files = []
67     for root, dirnames, filenames in os.walk(mp3_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)