Restyle Remove global lists and read everything from the database. (#71)

This commit is contained in:
restyled-io[bot] 2022-07-06 17:41:01 +03:00 committed by GitHub
parent 73891aa5cf
commit e77f2b9f2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 97 additions and 70 deletions

View File

@ -3,7 +3,6 @@ This module contains all the Flask Blueprints and API routes. It also contains a
that are used through-out the app. It handles the initialization of the watchdog,
checking and creating config dirs and starting the re-indexing process using a background thread.
"""
from app import functions
from app import helpers
from app import prep

View File

@ -10,7 +10,6 @@ from flask import Blueprint
artist_bp = Blueprint("artist", __name__, url_prefix="/")
# @artist_bp.route("/artist/<artist>")
# @cache.cached()
# def get_artist_data(artist: str):

View File

@ -1,10 +1,10 @@
"""
This file contains the Playlists class for interacting with the playlist documents in MongoDB.
"""
from app import helpers
from app.db.mongodb import convert_many
from app.db.mongodb import convert_one
from app.db.mongodb import MongoPlaylists
from app import helpers
from bson import ObjectId
@ -18,8 +18,12 @@ class Playlists(MongoPlaylists):
Inserts a new playlist object into the database.
"""
return self.collection.update_one(
{"name": playlist["name"]},
{"$set": playlist},
{
"name": playlist["name"]
},
{
"$set": playlist
},
upsert=True,
).upserted_id
@ -45,7 +49,9 @@ class Playlists(MongoPlaylists):
return self.collection.update_one(
{"_id": ObjectId(playlistid)},
{"$set": {"lastUpdated": date}},
{"$set": {
"lastUpdated": date
}},
)
def add_track_to_playlist(self, playlistid: str, track: dict) -> None:
@ -56,7 +62,9 @@ class Playlists(MongoPlaylists):
{
"_id": ObjectId(playlistid),
},
{"$push": {"pre_tracks": track}},
{"$push": {
"pre_tracks": track
}},
)
self.set_last_updated(playlistid)

View File

