-
Notifications
You must be signed in to change notification settings - Fork 0
/
relocate.py
executable file
·123 lines (105 loc) · 3.14 KB
/
relocate.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
#
# Copyright (C) 2018 Juan Pedro Bolivar Puente
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the MIT License, as detailed in the LICENSE
# file located at the root of this source code distribution,
# or here: <https://github.com/arximboldi/lager/blob/master/LICENSE>
#
# relocate.py
# -----------
#
# Goes through every file that is referenced in playlists and that is
# missing, and tries to find other entries that are similar and could
# replace it, using heuristics (artist name, track name, etc.), not
# necessarily the same file...
import os.path
import re
import sys
import shutil
import pathlib
import logging
import sqlite3
logger = logging.getLogger(__name__)
def relocate_file(db, tid):
all_attrs = db.execute('''
SELECT *
FROM library
WHERE id=?
''', (tid,)).fetchone()
if all_attrs == None:
logger.error("Pretty bad, no track found for ID: %s", tid)
return
location, artist, title, bpm = db.execute('''
SELECT location, artist, title, bpm
FROM library
WHERE id=?
''', (tid,)).fetchone()
path_str, = db.execute('''
SELECT location
FROM track_locations
WHERE id=?
''', (location,)).fetchone()
src_file=pathlib.Path(path_str)
if src_file.exists():
return
logger.info("relocating file: %s", path_str)
matches = db.execute('''
SELECT *
FROM library
WHERE id<>? AND artist LIKE ? AND title LIKE ?
''', (tid, artist, title))
best_match = None
best_match_rank = 0
for match in matches:
logger.debug("considering: %s", match)
rank = [a == b for a, b in zip(match, all_attrs)].count(True)
if rank > best_match_rank:
best_match_rank = rank
best_match = match
if not best_match:
logger.warning("no alternative found for: %s", path_str)
return
new_location, = db.execute('''
SELECT location
FROM library
WHERE id=?
''', (best_match[0],)).fetchone()
new_path_str, = db.execute('''
SELECT location
FROM track_locations
WHERE id=?
''', (new_location,)).fetchone()
logger.debug("best match: %s", best_match)
logger.info("new path: %s", new_path_str)
db.execute('''
UPDATE library SET location=? WHERE id=?
''', (new_location, tid))
def relocate_playlist(db, pid, name):
tracks = db.execute('''
SELECT track_id, position
FROM PlaylistTracks
WHERE playlist_id=?
ORDER BY position
''', (pid,))
logger.info("relocating playlist: %s", name)
for tid, tpos in tracks:
relocate_file(db, tid)
def main():
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
logger.info("copying database")
shutil.copyfile('./mixxxdb.sqlite',
'./mixxxdb.fixed.sqlite')
db = sqlite3.connect('mixxxdb.fixed.sqlite')
playlists = db.execute('''
SELECT id, name
FROM Playlists
WHERE hidden = 0
''')
for pid, name in playlists:
relocate_playlist(db, pid, name)
logger.info("committing")
db.commit()
if __name__ == '__main__':
main()