#303·dejavu

erors using fingerprint_file

Author: danijel1124Created Dec 30, 2023Updated Apr 2, 2024

hi, when I try to use fingerprint_file I get the following error: " _fingerprint_worker() got an unexpected keyword argument 'song_name' " I just pass one argument to the function. " djv.fingerprint_file(file_path)" From the library source code:

python
        song_name_from_path = decoder.get_audio_name_from_path(file_path)
        song_hash = decoder.unique_hash(file_path)
        song_name = song_name or song_name_from_path```
Here you can see that file_path is necessary, but song_name is optional. So I'm actually doing everything right, I gues...
I've even made sure that the filenames passed in are certainly strings, thought that unexpected things happen... but that don't solves my issue.
Here's just my script, maybe you can do something with it...
```python
import os
import argparse
import datetime
from dejavu import Dejavu

# load config from a JSON file (or anything outputting a python dictionary)
config = {
    "database": {
        "host": "db",
        "user": "postgres",
        "password": "password",
        "database": "dejavu"
    },
    "database_type": "postgres"
}

def read_fingerprinted_files(directory):
    file_list = []
    try:
        with open(os.path.join(directory, 'fingerprinted.txt'), 'r') as f:
            lines = f.readlines()
            file_list = [line.strip() for line in lines[2:]]  # Skip the first two lines (date and 'Files:')
    except FileNotFoundError:
        pass
    return file_list

def write_fingerprinted_file(directory, files, supported_extensions):
    with open(os.path.join(directory, 'fingerprinted.txt'), 'w') as f:
        f.write(f"Fingerprinted on: {datetime.datetime.now()}\n")
        f.write("Files:\n")
        for file in files:
            if os.path.splitext(file)[1].lower() in supported_extensions:
                f.write(f"{file}\n")

def fingerprint_file(djv, file_path):
    try:
        if not isinstance(file_path, str):
            file_path = str(file_path)
        djv.fingerprint_file(file_path)
        print(f"Fingerprinted: {file_path}")
    except Exception as e:
        print(f"Error fingerprinting {file_path}: \n {e}")

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Fingerprint audio files in a directory and its subdirectories.")
    parser.add_argument("directory", help="The path to the directory containing audio files.")
    args = parser.parse_args()

    # Supported audio file extensions: mp3, m4a, wav, etc.
    supported_extensions = [".mp3", ".m4a", ".wav"]

    # create a Dejavu instance
    djv = Dejavu(config)

    # Navigate through the main directory and each subdirectory
    for root, dirs, files in os.walk(args.directory):
        if root != args.directory:  # To avoid fingerprinting the parent directory itself
            existing_files = read_fingerprinted_files(root)
            current_files = [file for file in files if os.path.splitext(file)[1].lower() in supported_extensions]

            if set(existing_files) == set(current_files):
                print(f"Skipping directory (no changes): {root}")
                continue  

            # Fingerprint each file individually
            print(f"Fingerprinting directory: {root}")
            for file in current_files:
                full_path = os.path.join(root, file)
                fingerprint_file(djv, full_path)

            write_fingerprinted_file(root, current_files, supported_extensions)```