mirror of
https://github.com/tcsenpai/swingmusic.git
synced 2025-06-07 03:35:35 +00:00
handle watchdog events
- handle on_created event - handle on_moved event - handle on_deleted event - watch whole ~ directory
This commit is contained in:
parent
8b25fc4844
commit
dcfb0a6ed1
@ -1,20 +1,23 @@
|
||||
from pprint import pprint
|
||||
import time
|
||||
import os
|
||||
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import PatternMatchingEventHandler
|
||||
|
||||
from app import instances, functions
|
||||
from app import api
|
||||
|
||||
|
||||
class OnMyWatch:
|
||||
directory = "/home/cwilvx/Music"
|
||||
directory = os.path.expanduser("~")
|
||||
|
||||
def __init__(self):
|
||||
self.observer = Observer()
|
||||
|
||||
def run(self):
|
||||
event_handler = Handler()
|
||||
self.observer.schedule(
|
||||
event_handler, self.directory, recursive=True
|
||||
)
|
||||
self.observer.schedule(event_handler, self.directory, recursive=True)
|
||||
self.observer.start()
|
||||
|
||||
try:
|
||||
@ -27,36 +30,76 @@ class OnMyWatch:
|
||||
self.observer.join()
|
||||
|
||||
|
||||
def create_thumb_dir(filepath):
|
||||
f_name = filepath.split('/')[-1]
|
||||
parent_dir = filepath.replace(f_name, '')
|
||||
def add_track(filepath: str) -> None:
|
||||
"""
|
||||
Processes the audio tags for a given file ands add them to the music dict.
|
||||
"""
|
||||
tags = functions.get_tags(filepath)
|
||||
|
||||
thumb_dir = parent_dir + ".thumbnails"
|
||||
if tags is not None:
|
||||
print("🔵: tags okay")
|
||||
track_id = instances.songs_instance.insert_song(tags)
|
||||
track = instances.songs_instance.get_song_by_id(track_id)
|
||||
print(track_id)
|
||||
|
||||
if not os.path.exists(thumb_dir):
|
||||
os.makedirs(thumb_dir)
|
||||
track_obj = functions.create_track_class(track)
|
||||
api.all_the_f_music.append(track_obj)
|
||||
|
||||
|
||||
def remove_track(filepath: str) -> None:
|
||||
"""
|
||||
Removes a track from the music dict.
|
||||
"""
|
||||
track_id = instances.songs_instance.get_song_by_path(filepath)["_id"]["$oid"]
|
||||
instances.songs_instance.remove_song_by_id(track_id)
|
||||
|
||||
for track in api.all_the_f_music:
|
||||
if track.track_id == track_id:
|
||||
pprint(track)
|
||||
api.all_the_f_music.remove(track)
|
||||
|
||||
|
||||
class Handler(PatternMatchingEventHandler):
|
||||
files_to_process = []
|
||||
|
||||
def __init__(self):
|
||||
print("💠 started watchxx")
|
||||
print("💠 started watchdog")
|
||||
PatternMatchingEventHandler.__init__(
|
||||
self, patterns=['*.flac', '*.mp3'], ignore_directories=True, case_sensitive=False)
|
||||
self,
|
||||
patterns=["*.flac", "*.mp3"],
|
||||
ignore_directories=True,
|
||||
case_sensitive=False,
|
||||
)
|
||||
|
||||
def on_created(self, event):
|
||||
"""
|
||||
Fired when a supported file is created.
|
||||
"""
|
||||
print("🔵 created +++")
|
||||
print(event.src_path)
|
||||
create_thumb_dir(event.src_path)
|
||||
self.files_to_process.append(event.src_path)
|
||||
|
||||
def on_deleted(self, event):
|
||||
"""
|
||||
Fired when a delete event occurs on a supported file.
|
||||
"""
|
||||
print("🔴 deleted ---")
|
||||
print(event.src_path)
|
||||
remove_track(event.src_path)
|
||||
|
||||
def on_moved(self, event):
|
||||
"""
|
||||
Fired when a move event occurs on a supported file.
|
||||
"""
|
||||
print("🔘 moved -->")
|
||||
print(event.src_path)
|
||||
print(event.dest_path)
|
||||
remove_track(event.src_path)
|
||||
add_track(event.dest_path)
|
||||
|
||||
def on_closed(self, event):
|
||||
"""
|
||||
Fired when a created file is closed.
|
||||
"""
|
||||
print("⚫ closed ~~~")
|
||||
self.files_to_process.remove(event.src_path)
|
||||
add_track(event.src_path)
|
||||
|
||||
|
||||
watch = OnMyWatch()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user