#import sqlite3
#from mutagen.mp3 import MP3
#from mutagen.id3 import ID3

# Connect to SQLite database (or create one)
#conn = sqlite3.connect("music_metadata.db")
#cursor = conn.cursor()

# Create a table to store metadata
#cursor.execute("""
#CREATE TABLE IF NOT EXISTS music (
#    id INTEGER PRIMARY KEY AUTOINCREMENT,
#    title TEXT,
#    artist TEXT,
#    album TEXT,
#    year TEXT,
#    genre TEXT
#)
#""")

# Function to extract metadata and store it
#def extract_and_store(mp3_file):
#    audio = MP3(mp3_file, ID3=ID3)
#    tags = audio.tags
#
#    title = tags.get("TIT2", "Unknown").text[0] if "TIT2" in tags else "Unknown"
#    artist = tags.get("TPE1", "Unknown").text[0] if "TPE1" in tags else "Unknown"
#    album = tags.get("TALB", "Unknown").text[0] if "TALB" in tags else "Unknown"
#    year = tags.get("TYER", "Unknown").text[0] if "TYER" in tags else "Unknown"
#    genre = tags.get("TCON", "Unknown").text[0] if "TCON" in tags else "Unknown"
#
#    cursor.execute("INSERT INTO music (title, artist, album, year, genre) VALUES (?, ?, ?, ?, ?)",
#                   (title, artist, album, year, genre))
#    conn.commit()

# Example usage
#extract_and_store("M:/Absolute Elsewhere/In Search Of Ancient Gods/01 - Earthbound.mp3")  # Replace with actual MP3 file path

# Close connection
#conn.close()

#import sqlite3
#import os
#from mutagen.mp3 import MP3
#from mutagen.id3 import ID3

# Connect to SQLite database (or create one)
#conn = sqlite3.connect("music_metadata2.db")
#cursor = conn.cursor()

# Create a table to store metadata
#cursor.execute("""
#CREATE TABLE IF NOT EXISTS music (
#    id INTEGER PRIMARY KEY AUTOINCREMENT,
#    title TEXT,
#    artist TEXT,
#    album TEXT,
#    year TEXT,
#    genre TEXT,
#    track_number TEXT,
#    duration REAL
#)
#""")

#def extract_and_store(mp3_file):
#    """ Extract metadata from an MP3 file and store it in SQLite """
#    try:
#        audio = MP3(mp3_file, ID3=ID3)
#        tags = audio.tags

#        title = tags.get("TIT2", ["Unknown"])[0] if "TIT2" in tags else "Unknown"
#        artist = tags.get("TPE1", ["Unknown"])[0] if "TPE1" in tags else "Unknown"
#        album = tags.get("TALB", ["Unknown"])[0] if "TALB" in tags else "Unknown"
#        year = tags.get("TYER", ["Unknown"])[0] if "TYER" in tags else "Unknown"
#        genre = tags.get("TCON", ["Unknown"])[0] if "TCON" in tags else "Unknown"
#        track_number = tags.get("TRCK", ["Unknown"])[0] if "TRCK" in tags else "Unknown"
#        duration = round(audio.info.length, 2)  # Get track duration in seconds

#        cursor.execute("""
#        INSERT INTO music (title, artist, album, year, genre, track_number, duration)
#        VALUES (?, ?, ?, ?, ?, ?, ?)
#        """, (title, artist, album, year, genre, track_number, duration))

#       conn.commit()

#        print(f"Processed: {mp3_file}")

#    except Exception as e:
#        print(f"Error processing {mp3_file}: {e}")

#def process_directory(directory):
#    """ Process all MP3 files in a given directory """
#    for filename in os.listdir(directory):
#        if filename.endswith(".mp3"):
#            print("Working on " + str(directory) + " - " + str(filename), end="r")
#            extract_and_store(os.path.join(directory, filename))

# Example usage: Update this with your actual folder path
#music_folder = "M:/"
#process_directory(music_folder)

# Close connection
#conn.close()

import sqlite3
import os
from mutagen.mp3 import MP3
from mutagen.id3 import ID3

# Connect to SQLite database (or create one)
conn = sqlite3.connect("music_metadata3.db")
cursor = conn.cursor()

# Create a table to store metadata
cursor.execute("""
CREATE TABLE IF NOT EXISTS music (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT,
    artist TEXT,
    album TEXT,
    year TEXT,
    genre TEXT,
    track_number TEXT,
    duration REAL
)
""")

def extract_and_store(mp3_file):
    """ Extract metadata from an MP3 file and store it in SQLite """
    try:
        audio = MP3(mp3_file, ID3=ID3)
        tags = audio.tags

        title = tags.get("TIT2", ["Unknown"])[0] if "TIT2" in tags else "Unknown"
        artist = tags.get("TPE1", ["Unknown"])[0] if "TPE1" in tags else "Unknown"
        album = tags.get("TALB", ["Unknown"])[0] if "TALB" in tags else "Unknown"
        year = tags.get("TYER", ["Unknown"])[0] if "TYER" in tags else "Unknown"
        genre = tags.get("TCON", ["Unknown"])[0] if "TCON" in tags else "Unknown"
        track_number = tags.get("TRCK", ["Unknown"])[0] if "TRCK" in tags else "Unknown"
        duration = round(audio.info.length, 2)  # Get track duration in seconds

        cursor.execute("""
        INSERT INTO music (title, artist, album, year, genre, track_number, duration)
        VALUES (?, ?, ?, ?, ?, ?, ?)
        """, (title, artist, album, year, genre, track_number, duration))
        conn.commit()

        print(f"Processed: {mp3_file}")

    except Exception as e:
        print(f"Error processing {mp3_file}: {e}")

def process_directory(directory):
    """ Process all MP3 files in a given directory """
    for filename in os.listdir(directory):
        if filename.endswith(".mp3"):
            print("Working on " + str(directory) + " - " + str(filename), end="r")
            extract_and_store(os.path.join(directory, filename))

# Example usage: Update this with your actual folder path
music_folder = "M:/"
process_directory(music_folder)

# Close connection
conn.close()