removed useless line
[mp3tohtml.git] / mp3tohtml / mp3tohtml.py
1 #!/usr/bin/python2
2
3 # usage: mp3tohtml.py [-h] [--config CONFIG] dir
4 #
5 # Generate an HTML containing information about the MP3 files.
6 #
7 # positional arguments:
8 #  dir              The directory containing the MP3 files
9 #
10 # optional arguments:
11 #  -h, --help       show this help message and exit
12 #  --config CONFIG  The directory containing the configuration
13 #
14 # Last version of this tool can be get from the GIT repository:
15 # http://wpitchoune.net/mp3tohtml
16 #
17 # Released under the terms of the GPLv2 license.
18 # (c) 2016 Jean-Philippe Orsini  <jeanfi@gmail.com>
19
20 import argparse
21 import eyeD3
22 import fnmatch
23 import glob
24 import os
25 import re
26 import sys
27
28 def fma_copyright_to_html(path, copyright):
29     if copyright == "":
30         sys.stderr.write(path + ": no FMA copyright.\n")
31         return ""
32
33     p = re.compile("(.*): (.*)")
34     m = p.match(copyright)
35     if m is not None:
36         name = m.group(1)
37         name = name.replace("Creative Commons", "CC")
38         name = name.replace("Attribution", "BY")
39         name = name.replace("NonCommercial", "NC")
40         name = name.replace("Noncommercial", "NC")
41         name = name.replace("NoDerivatives", "ND")
42         name = name.replace("ShareAlike", "SA")
43         name = name.replace("Share Alike", "SA")
44         name = name.replace("United States", "US")
45         return "<a href=\"" + m.group(2) + "\">" + name + "</a>"
46     else:
47         sys.stderr.write(path + ": invalid FMA copyright: "
48                          + copyright + ".\n");
49         return copyright
50
51 def copy_header(path):
52     header_path = path + "/header.tpl"
53     try:
54         header = open(header_path, "r")
55         for line in header:
56             sys.stdout.write(line)
57     except IOError as err:
58         sys.stderr.write("Failed to read header file "
59                          + header_path
60                          + ": " + err.strerror + "\n")
61         sys.exit(1)
62     header.close()
63
64 def copy_footer(path):
65     footer_path = path + "/footer.tpl"
66     try:
67         footer = open(footer_path, "r")
68         for line in footer:
69             sys.stdout.write(line)
70     except IOError as err:
71         sys.stderr.write("Failed to read footer file "
72                          + footer_path
73                          + ": " + err.strerror + "\n")
74         sys.exit(1)
75     footer.close()
76
77 parser = argparse.ArgumentParser(description='Generate an HTML containing information about the MP3 files.')
78 parser.add_argument('dir', help='The directory containing the MP3 files')
79 parser.add_argument("--config", help="The directory containing the configuration")
80
81 args = parser.parse_args()
82
83 if (args.config is None):
84     cfg_dir = os.path.dirname(sys.argv[0])
85 else:
86     cfg_dir = args.config
87
88 copy_header(cfg_dir)
89
90 files = []
91 for root, dirnames, filenames in os.walk(args.dir):
92     for filename in fnmatch.filter(filenames, '*.mp3'):
93         files.append(os.path.join(root, filename))
94
95 for f in files:
96     tag = eyeD3.Tag()
97
98     tag.link(f)
99
100     print("\t<tr>")
101
102     print("\t\t<td>" + tag.getArtist().encode("UTF8") + "</td>\n")
103     print("\t\t<td>" + tag.getAlbum().encode("UTF-8") + "</td>")
104     print("\t\t<td>" + tag.getTitle().encode("UTF-8") + "</td>")
105
106     comments = tag.getComments()
107     strComments = ""
108     for c in comments:
109         strComments += c.comment
110
111     p = re.compile("URL: (.*)\r\nComments: (.*)\r\nCurators?: (.*)\r\nCopyright: (.*)")
112     m = p.match(strComments)
113
114     if m is None:
115         url = ""
116         curator = ""
117         copyright = ""
118     else:
119         if m.group(2) == "http://freemusicarchive.org/":
120             strComments = ""
121             url = "<a href=\"" + m.group(1) + "\">FMA</a>"
122         else:
123             strComments =  m.group(2)
124             url = "<a href=\"" + m.group(1) + "\">source</a>"
125
126         curator = m.group(3)
127
128         copyright = fma_copyright_to_html(f, m.group(4))
129
130     print("\t\t<td>" + url + "</td>")
131     print("\t\t<td>" + curator + "</td>")
132     print("\t\t<td>" + copyright + "</td>")
133     print("\t\t<td>" + strComments + "</td>")
134
135     print("\t</tr>")
136
137 copy_footer(cfg_dir)