fixed encoding issue
[radio.git] / mp3tohtml.py
index d2e4529..221269a 100755 (executable)
@@ -1,17 +1,54 @@
 #!/usr/bin/python
 
+# Generate an HTML page containing information about MP3s in a
+# directory.
+#
+# Usage: mp3tohtml.py <mp3_dir>
+#
+# Last version of this tool can be get from the GIT repository:
+# http://git.wpitchoune.net/radio.git
+#
+# (c) 2016 Jean-Philippe Orsini  <jeanfi@gmail.com>
+
+import eyeD3
 import glob
+import os.path
+import re
 import sys
-import eyeD3
+
+def fma_copyright_to_html(path, copyright):
+    if copyright == "":
+        sys.stderr.write(path + ": no FMA copyright.\n")
+        return ""
+
+    p = re.compile("(.*): (.*)")
+    m = p.match(copyright)
+    if m is not None:
+        name = m.group(1)
+        name = name.replace("Creative Commons", "CC")
+        name = name.replace("Attribution", "BY")
+        name = name.replace("NonCommercial", "NC")
+        name = name.replace("Noncommercial", "NC")
+        name = name.replace("NoDerivatives", "ND")
+        name = name.replace("ShareAlike", "SA")
+        name = name.replace("Share Alike", "SA")
+        name = name.replace("United States", "US")
+        return "<a href=\"" + m.group(2) + "\">" + name + "</a>"
+    else:
+        sys.stderr.write(path + ": invalid FMA copyright: "
+                         + copyright + ".\n");
+        return copyright
 
 if len(sys.argv) != 2:
+    sys.stderr.write("Usage: mp3tohtml.py <mp3_dir>\n")
     exit(1)
 
-files = glob.glob(sys.argv[1] + "/**/*mp3")
+header_path = os.path.dirname(sys.argv[0]) + "/header.tpl"
+header = open(header_path, "r")
+for line in header:
+    sys.stdout.write(line)
 
-print "<html><head><title>List of songs</title></head>"
-print "<body><h1>List of songs</h1>"
-print "<table>"
+files = glob.glob(sys.argv[1] + "/**/*mp3")
 
 for f in files:
     tag = eyeD3.Tag()
@@ -20,30 +57,45 @@ for f in files:
 
     print "\t<tr>"
 
-    try: 
-        artist = tag.getArtist().decode('utf-8')
-        print "\t\t<td>" + artist + "</td>"       
-    except UnicodeEncodeError:
-        print "\t\t<td></td>"
+    print "\t\t<td>" + tag.getArtist().encode("UTF8") + "</td>\n"
+    print "\t\t<td>" + tag.getAlbum().encode("UTF-8") + "</td>"
+    print "\t\t<td>" + tag.getTitle().encode("UTF-8") + "</td>"
 
-    try:        
-        print "\t\t<td>" + tag.getAlbum() + "</td>"
-    except UnicodeEncodeError:
-        print "\t\t<td></td>"
-
-    try:
-        print "\t\t<td>" + tag.getTitle() + "</td>"
-    except UnicodeEncodeError:
-        print "\t\t<td></td>"
-
-    print "\t\t<td>"
     comments = tag.getComments()
+    strComments = ""
     for c in comments:
-        print comments[0].comment
-    print "\t\t</td>"
+        strComments += c.comment
+
+    p = re.compile("URL: (.*)\r\nComments: (.*)\r\nCurators?: (.*)\r\nCopyright: (.*)")
+    m = p.match(strComments)
+
+    if m is None:
+        url = ""
+        curator = ""
+        copyright = ""
+    else:
+        if m.group(2) == "http://freemusicarchive.org/":
+            strComments = ""
+            url = "<a href=\"" + m.group(1) + "\">FMA</a>"
+        else:
+            strComments =  m.group(2)
+            url = "<a href=\"" + m.group(1) + "\">source</a>"
+
+        curator = m.group(3)
+
+        copyright = fma_copyright_to_html(f, m.group(4))
+
+    print "\t\t<td>" + url + "</td>"
+    print "\t\t<td>" + curator + "</td>"
+    print "\t\t<td>" + copyright + "</td>"
+    print "\t\t<td>" + strComments + "</td>"
 
     print "\t</tr>"
 
 print "</table>"
-    
+print "<footer>"
+print "            <ul>"
+print "                    <li><a href=\"mailto:proxyradio@wpitchoune.net\">Contact</a></li>"
+print "            </ul>"
+print "</footer>"
 print "</body></html>"