tmp
This commit is contained in:
parent
366860a1ad
commit
22e19557d8
5 changed files with 162 additions and 7 deletions
97
libResolver.py
Normal file
97
libResolver.py
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
import common
|
||||
import os
|
||||
import fnmatch
|
||||
import json
|
||||
from fuzzywuzzy import fuzz
|
||||
|
||||
|
||||
def get_local_library(root_dir):
|
||||
music_extensions = ('*.mp3', '*.flac', '*.wav', '*.aac', '*.ogg', '*.m4a')
|
||||
music_files_map = {}
|
||||
for dir_path, dir_names, filenames in os.walk(root_dir):
|
||||
for pattern in music_extensions:
|
||||
for filename in fnmatch.filter(filenames, pattern):
|
||||
song_name = os.path.splitext(filename)[0]
|
||||
relative_path = os.path.relpath(os.path.join(dir_path, filename), root_dir)
|
||||
music_files_map[song_name] = relative_path
|
||||
return music_files_map
|
||||
|
||||
|
||||
def get_matched(pattern, patterns):
|
||||
result = {}
|
||||
for target_pattern in patterns:
|
||||
similarity_score = fuzz.token_set_ratio(pattern, target_pattern)
|
||||
result[target_pattern] = similarity_score / 100
|
||||
|
||||
sorted_results = sorted(result.items(), key=lambda item: item[1], reverse=True)
|
||||
return sorted_results
|
||||
|
||||
|
||||
def resolve_tracks(source_patterns, target_patterns):
|
||||
from tqdm import tqdm
|
||||
|
||||
pattern_map = {}
|
||||
|
||||
total = len(source_patterns)
|
||||
with tqdm(total=total, desc='Progress') as pbar:
|
||||
for pattern in source_patterns:
|
||||
res = get_matched(pattern, target_patterns)
|
||||
matched = {"score": 0, "items": []}
|
||||
if len(res) and res[0][1] > 0.9:
|
||||
matched['items'] = res[0:min(len(res), 2)]
|
||||
matched['score'] = res[0][1]
|
||||
pattern_map[pattern] = matched
|
||||
pbar.update(1)
|
||||
|
||||
return pattern_map
|
||||
|
||||
|
||||
def resolver1(local_library):
|
||||
track_descriptions = common.get_data("tracks")
|
||||
|
||||
source_patterns = []
|
||||
for track in track_descriptions:
|
||||
artists = common.get_artists_str(track['track']['artists'])
|
||||
pattern = f"{track['track']['name']} {artists}"
|
||||
source_patterns.append(pattern)
|
||||
|
||||
target_patterns = [local for _, local in local_library.items()]
|
||||
|
||||
pattern_map = resolve_tracks(source_patterns, target_patterns)
|
||||
|
||||
with open('prefetched/resolved.json', 'w') as file:
|
||||
json.dump(pattern_map, file, indent=2)
|
||||
|
||||
|
||||
def resolver2(local_library):
|
||||
found_tracks = common.get_data("local_library_on_spotify")
|
||||
track_descriptions = common.get_data("tracks")
|
||||
|
||||
user_tracks_ids = {track['track']['id']: track for track in track_descriptions}
|
||||
|
||||
resolved_map = {}
|
||||
|
||||
for name, found in found_tracks.items():
|
||||
if not len(found):
|
||||
continue
|
||||
track_id = found[0]['id']
|
||||
if track_id in user_tracks_ids:
|
||||
resolved_map[name] = track_id
|
||||
|
||||
with open('prefetched/resolved2.json', 'w') as file:
|
||||
json.dump(resolved_map, file, indent=2)
|
||||
|
||||
def resolve(root_directory):
|
||||
local_library = get_local_library(root_directory)
|
||||
with open('prefetched/local_library.json', 'w') as file:
|
||||
json.dump(local_library, file, indent=2)
|
||||
|
||||
resolver2(local_library)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# root_directory = "/mnt/main/data/music/raw"
|
||||
#root_directory = input("root: ")
|
||||
resolve("/mnt/main/data/music/raw")
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue