-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_indexer.py
More file actions
executable file
·57 lines (47 loc) · 2.04 KB
/
file_indexer.py
File metadata and controls
executable file
·57 lines (47 loc) · 2.04 KB
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
import glob2
import taglib
import sqlite3
import os.path
def get_files():
return glob2.glob('Music/**/*.mp3')
def update_db():
db_main = sqlite3.connect('db.sqlite3')
cursor = db_main.cursor()
songs = []
albums = []
artists = []
album_id = 0
artist_id = 0
album_lookup = {}
artist_lookup = {}
for file in get_files():
song = taglib.File(file)
if (not song.tags['ALBUM'][0] in album_lookup):
album_id += 1
album_lookup[song.tags['ALBUM'][0]] = album_id
albums.append((album_id, song.tags['ALBUM'][0]))
if (not song.tags['ARTIST'][0] in artist_lookup):
artist_id += 1
artist_lookup[song.tags['ARTIST'][0]] = artist_id
artists.append((artist_id, song.tags['ARTIST'][0]))
songs.append((album_id, artist_id, os.path.dirname(os.path.realpath(file)) + '/Cover.png',
song.tags['DATE'][0], 0, song.tags['TITLE'][0], song.tags['TRACKNUMBER'][0], file))
cursor.execute('DELETE FROM lister_song')
cursor.executemany(
'INSERT INTO lister_song (album_id, artist_id, image_file, year, rating, title, track_number, path) VALUES(?, ?, ?, ?, ?, ?, ?, ?)', songs)
cursor.execute('DELETE FROM lister_album')
cursor.executemany(
'INSERT INTO lister_album (album_id, description) VALUES(?, ?)', albums)
cursor.execute('DELETE FROM lister_artist')
cursor.executemany(
'INSERT INTO lister_artist (artist_id, description) VALUES(?, ?)', artists)
cursor.execute('''
UPDATE lister_song SET search_key = (SELECT replace(lower(title||al.description||ar.description),' ','')
FROM lister_song so, lister_album al, lister_artist ar
WHERE ar.artist_id = so.artist_id
AND al.album_id = so.album_id
AND lister_song.id = so.id )
''')
db_main.commit()
if __name__ == '__main__':
update_db()