my-music-list/generatedatabase.py

78 lines
3.4 KiB
Python

import os
import sqlite3
import tinytag
musiclist = []
with open('musiclist.txt', 'r') as f:
for line in f:
if line.strip() == '':
continue
musicname, videoid, provider = line.strip().split('---')
musiclist.append({"musicname": musicname.strip(), "videoid": videoid.strip(), "provider": provider.strip()})
if os.path.exists('/mnt/sdcard/Music'):
download_location = '/mnt/sdcard/Music'
elif os.path.exists('/storage/emulated/0/Music'):
download_location = '/storage/emulated/0/Music'
elif os.path.exists('/sdcard/Music'):
download_location = '/sdcard/Music'
elif os.environ.get('HOME') is not None:
if os.path.exists(f'{os.environ["HOME"]}/Music'):
download_location = f'{os.environ["HOME"]}/Music'
else:
os.mkdir(f'{os.environ["HOME"]}/Music')
download_location = f'{os.environ["HOME"]}/Music'
else:
download_location = os.getcwd() + '/music'
if not os.path.exists(download_location):
print("There is no downloaded songs")
exit()
if not os.path.exists('music.db'):
conn = sqlite3.connect('music.db')
c = conn.cursor()
c.execute('''CREATE TABLE music (listmusicname text, listvideoid text, listprovider text, filelocation text, fileartistname text, filealbumname text, filemusictitle text, fileduration text)''')
conn.commit()
conn.close()
downloaded_music_list = []
for music in os.listdir(download_location):
if music.endswith('.opus'):
downloaded_music_list.append(music.split('.opus')[0])
tagextracted = []
conn = sqlite3.connect('music.db')
c = conn.cursor()
for musicfile in downloaded_music_list:
filelocation = f'{download_location}/{musicfile}.opus'
file = tinytag.TinyTag.get(filelocation)
tagextracted.append({"filename": musicfile, "filemusicname": file.title, "fileartistname": file.artist, "filealbumname": file.album, "fileduration": file.duration, "filelocation": filelocation})
for listmusic in musiclist:
music_name = listmusic['musicname']
music_name = music_name.replace('/', '-')
music_name = music_name.replace('@', '')
music_name = music_name.replace('#', '')
music_name = music_name.replace('%', '')
music_name = music_name.replace('$', '')
music_name = music_name.replace('^', '')
music_name = music_name.replace('?', '')
music_name = music_name.replace('"', '')
music_name = music_name.replace("'", '')
music_name = music_name.replace(':', '')
music_name = music_name.replace('|', '')
music_name = music_name.replace('>', '')
music_name = music_name.replace('<', '')
music_name = music_name.replace('\\', '')
music_name = music_name.replace('&', '')
music_name = music_name.replace('+', '')
music_name = music_name.replace('=', '')
music_name = music_name.replace('~', '')
music_name = music_name.replace('`', '')
music_name = music_name.replace(';', '')
music_name = music_name.replace('*', '')
for a in tagextracted:
if music_name == a['filename']:
c.execute("INSERT INTO music VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (music_name, listmusic["videoid"], listmusic["provider"], a["filelocation"], a["fileartistname"], a["filealbumname"], a["filemusicname"], a["fileduration"]))
conn.commit()
conn.close()