make new file detection be based on filename, instead of age

This commit is contained in:
Tomasz Frątczak 2024-02-16 14:35:33 +01:00
parent 5ca0805b10
commit fa2af40b03
1 changed files with 29 additions and 8 deletions

View File

@ -1,16 +1,38 @@
import time import time
import os import os
from datetime import datetime
class Picker: class Picker:
directory = None directory = None
dispatcher = None dispatcher = None
observer = None observer = None
last_file_time = None last_file_marker = None
def __init__(self, directory, dispatcher): def __init__(self, directory, dispatcher):
self.directory = directory self.directory = directory
self.dispatcher = dispatcher self.dispatcher = dispatcher
self.last_file_marker = None
def read_last_file_marker(self):
if self.last_file_marker is None:
self.last_file_marker = datetime.now()
return self.last_file_marker
def update_last_file_marker(self, filename):
filename = filename.rstrip(".m4a")
marker = datetime.strptime(filename, "%Y_%m_%d_%H_%M_%S")
self.last_file_marker = marker
# TODO: also write to file, that one for later...
def find_next_file(self, files, file_marker):
for file in files:
short = os.path.basename(file).rstrip(".m4a")
marker = datetime.strptime(short, "%Y_%m_%d_%H_%M_%S")
if file_marker < marker:
return file
def identify_time_window(self, file_time): def identify_time_window(self, file_time):
if self.last_file_time is None: if self.last_file_time is None:
@ -23,17 +45,16 @@ class Picker:
files = [] files = []
for filename in os.listdir(self.directory): for filename in os.listdir(self.directory):
filepath = os.path.join(self.directory, filename) filepath = os.path.join(self.directory, filename)
if os.path.isfile(filepath): if os.path.isfile(filepath) and filename.endswith(".m4a"):
files.append(filepath) files.append(filepath)
return files return files
def observe(self): def observe(self):
while True: while True:
file_marker = self.read_last_file_marker()
files = self.list_files() files = self.list_files()
for file in files: next_file = self.find_next_file(files, file_marker)
file_time = os.path.getmtime(file) if next_file is not None:
if self.identify_time_window(file_time): self.dispatcher.process_audio_file(next_file)
self.dispatcher.process_audio_file(file) self.update_last_file_marker(os.path.basename(next_file).rstrip(".m4a"))
self.last_file_time = time.time()
time.sleep(10) time.sleep(10)