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