Restyle Implement Fuzzy search using rapidfuzz (#60)

This commit is contained in:
restyled-io[bot]
2022-05-26 19:12:04 +03:00
committed by GitHub
parent 1a3a196d7a
commit 2b33fb87a2
15 changed files with 90 additions and 74 deletions
+1 -1
View File
@@ -6,13 +6,13 @@ import urllib
from typing import List
from app import api
from app import helpers
from app import instances
from app import models
from app.lib import taglib
from app.lib import trackslib
from progress.bar import Bar
from tqdm import tqdm
from app import helpers
def get_all_albums() -> List[models.Album]:
+16 -17
View File
@@ -1,26 +1,26 @@
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import Pool
from copy import deepcopy
import os
from os import path
import time
from concurrent.futures import ThreadPoolExecutor
from copy import deepcopy
from multiprocessing import Pool
from os import path
from typing import List
from tqdm import tqdm
from app import api
from app import settings
from app.helpers import create_album_hash, run_fast_scandir
from app.helpers import create_album_hash
from app.helpers import run_fast_scandir
from app.instances import album_instance
from app.instances import tracks_instance
from app.lib import folderslib
from app.lib.albumslib import create_album
from app.lib.albumslib import find_album
from app.lib.taglib import get_tags
from app.logger import Log
from app.models import Album, Track
from app.lib.trackslib import find_track
from app.logger import Log
from app.models import Album
from app.models import Track
from tqdm import tqdm
class Populate:
@@ -95,7 +95,8 @@ class Populate:
folder = tags["folder"]
self.folders.add(folder)
tags["albumhash"] = create_album_hash(tags["album"], tags["albumartist"])
tags["albumhash"] = create_album_hash(tags["album"],
tags["albumartist"])
self.tagged_tracks.append(tags)
api.DB_TRACKS.append(tags)
@@ -168,9 +169,8 @@ class Populate:
for album in tqdm(self.pre_albums, desc="Building albums"):
self.create_album(album)
Log(
f"{self.exist_count} of {len(self.pre_albums)} albums were already in the database"
)
Log(f"{self.exist_count} of {len(self.pre_albums)} albums were already in the database"
)
def create_track(self, track: dict):
"""
@@ -205,9 +205,8 @@ class Populate:
with ThreadPoolExecutor() as executor:
executor.map(self.create_track, self.tagged_tracks)
Log(
f"Added {len(self.tagged_tracks)} new tracks and {len(self.albums)} new albums"
)
Log(f"Added {len(self.tagged_tracks)} new tracks and {len(self.albums)} new albums"
)
def save_albums(self):
"""
+11 -5
View File
@@ -1,13 +1,14 @@
"""
This library contains all the functions related to the search functionality.
"""
from typing import List
from app import api, helpers, models
from app import api
from app import helpers
from app import models
from app.lib import albumslib
from rapidfuzz import fuzz, process
from rapidfuzz import fuzz
from rapidfuzz import process
ratio = fuzz.ratio
wratio = fuzz.WRatio
@@ -34,6 +35,7 @@ class Limit:
class SearchTracks:
def __init__(self, query) -> None:
self.query = query
@@ -55,6 +57,7 @@ class SearchTracks:
class SearchArtists:
def __init__(self, query) -> None:
self.query = query
@@ -100,6 +103,7 @@ class SearchArtists:
class SearchAlbums:
def __init__(self, query) -> None:
self.query = query
@@ -137,6 +141,7 @@ class SearchAlbums:
class GetTopArtistTracks:
def __init__(self, artist: str) -> None:
self.artist = artist
@@ -160,5 +165,6 @@ def get_artists(artist: str) -> List[models.Track]:
Gets all songs with a given artist.
"""
return [
track for track in api.TRACKS if artist.lower() in str(track.artists).lower()
track for track in api.TRACKS
if artist.lower() in str(track.artists).lower()
]
+5 -4
View File
@@ -7,14 +7,14 @@ import time
from app import api
from app import instances
from app import models
from app.helpers import create_album_hash
from app.lib import folderslib
from app.lib.albumslib import create_album, find_album
from app.lib.albumslib import create_album
from app.lib.albumslib import find_album
from app.lib.taglib import get_tags
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
from app.helpers import create_album_hash
class OnMyWatch:
"""
@@ -87,7 +87,8 @@ def remove_track(filepath: str) -> None:
fpath = filepath.replace(fname, "")
try:
trackid = instances.tracks_instance.get_song_by_path(filepath)["_id"]["$oid"]
trackid = instances.tracks_instance.get_song_by_path(
filepath)["_id"]["$oid"]
except TypeError:
print(f"💙 Watchdog Error: Error removing track {filepath} TypeError")
return