uho/mk_music_library_db.py

124 lines
4.2 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2024-01-21 01:29:33 +01:00
# CREATE MUSIC DATABASE ------------------------------------------
import sys, os, datetime, fnmatch, glob, random, time, pathlib, re
from datetime import timedelta
from os.path import join
from tinytag import TinyTag
from random import shuffle
import sqlite3, json
import uuid
2024-01-19 01:50:32 +01:00
#import pypika # sql query builder
from pypika import Query, Table, Field, Column
conn = ''
music_library_path = "/home/rob/antena/music/"
conn = sqlite3.connect("database/show.db")
#TODO fix getting bandcamp urls into db
labelnames = [("sploh", "https://sploh.bandcamp.com"), ("terraformer"), ("pharamafabric") ]
2024-01-19 01:50:32 +01:00
# todo find album with no album name
mus_lib = Query \
.create_table("MUSIC_LIBRARY") \
.columns(
Column("id", "INT", nullable=True),
Column("label", "VARCHAR(100)", nullable=True),
Column("album", "VARCHAR(100)", nullable=True),
Column("track", "VARCHAR(200)", nullable=True),
Column("artist", "VARCHAR(120)", nullable=True),
Column("genre", "VARCHAR(120)", nullable=True),
Column("trackdur", "FLOAT", nullable=True),
Column("year", "INT", nullable=True),
Column("url", "VARCHAR(120)", nullable=True),
Column("path", "VARCHAR(120)", nullable=False),
Column("lastplay", "DATETINE", nullable=True),
Column("comment", "VARCHAR(120)", nullable=True))\
.unique("path") \
.primary_key("path")
def database_create(conn):
# the MUSIC LIBRARY TABLE
#TODO UNIQUE constraint prevents duplicats but what if tracks are in another locaton?fixme
2024-01-19 01:50:32 +01:00
conn.execute(str(mus_lib))
print('''MUSIC LIBRARY Table created successfully''');
2024-01-21 01:29:33 +01:00
#conn = ''
def database_create_episodes_table(conn):
# the show database
# conn = sqlite3.connect("database/show.db")
#TODO put the below back as a raw quiry to make if not exists possible
q = Query \
.create_table("EPISODES") \
.columns(
Column("id", "INT", nullable=True),
Column("episode", "INT", nullable=True),
Column("date", 'DATETIME', nullable=True),
Column("album", "VARCHAR(200)", nullable=True),
Column("track", "VARCHAR(120)", nullable=True),
Column("artist", "VARCHAR(120)", nullable=True),
Column("trackdur", "FLOAT", nullable=True),
Column("genre", "VARCHAR(120)", nullable=True),
Column("year", 'DATETIME', nullable=True),
Column("path", "VARCHAR(120)", nullable=False))\
.unique("path") \
.primary_key("path")
#TODO get the unique path back into action find bug
conn.execute(str(q))
print("EPISODES Table created successfully");
def mk_db_entry(conn):
2024-01-19 01:50:32 +01:00
print("ADDING TRACKS TO DATABASE > > > one moment please! ");
label_url = "https://fixme.bandcamp.com/"
2024-01-19 01:50:32 +01:00
for subdir, dirs, files in os.walk(music_library_path):
2024-01-19 01:50:32 +01:00
for file in files:
for m in [".flac", ".FLAC", ".mp3"]: # get audio files
if m in file:
filepath = pathlib.Path(subdir + "/" +file)
label = filepath.parts[5] # get LABEL etc name from path
track = TinyTag.get(os.path.join(subdir, file))# get metadata from file
cursor = conn.cursor()
2024-01-19 01:50:32 +01:00
mus_lib = Table('MUSIC_LIBRARY')
q = mus_lib.insert(1, label, track.album, track.title, track.artist, \
track.genre, track.duration, track.year, \
label_url, str(filepath), "last_played", track.comment)
cursor.execute(str(q));
conn.commit()
2024-01-19 01:50:32 +01:00
def count_tracks(conn):
query = f"SELECT COUNT(*) FROM MUSIC_LIBRARY"
cursor = conn.cursor()
cursor.execute(query)
result = cursor.fetchone()
row_count = result[0]
print(f'''\n WHOOP! THE MUSIC LIBRARY CONTAINS {row_count} TRACKS!!
--------------------------------------------''')
2024-01-19 01:50:32 +01:00
def main():
database_create(conn)
2024-01-21 01:29:33 +01:00
database_create_episodes_table(conn)
2024-01-19 01:50:32 +01:00
mk_db_entry(conn)
count_tracks(conn)
if __name__ == "__main__":
main()
2024-01-19 01:50:32 +01:00