#!/usr/bin/env python

# Audio tag filter for Recoll, using mutagen

import sys
import os
import rclexecm

try:
    from mutagen.mp3 import MP3
    from mutagen.easyid3 import EasyID3
    from mutagen.flac import FLAC
    from mutagen.oggvorbis import OggVorbis
except:
    print("RECFILTERROR HELPERNOTFOUND python:mutagen")
    sys.exit(1);

# prototype for the html document we're returning
htmltemplate = '''
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <meta name="album" content="%s">
      <meta name="author" content="%s">
      <meta name="title" content="%s">
   </head>
   <body>
   %s
   </body>
</html>
'''

# mp3:      album, title, artist, genre, date, tracknumber
# flac:     album, title, artist, genre, xxx, tracknumber
# oggvorbis:album, title, artist, genre, date, tracknumber
class AudioTagExtractor:
    def __init__(self, em):
        self.em = em
        self.currentindex = 0

    def extractone(self, params):
        #self.em.rclog("extractone %s %s" % (params["filename:"], params["mimetype:"]))
        docdata = ""
        ok = False
        if not "mimetype:" in params or not "filename:" in params:
            self.em.rclog("extractone: no mime or file name")
            return (ok, docdata, "", rclexecm.RclExecM.eofnow)
        filename = params["filename:"]
        mimetype = params["mimetype:"]
        try:
            if mimetype == b'audio/mpeg':
                tags = MP3(filename, ID3=EasyID3)
            elif mimetype == b'application/ogg' or \
                     mimetype == b'audio/x-vorbis+ogg':
                tags = OggVorbis(filename)
            elif mimetype == b'application/x-flac' or \
                     mimetype == 'audio/x-flac' or \
                     mimetype == b'audio/flac':
                tags = FLAC(filename)
            else:
                raise Exception("Bad mime type %s" % mimetype)
        except Exception as err:
            self.em.rclog("extractone: extract failed: [%s]" % err)
            return (ok, docdata, "", rclexecm.RclExecM.eofnow)

        album = ""
        artist = ""
        title = ""
        try:
            album = self.em.htmlescape(tags["album"][0])
        except:
            pass
        try:
            artist = self.em.htmlescape(tags["artist"][0])
        except:
            pass
        try:
            title = self.em.htmlescape(tags["title"][0])
        except:
            pass
        self.em.setmimetype("text/html")
        alldata = self.em.htmlescape(tags.pprint())
        alldata = alldata.replace("\n", "<br>")
        docdata = (htmltemplate % (album, artist, title, alldata))\
                  .encode('UTF-8')
        ok = True
        return (ok, docdata, "", rclexecm.RclExecM.eofnext)

    ###### File type handler api, used by rclexecm ---------->
    def openfile(self, params):
        self.currentindex = 0
        return True

    def getipath(self, params):
        return self.extractone(params)
        
    def getnext(self, params):
        if self.currentindex >= 1:
            return (False, "", "", rclexecm.RclExecM.eofnow)
        else:
            ret= self.extractone(params)
            self.currentindex += 1
            return ret

def makeObject():
    print("makeObject");
    proto = rclexecm.RclExecM()
    print("makeObject: rclexecm ok");
    extract = AudioTagExtractor(proto)
    return 17

if __name__ == '__main__':
    proto = rclexecm.RclExecM()
    extract = AudioTagExtractor(proto)
    rclexecm.main(proto, extract)
