Example #1
0
File: cache.py Project: blais/pyofa
    def getpuid(self, fn, musicdns_key):
        fn = abspath(fn)

        # Lookup puid from the fingerprint.
        try:
            duration, fingerprint, puid = self.dbm[fn]
        except KeyError:
            duration = None
            fingerprint = None
            puid = None

        if fingerprint is None or duration is None:

            # Get the fingerprint and duration.
            try:
                duration, fingerprint, puid = self.dbm[fn]
            except KeyError:
                fingerprint, duration = musicdns.create_fingerprint(fn)
                self.dbm[fn] = duration, fingerprint, puid
                self.dbm.sync()

        if puid is None:
            assert duration is not None
            puid = musicdns.lookup_fingerprint(fingerprint, duration,
                                               musicdns_key)
            self.dbm[fn] = duration, fingerprint, puid
            self.dbm.sync()

        return puid, duration
Example #2
0
def askMusicDNS(filePath):
    """Fingerprint audio file; look for match in MusicDNS database.
    
    filePath must point to an audio file with a supported format (MP3 or OGG).

    This functions uses OFA (the Open Fingerprint Architecture) accessed via 
    the pyofa wrapper to generate an audio fingerprint for the given file.
    Then it queries MusicDNS to see if the fingerprint matches a known song.
    If so, it returns a dictionary of metadata including the PUID and (if found) 
    the artist, song title, genre and year of first release.    . 
    If the process fails for any reason, it returns None."""
    
    if not musicdns:
        log("Cannot fingerprint; pyofa is not installed.")
        return None
    
    fileName = os.path.basename(filePath)
    #try:
    filePath = toUnicode(filePath).encode("UTF-8")
    #except:
    #    log("Filepaths to be fingerprinted must contain only ASCII chars.")
    #    log("Bad filepath: %s" % filePath)
    #    return None
    
    log("Generating an audio fingerprint for %s." % quote(fileName))
    try:
        fingerprint, duration = musicdns.create_fingerprint(filePath)
    except IOError:
        log("%s is not a supported filetype for audio fingerprinting." % 
            quote(fileName))
        return None
    
    log("Searching for a match in the MusicDNS database.")
    try:
        metadata = lookup_fingerprint_metadata(fingerprint, duration, 
                                               "a66a78b0401f53189d5dd98a5c89f5a9")
    except:
        log("Unable to search for MusicDNS match.")
        return None
        
    if metadata["puid"]:
        log("MusicDNS found a match.")
    else:
        log("MusicDNS failed to find a match.") 
        
    return metadata
    def process(self, file):
        global musicdns 
        if not musicdns:
            return file

        if file.get('puid'):
            return file

        if not file.has_key('fname'):
            return file

        if not os.path.exists(file['fname']):
            return file

        try:
            fp = musicdns.create_fingerprint(file['fname'])
            puid = musicdns.lookup_fingerprint(fp[0], fp[1], config['musicdns.key'])
        except Exception, e:
            log.warn("Could not fingerprint %s: %s", file['fname'], e)
            return file #We don't need the fingerprint per say