@ -2,7 +2,9 @@
This file contains the AllSongs class for interacting with track documents in MongoDB.
"""
import pymongo
from app.db.mongodb import MongoTracks, convert_many, convert_one
from app.db.mongodb import convert_many
from app.db.mongodb import convert_one
from app.db.mongodb import MongoTracks
from bson import ObjectId
@ -18,9 +20,12 @@ class Tracks(MongoTracks):
"""
Inserts a new track object into the database.
"""
return self.collection.update_one(
{"filepath": song_obj["filepath"]}, {"$set": song_obj}, upsert=True
).upserted_id
return self.collection.update_one({
"filepath": song_obj["filepath"]
}, {
"$set": song_obj
},
upsert=True).upserted_id
def insert_many(self, songs: list):
"""
@ -52,21 +57,33 @@ class Tracks(MongoTracks):
"""
Returns all the songs matching the albums in the query params (using regex).
"""
songs = self.collection.find({"album": {"$regex": query, "$options": "i"}})
songs = self.collection.find(
{"album": {
"$regex": query,
"$options": "i"
}})
return convert_many(songs)
def search_songs_by_artist(self, query: str) -> list:
"""
Returns all the songs matching the artists in the query params.
"""
songs = self.collection.find({"artists": {"$regex": query, "$options": "i"}})
songs = self.collection.find(
{"artists": {
"$regex": query,
"$options": "i"
}})
return convert_many(songs)
def find_song_by_title(self, query: str) -> list:
"""
Finds all the tracks matching the title in the query params.
"""
song = self.collection.find({"title": {"$regex": query, "$options": "i"}})
song = self.collection.find(
{"title": {
"$regex": query,
"$options": "i"
}})
return convert_many(song)
def find_songs_by_album(self, name: str, artist: str) -> list:
@ -80,7 +97,9 @@ class Tracks(MongoTracks):
"""
Returns a sorted list of all the tracks exactly matching the folder in the query params
"""
songs = self.collection.find({"folder": query}).sort("title", pymongo.ASCENDING)
songs = self.collection.find({
"folder": query
}).sort("title", pymongo.ASCENDING)
return convert_many(songs)
def find_songs_by_filenames(self, filenames: list) -> list:
@ -102,8 +121,10 @@ class Tracks(MongoTracks):
Returns a list of all the tracks matching the path in the query params.
"""
return self.collection.count_documents(
{"filepath": {"$regex": f"^{path}", "$options": "i"}}
)
{"filepath": {
"$regex": f"^{path}",
"$options": "i"
}})
def find_songs_by_artist(self, query: str) -> list:
"""
@ -117,8 +138,10 @@ class Tracks(MongoTracks):
Returns a list of all the tracks containing the albumartist in the query params.
"""
songs = self.collection.find(
{"albumartist": {"$regex": query, "$options": "i"}}
)
{"albumartist": {
"$regex": query,
"$options": "i"
}})
return convert_many(songs)
def get_song_by_path(self, path: str) -> dict:
@ -155,13 +178,14 @@ class Tracks(MongoTracks):
songs = self.collection.find({"albumhash": hash})
return convert_many(songs)
def find_track_by_title_artists_album(
self, title: str, artist: str, album: str
) -> dict:
def find_track_by_title_artists_album(self, title: str, artist: str,
album: str) -> dict:
"""
Returns a single track matching the title, artist, and album in the query params.
"""
song = self.collection.find_one(
{"title": title, "artists": artist, "album": album}
)
song = self.collection.find_one({
"title": title,
"artists": artist,
"album": album
})
return convert_one(song)

View File

@ -1,13 +1,12 @@
import time
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from os import scandir
import time
from typing import Tuple
from concurrent.futures import ThreadPoolExecutor
from app.models import Folder
from app.models import Track
from app import instances
from app.models import Folder
from app.models import Track
@dataclass
@ -27,10 +26,14 @@ def get_folder_track_count(path: str) -> int:
def create_folder(dir: Dir) -> Folder:
"""Create a single Folder object"""
folder = {
"name": dir.path.split("/")[-1],
"path": dir.path,
"is_sym": dir.is_sym,
"trackcount": instances.tracks_instance.find_tracks_inside_path_regex(dir.path),
"name":
dir.path.split("/")[-1],
"path":
dir.path,
"is_sym":
dir.is_sym,
"trackcount":
instances.tracks_instance.find_tracks_inside_path_regex(dir.path),
}
return Folder(folder)

View File

@ -11,14 +11,13 @@ from app import exceptions
from app import instances
from app import models
from app import settings
from app.helpers import Get
from app.lib import trackslib
from app.logger import get_logger
from PIL import Image
from PIL import ImageSequence
from werkzeug import datastructures
from app.lib import trackslib
from app.helpers import Get
from app.logger import get_logger
TrackExistsInPlaylist = exceptions.TrackExistsInPlaylist
logg = get_logger()
@ -53,7 +52,8 @@ def create_thumbnail(image: any, img_path: str) -> str:
Creates a 250 x 250 thumbnail from a playlist image
"""
thumb_path = "thumb_" + img_path
full_thumb_path = os.path.join(settings.APP_DIR, "images", "playlists", thumb_path)
full_thumb_path = os.path.join(settings.APP_DIR, "images", "playlists",
thumb_path)
aspect_ratio = image.width / image.height
@ -71,11 +71,13 @@ def save_p_image(file: datastructures.FileStorage, pid: str):
"""
img = Image.open(file)
random_str = "".join(random.choices(string.ascii_letters + string.digits, k=5))
random_str = "".join(
random.choices(string.ascii_letters + string.digits, k=5))
img_path = pid + str(random_str) + ".webp"
full_img_path = os.path.join(settings.APP_DIR, "images", "playlists", img_path)
full_img_path = os.path.join(settings.APP_DIR, "images", "playlists",
img_path)
if file.content_type == "image/gif":
frames = []

View File

@ -22,5 +22,4 @@ def validate_tracks() -> None:
def get_p_track(ptrack):
return instances.tracks_instance.find_track_by_title_artists_album(
ptrack["title"], ptrack["artists"], ptrack["album"]
)
ptrack["title"], ptrack["artists"], ptrack["album"])

View File

@ -11,13 +11,11 @@ class CopyFiles:
"""Copies assets to the app directory."""
def __init__(self) -> None:
files = [
{
"src": "assets",
"dest": os.path.join(settings.APP_DIR, "assets"),
"is_dir": True,
}
]
files = [{
"src": "assets",
"dest": os.path.join(settings.APP_DIR, "assets"),
"is_dir": True,
}]
for entry in files:
src = os.path.join(os.getcwd(), entry["src"])
@ -26,9 +24,7 @@ class CopyFiles:
shutil.copytree(
src,
entry["dest"],
ignore=shutil.ignore_patterns(
"*.pyc",
),
ignore=shutil.ignore_patterns("*.pyc", ),
copy_function=shutil.copy2,
dirs_exist_ok=True,
)

View File

@ -1,20 +1,17 @@
import { createRouter, createWebHashHistory } from "vue-router";
import Home from "@/views/Home.vue";
import FolderView from "@/views/FolderView.vue";
import PlaylistView from "@/views/PlaylistView.vue";
import Playlists from "@/views/Playlists.vue";
import state from "@/composables/state";
import useAStore from "@/stores/pages/album";
import useFStore from "@/stores/pages/folder";
import usePTrackStore from "@/stores/pages/playlist";
import usePStore from "@/stores/pages/playlists";
import AlbumsExplorer from "@/views/AlbumsExplorer.vue";
import AlbumView from "@/views/AlbumView.vue";
import ArtistsExplorer from "@/views/ArtistsExplorer.vue";
import FolderView from "@/views/FolderView.vue";
import Home from "@/views/Home.vue";
import Playlists from "@/views/Playlists.vue";
import PlaylistView from "@/views/PlaylistView.vue";
import SettingsView from "@/views/SettingsView.vue";
import usePStore from "@/stores/pages/playlists";
import usePTrackStore from "@/stores/pages/playlist";
import useFStore from "@/stores/pages/folder";
import useAStore from "@/stores/pages/album";
import state from "@/composables/state";
import { createRouter, createWebHashHistory } from "vue-router";
const routes = [
{

4
todo
View File

@ -7,7 +7,7 @@
- [ ] Add and deploy demo branch
### Client
- [ ] Add processes tab to show running tasks, eg. when tagging files. I have no idea on how to go about it so far. Web sockets?
- [ ] Add processes tab to show running tasks, eg. when tagging files. I have no idea on how to go about it so far. Web sockets?
- [ ] Responsiveness, especially the track list.
- [ ] Make dummy buttons functional.
- [ ] Add settings page (or modal)
@ -21,4 +21,4 @@
- Resolve album page using albumhash instead of album title and artist
- Resolve album page using albumhash instead of album title and artist

View File

@ -1,6 +1,6 @@
import { defineConfig } from "vite";
import svgLoader from 'vite-svg-loader'
import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";
import svgLoader from "vite-svg-loader";
const path = require("path");