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 os
from datetime import datetime
class Picker:
directory = None
dispatcher = None
observer = None
last_file_time = None
last_file_marker = None
def __init__(self, directory, dispatcher):
self.directory = directory
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):
if self.last_file_time is None:
@ -23,17 +45,16 @@ class Picker:
files = []
for filename in os.listdir(self.directory):
filepath = os.path.join(self.directory, filename)
if os.path.isfile(filepath):
if os.path.isfile(filepath) and filename.endswith(".m4a"):
files.append(filepath)
return files
def observe(self):
while True:
file_marker = self.read_last_file_marker()
files = self.list_files()
for file in files:
file_time = os.path.getmtime(file)
if self.identify_time_window(file_time):
self.dispatcher.process_audio_file(file)
self.last_file_time = time.time()
next_file = self.find_next_file(files, file_marker)
if next_file is not None:
self.dispatcher.process_audio_file(next_file)
self.update_last_file_marker(os.path.basename(next_file).rstrip(".m4a"))
time.sleep(10)