-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.py
61 lines (49 loc) · 2.61 KB
/
fs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import sqlite3
import re
import mutagen
audioFormats = ['mp3', 'flac', 'ogg', 'opus']
def __scanDir(cur, path, relativePath='/'):
if path[-1] != '/':
path += '/'
directory = []
for file in os.scandir(path):
if file.is_dir():
folderName = file.name
directory.append(
__scanDir(cur, f'{path}/{folderName}', relativePath=f'{relativePath}{folderName}/'))
elif re.search('.*\.(.*)$', file.name).group(1) not in audioFormats:
pass
else:
audiofile = mutagen.File(f'{path}{file.name}')
if audiofile is None:
continue
metadata = {
'name': file.name if 'title' not in audiofile else audiofile['title'][0],
'artist': 'Unknown artist' if 'artist' not in audiofile else ', '.join(audiofile['artist']),
'album': 'Unknown album' if 'album' not in audiofile else ', '.join(audiofile['album']),
'genre': 'Unknown' if 'genre' not in audiofile else ', '.join(audiofile['genre']),
'date': 'Unknown' if 'date' not in audiofile else ', '.join(audiofile['date']),
'sample_rate': audiofile.info.sample_rate,
'length': int(audiofile.info.length),
}
cur.execute(
'''INSERT OR IGNORE INTO artists (artist_name) VALUES (?)''', (metadata['artist'],))
cur.execute('''INSERT OR IGNORE INTO albums (album_name, artist_name) VALUES (?, ?)''',
(metadata['album'], metadata['artist'],))
cur.execute('''INSERT OR IGNORE INTO songs (song_name, artist_name, album_name, genre, date, song_file_name, sample_rate, length) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (metadata['name'], metadata['artist'], metadata['album'], metadata['genre'], metadata['date'], relativePath + file.name, metadata['sample_rate'], metadata['length']))
directory.append(relativePath + file.name)
return directory
def scanDir(con, cur, path):
cur = con.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS artists
(artist_id integer primary key, artist_name text, unique(artist_name))
''')
cur.execute('''CREATE TABLE IF NOT EXISTS albums
(album_id integer primary key, album_name text, artist_name text, unique(album_name))
''')
cur.execute('''CREATE TABLE IF NOT EXISTS songs
(song_id integer primary key, song_name text, artist_name text, album_name text, genre text, date text, song_file_name text, sample_rate integer, length integer, unique(song_name))''')
__scanDir(cur, path)
con.commit()