add file watcher

- disable double flask instances
- remove unused files
- play song by id (instead of from nginx)
This commit is contained in:
geoffrey45
2022-02-16 15:02:36 +03:00
parent 1802e02179
commit 1caef9d3a2
9 changed files with 60 additions and 169 deletions
+4 -3
View File
@@ -17,7 +17,8 @@ def create_app():
app.config.from_mapping(config)
cache.init_app(app)
from . import api
app.register_blueprint(api.bp, url_prefix='/')
with app.app_context():
from . import api
app.register_blueprint(api.bp, url_prefix='/')
return app
return app
+19 -7
View File
@@ -1,7 +1,7 @@
import os
import urllib
from typing import List
from flask import Blueprint, request
from flask import Blueprint, request, send_file
from app import functions, instances, helpers, cache
bp = Blueprint("api", __name__, url_prefix="")
@@ -17,6 +17,7 @@ def initialize() -> None:
"""
helpers.create_config_dir()
helpers.check_for_new_songs()
helpers.start_watchdog()
initialize()
@@ -85,8 +86,8 @@ def search_by_title():
artist_obj = {
"name": artist,
"image": "http://0.0.0.0:8900/images/artists/"
+ artist.replace("/", "::")
+ ".webp",
+ artist.replace("/", "::")
+ ".webp",
}
if artist_obj not in artists_dicts:
@@ -135,8 +136,8 @@ def get_albumartists(album, artist):
artist_obj = {
"name": artist,
"image": "http://0.0.0.0:8900/images/artists/"
+ artist.replace("/", "::")
+ ".webp",
+ artist.replace("/", "::")
+ ".webp",
}
final_artists.append(artist_obj)
@@ -270,8 +271,8 @@ def get_album_tracks(title: str, artist: str):
"image": songs[0].image,
"artist": songs[0].albumartist,
"artist_image": "http://127.0.0.1:8900/images/artists/"
+ songs[0].albumartist.replace("/", "::")
+ ".webp",
+ songs[0].albumartist.replace("/", "::")
+ ".webp",
}
return {"songs": songs, "info": album_obj}
@@ -283,3 +284,14 @@ def get_album_bio(title, artist):
"""Returns the album bio for the given album."""
bio = functions.get_album_bio(title, artist)
return {"bio": bio}, 200
@bp.route("/file/<track_id>")
def send_track_file(track_id):
"""
Returns an audio file that matches the passed id to the client.
"""
filepath = instances.songs_instance.get_song_by_id(track_id)['filepath']
return send_file(filepath, mimetype="audio/mp3")
+8 -7
View File
@@ -32,6 +32,7 @@ def populate():
"""
start = time.time()
print("\nchecking for new tracks")
files = helpers.run_fast_scandir(helpers.home_dir, [".flac", ".mp3"])[1]
for file in files:
@@ -41,6 +42,7 @@ def populate():
instances.songs_instance.insert_song(tags)
api.all_the_f_music = helpers.get_all_songs()
print("\n check done")
end = time.time()
@@ -251,18 +253,18 @@ def parse_disk_number(audio):
return disk_number
def get_tags(full_path: str) -> dict:
def get_tags(fullpath: str) -> dict:
"""
Returns a dictionary of tags for a given file.
"""
try:
audio = mutagen.File(full_path, easy=True)
audio = mutagen.File(fullpath, easy=True)
except MutagenError:
return None
tags = {
"artists": parse_artist_tag(audio),
"title": parse_title_tag(audio, full_path),
"title": parse_title_tag(audio, fullpath),
"albumartist": parse_album_artist_tag(audio),
"album": parse_album_tag(audio),
"genre": parse_genre_tag(audio),
@@ -271,9 +273,9 @@ def get_tags(full_path: str) -> dict:
"discnumber": parse_disk_number(audio),
"length": round(audio.info.length),
"bitrate": round(int(audio.info.bitrate) / 1000),
"filepath": full_path.replace(helpers.home_dir, ""),
"image": extract_thumb(full_path),
"folder": os.path.dirname(full_path).replace(helpers.home_dir, ""),
"filepath": fullpath,
"image": extract_thumb(fullpath),
"folder": os.path.dirname(fullpath).replace(helpers.home_dir, ""),
}
return tags
@@ -314,7 +316,6 @@ def create_track_class(tags):
tags["artists"],
tags["albumartist"],
tags["album"],
tags["filepath"],
tags["folder"],
tags["length"],
tags["date"],
+13 -9
View File
@@ -9,24 +9,26 @@ from typing import List
import requests
from io import BytesIO
from PIL import Image
from app import instances
from app import functions
from app import watchdoge
home_dir = os.path.expanduser('~') + '/'
app_dir = os.path.join(home_dir, '.musicx')
LAST_FM_API_KEY = "762db7a44a9e6fb5585661f5f2bdf23a"
def background(f):
def background(func):
"""
a threading decorator
use @background above the function you want to run in the background
"""
def background_func(*a, **kw):
threading.Thread(target=f, args=a, kwargs=kw).start()
threading.Thread(target=func, args=a, kwargs=kw).start()
return background_func
@@ -44,6 +46,11 @@ def check_for_new_songs():
time.sleep(300)
@background
def start_watchdog():
watchdoge.watch.run()
def run_fast_scandir(_dir: str, ext: list):
"""
Scans a directory for files with a specific extension. Returns a list of files and folders in the directory.
@@ -89,7 +96,7 @@ def remove_duplicates(array: list) -> list:
def save_image(url: str, path: str) -> None:
"""
Saves an image from a url to a path.
Saves an image from an url to a path.
"""
response = requests.get(url)
@@ -138,14 +145,11 @@ def get_all_songs() -> List:
tracks = []
for track in instances.songs_instance.get_all_songs():
track = functions.create_track_class(track)
try:
os.chmod(os.path.join(home_dir, track.filepath), 0o755)
os.chmod(os.path.join(track["filepath"]), 0o755)
except FileNotFoundError:
print("")
instances.songs_instance.remove_song_by_filepath(track.filepath)
instances.songs_instance.remove_song_by_filepath(track['filepath'])
tracks.append(track)
tracks.append(functions.create_track_class(track))
return tracks
-1
View File
@@ -202,7 +202,6 @@ class Track:
artists: str
albumartist: str
album: str
filepath: str
folder: str
length: int
date: int
+12 -6
View File
@@ -4,9 +4,8 @@ import os
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class OnMyWatch:
watchDirectory = "/home/cwilvx/Music"
directory = "/home/cwilvx/Music"
def __init__(self):
self.observer = Observer()
@@ -14,8 +13,10 @@ class OnMyWatch:
def run(self):
event_handler = Handler()
self.observer.schedule(
event_handler, self.watchDirectory, recursive=True)
event_handler, self.directory, recursive=True
)
self.observer.start()
try:
while True:
time.sleep(5)
@@ -38,21 +39,26 @@ def create_thumb_dir(filepath):
class Handler(PatternMatchingEventHandler):
def __init__(self):
print("💠 started watchxx")
PatternMatchingEventHandler.__init__(
self, patterns=['*.flac', '*.mp3'], ignore_directories=True, case_sensitive=False)
def on_created(self, event):
print("🔵 created +++")
print(event.src_path)
create_thumb_dir(event.src_path)
def on_deleted(self, event):
print("🔴 deleted ---")
print(event.src_path)
def on_moved(self, event):
print("🔘 moved -->")
print(event.src_path)
print(event.dest_path)
if __name__ == '__main__':
watch = OnMyWatch()
watch.run()
# if __name__ == '__main__':
watch = OnMyWatch()
# watch.run()