escape special chars in count_documents regex

- thanks github copilot ❤️
This commit is contained in:
geoffrey45 2022-07-07 08:37:02 +03:00
parent 27605facf2
commit c8e4529e3c
2 changed files with 23 additions and 48 deletions

View File

@ -1,6 +1,7 @@
"""
This file contains the AllSongs class for interacting with track documents in MongoDB.
"""
import re
import pymongo
from app.db.mongodb import convert_many
from app.db.mongodb import convert_one
@ -20,12 +21,9 @@ 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):
"""
@ -57,33 +55,21 @@ 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:
@ -97,9 +83,7 @@ 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:
@ -120,11 +104,9 @@ 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"
}})
regex = re.compile(r"^.*" + re.escape(path) + r".*$")
return self.collection.count_documents({"filepath": {"$regex": regex}})
def find_songs_by_artist(self, query: str) -> list:
"""
@ -138,10 +120,8 @@ 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:
@ -178,14 +158,13 @@ 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

@ -26,14 +26,10 @@ 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)