mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
rewrite some for loops with UseBisection
This commit is contained in:
@@ -34,7 +34,7 @@ def initialize() -> None:
|
|||||||
albumslib.create_everything()
|
albumslib.create_everything()
|
||||||
folderslib.run_scandir()
|
folderslib.run_scandir()
|
||||||
playlistlib.create_all_playlists()
|
playlistlib.create_all_playlists()
|
||||||
functions.reindex_tracks()
|
# functions.reindex_tracks()
|
||||||
|
|
||||||
|
|
||||||
initialize()
|
initialize()
|
||||||
|
|||||||
+10
-10
@@ -12,7 +12,7 @@ from app.lib import playlistlib
|
|||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from flask import request
|
from flask import request
|
||||||
|
|
||||||
from app.helpers import create_new_date
|
from app.helpers import UseBisection, create_new_date
|
||||||
|
|
||||||
playlist_bp = Blueprint("playlist", __name__, url_prefix="/")
|
playlist_bp = Blueprint("playlist", __name__, url_prefix="/")
|
||||||
|
|
||||||
@@ -78,13 +78,12 @@ def add_track_to_playlist(playlist_id: str):
|
|||||||
|
|
||||||
@playlist_bp.route("/playlist/<playlistid>")
|
@playlist_bp.route("/playlist/<playlistid>")
|
||||||
def get_single_p_info(playlistid: str):
|
def get_single_p_info(playlistid: str):
|
||||||
for p in api.PLAYLISTS:
|
p = UseBisection(api.PLAYLISTS, "playlistid", [playlistid])()
|
||||||
if p.playlistid == playlistid:
|
playlist: models.Playlist = p[0]
|
||||||
tracks = p.get_tracks()
|
|
||||||
return {
|
if playlist is not None:
|
||||||
"info": serializer.Playlist(p),
|
tracks = playlist.get_tracks()
|
||||||
"tracks": tracks,
|
return {"info": serializer.Playlist(playlist), "tracks": tracks}
|
||||||
}
|
|
||||||
|
|
||||||
return {"info": {}, "tracks": []}
|
return {"info": {}, "tracks": []}
|
||||||
|
|
||||||
@@ -106,9 +105,10 @@ def update_playlist(playlistid: str):
|
|||||||
"thumb": None,
|
"thumb": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
for p in api.PLAYLISTS:
|
p = UseBisection(api.PLAYLISTS, "playlistid", [playlistid])()
|
||||||
if p.playlistid == playlistid:
|
p: models.Playlist = p[0]
|
||||||
|
|
||||||
|
if playlist is not None:
|
||||||
if image:
|
if image:
|
||||||
image_, thumb_ = playlistlib.save_p_image(image, playlistid)
|
image_, thumb_ = playlistlib.save_p_image(image, playlistid)
|
||||||
playlist["image"] = image_
|
playlist["image"] = image_
|
||||||
|
|||||||
+16
-12
@@ -4,9 +4,7 @@ This module contains mini functions for the server.
|
|||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import threading
|
import threading
|
||||||
import time
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Any
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
@@ -62,10 +60,12 @@ def remove_duplicates(tracklist: List[models.Track]) -> List[models.Track]:
|
|||||||
|
|
||||||
while song_num < len(tracklist) - 1:
|
while song_num < len(tracklist) - 1:
|
||||||
for index, song in enumerate(tracklist):
|
for index, song in enumerate(tracklist):
|
||||||
if (tracklist[song_num].title == song.title
|
if (
|
||||||
and tracklist[song_num].album == song.album
|
tracklist[song_num].title == song.title
|
||||||
and tracklist[song_num].artists == song.artists
|
and tracklist[song_num].album == song.album
|
||||||
and index != song_num):
|
and tracklist[song_num].artists == song.artists
|
||||||
|
and index != song_num
|
||||||
|
):
|
||||||
tracklist.remove(song)
|
tracklist.remove(song)
|
||||||
|
|
||||||
song_num += 1
|
song_num += 1
|
||||||
@@ -108,8 +108,7 @@ def check_artist_image(image: str) -> str:
|
|||||||
"""
|
"""
|
||||||
img_name = image.replace("/", "::") + ".webp"
|
img_name = image.replace("/", "::") + ".webp"
|
||||||
|
|
||||||
if not os.path.exists(os.path.join(app_dir, "images", "artists",
|
if not os.path.exists(os.path.join(app_dir, "images", "artists", img_name)):
|
||||||
img_name)):
|
|
||||||
return use_memoji()
|
return use_memoji()
|
||||||
else:
|
else:
|
||||||
return img_name
|
return img_name
|
||||||
@@ -136,10 +135,15 @@ def create_safe_name(name: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
class UseBisection:
|
class UseBisection:
|
||||||
|
"""
|
||||||
|
Uses bisection to find a list of items in another list.
|
||||||
|
|
||||||
def __init__(self, list: List, search_from: str,
|
returns a list of found items with `None` items being not found
|
||||||
queries: List[str]) -> None:
|
items.
|
||||||
self.list = list
|
"""
|
||||||
|
|
||||||
|
def __init__(self, source: List, search_from: str, queries: List[str]) -> None:
|
||||||
|
self.list = source
|
||||||
self.queries = queries
|
self.queries = queries
|
||||||
self.search_from = search_from
|
self.search_from = search_from
|
||||||
self.list.sort(key=lambda x: getattr(x, search_from))
|
self.list.sort(key=lambda x: getattr(x, search_from))
|
||||||
@@ -160,5 +164,5 @@ class UseBisection:
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def __call__(self) -> Any:
|
def __call__(self) -> List:
|
||||||
return [self.find(query) for query in self.queries]
|
return [self.find(query) for query in self.queries]
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ import time
|
|||||||
from app import api
|
from app import api
|
||||||
from app import instances
|
from app import instances
|
||||||
from app import models
|
from app import models
|
||||||
from app.helpers import create_album_hash
|
from app.helpers import UseBisection, create_album_hash
|
||||||
from app.lib import folderslib
|
|
||||||
from app.lib.albumslib import create_album
|
from app.lib.albumslib import create_album
|
||||||
from app.lib.albumslib import find_album
|
from app.lib.albumslib import find_album
|
||||||
from app.lib.taglib import get_tags
|
from app.lib.taglib import get_tags
|
||||||
@@ -71,38 +70,22 @@ def add_track(filepath: str) -> None:
|
|||||||
|
|
||||||
api.TRACKS.append(models.Track(tags))
|
api.TRACKS.append(models.Track(tags))
|
||||||
|
|
||||||
# folder = tags["folder"]
|
|
||||||
|
|
||||||
# if folder not in api.VALID_FOLDERS:
|
|
||||||
# api.VALID_FOLDERS.add(folder)
|
|
||||||
# f = folderslib.create_folder(folder)
|
|
||||||
# api.FOLDERS.append(f)
|
|
||||||
|
|
||||||
|
|
||||||
def remove_track(filepath: str) -> None:
|
def remove_track(filepath: str) -> None:
|
||||||
"""
|
"""
|
||||||
Removes a track from the music dict.
|
Removes a track from the music dict.
|
||||||
"""
|
"""
|
||||||
fname = filepath.split("/")[-1]
|
|
||||||
fpath = filepath.replace(fname, "")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
trackid = instances.tracks_instance.get_song_by_path(
|
trackid = instances.tracks_instance.get_song_by_path(filepath)["_id"]["$oid"]
|
||||||
filepath)["_id"]["$oid"]
|
|
||||||
except TypeError:
|
except TypeError:
|
||||||
print(f"💙 Watchdog Error: Error removing track {filepath} TypeError")
|
print(f"💙 Watchdog Error: Error removing track {filepath} TypeError")
|
||||||
return
|
return
|
||||||
|
|
||||||
instances.tracks_instance.remove_song_by_id(trackid)
|
track = UseBisection(api.TRACKS, "trackid", [trackid])()
|
||||||
|
if track is not None:
|
||||||
for track in api.TRACKS:
|
api.TRACKS.remove(track[0])
|
||||||
if track.trackid == trackid:
|
instances.tracks_instance.remove_song_by_id(trackid)
|
||||||
api.TRACKS.remove(track)
|
|
||||||
|
|
||||||
for folder in api.FOLDERS:
|
|
||||||
if folder.path + "/" == fpath and folder.trackcount - 1 == 0:
|
|
||||||
api.FOLDERS.remove(folder)
|
|
||||||
api.VALID_FOLDERS.remove(folder.path)
|
|
||||||
|
|
||||||
|
|
||||||
class Handler(PatternMatchingEventHandler):
|
class Handler(PatternMatchingEventHandler):
|
||||||
@@ -156,13 +139,12 @@ class Handler(PatternMatchingEventHandler):
|
|||||||
print("⚫ closed ~~~")
|
print("⚫ closed ~~~")
|
||||||
try:
|
try:
|
||||||
self.files_to_process.remove(event.src_path)
|
self.files_to_process.remove(event.src_path)
|
||||||
|
add_track(event.src_path)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
"""
|
"""
|
||||||
The file was already removed from the list, or it was not in the list to begin with.
|
The file was already removed from the list, or it was not in the list to begin with.
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
add_track(event.src_path)
|
|
||||||
|
|
||||||
|
|
||||||
watch = OnMyWatch()
|
watch = OnMyWatch()
|
||||||
|
|||||||
Reference in New Issue
Block a user