MOVE write_state and load_state functions to paths.py
This is to encapsulate all path and state file related operations within the paths module.
This commit is contained in:
@@ -12,9 +12,14 @@ def state_dir() -> Path:
|
|||||||
def get_state_file_path() -> Path:
|
def get_state_file_path() -> Path:
|
||||||
return state_dir() / "state.json"
|
return state_dir() / "state.json"
|
||||||
|
|
||||||
def get_state_file() -> dict:
|
def load_state() -> dict:
|
||||||
path = get_state_file_path()
|
if not get_state_file_path.exists():
|
||||||
if not path.exists():
|
|
||||||
return {"files": []}
|
return {"files": []}
|
||||||
with path.open() as f:
|
with get_state_file_path.open() as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
|
|
||||||
|
def write_state(state: dict):
|
||||||
|
tmp = get_state_file_path().with_suffix(".tmp")
|
||||||
|
with tmp.open("w") as f:
|
||||||
|
json.dump(state, f, indent=2)
|
||||||
|
tmp.replace(get_state_file_path()) # atomic
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ import subprocess
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from video import get_duration
|
from video import get_duration
|
||||||
|
from paths import load_state, write_state
|
||||||
|
|
||||||
INTERVAL = 10
|
INTERVAL = 10
|
||||||
MAX_AGE_SECONDS = 60 * 60 * 8
|
MAX_AGE_SECONDS = 60 * 60 * 8
|
||||||
STATE_FILE = "state.json"
|
|
||||||
|
|
||||||
def open_obs():
|
def open_obs():
|
||||||
subprocess.Popen(["obs", "--minimize-to-tray"])
|
subprocess.Popen(["obs", "--minimize-to-tray"])
|
||||||
@@ -36,46 +36,52 @@ def cleanup_old_files(directory, max_age_seconds):
|
|||||||
file_path = os.path.join(directory, filename)
|
file_path = os.path.join(directory, filename)
|
||||||
if os.path.isfile(file_path):
|
if os.path.isfile(file_path):
|
||||||
file_age = datetime.datetime.now().timestamp() - os.path.getmtime(file_path)
|
file_age = datetime.datetime.now().timestamp() - os.path.getmtime(file_path)
|
||||||
if file_age > max_age_seconds:
|
if file_age > max_age_seconds and filename.endswith(".ts"):
|
||||||
os.remove(file_path)
|
os.remove(file_path)
|
||||||
print(f"Removed old file: {file_path}")
|
print(f"Removed old file: {file_path}")
|
||||||
|
|
||||||
|
|
||||||
|
def create_state_file():
|
||||||
|
state = {"files": []}
|
||||||
|
write_state(state)
|
||||||
|
|
||||||
def add_file_to_state(file_path):
|
def add_file_to_state(file_path):
|
||||||
state = {}
|
state = load_state()
|
||||||
if os.path.exists(STATE_FILE):
|
|
||||||
state = json.load(open(STATE_FILE))
|
|
||||||
files = state.get("files", [])
|
files = state.get("files", [])
|
||||||
|
|
||||||
if len(files) > 0:
|
# Update duration of last file if exists
|
||||||
files[len(files)-1][1] = get_duration(files[len(files)-1][0])
|
if files and len(files) > 0:
|
||||||
|
last_file = files[-1]
|
||||||
|
last_file["duration"] = get_duration(last_file["path"])
|
||||||
|
|
||||||
files.append([file_path, 0.0])
|
files.append({
|
||||||
json.dump(state, open(STATE_FILE, "w"))
|
"path": file_path,
|
||||||
|
"duration": 0.0
|
||||||
|
})
|
||||||
|
|
||||||
|
state["files"] = files
|
||||||
|
write_state(state)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
open_obs()
|
open_obs()
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
con = open_obs_connection()
|
con = open_obs_connection()
|
||||||
if con is None:
|
if con is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
recording_dir = con.get_record_directory().record_directory
|
recording_dir = con.get_record_directory().record_directory
|
||||||
start_recording(con)
|
start_recording(con)
|
||||||
|
|
||||||
# create state file
|
create_state_file()
|
||||||
state = {
|
|
||||||
"files": []
|
|
||||||
}
|
|
||||||
json.dump(state, open(STATE_FILE, "w"))
|
|
||||||
|
|
||||||
current_files = os.listdir(recording_dir)
|
current_files = os.listdir(recording_dir)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
cleanup_old_files(recording_dir, MAX_AGE_SECONDS)
|
cleanup_old_files(recording_dir, MAX_AGE_SECONDS)
|
||||||
|
|
||||||
new_files = os.listdir(recording_dir)
|
new_files = os.listdir(recording_dir)
|
||||||
added_files = set(new_files) - set(current_files)
|
added_files = set(new_files) - set(current_files)
|
||||||
|
|
||||||
|
# Add new files to state
|
||||||
for filename in added_files:
|
for filename in added_files:
|
||||||
file_path = os.path.join(recording_dir, filename)
|
file_path = os.path.join(recording_dir, filename)
|
||||||
add_file_to_state(file_path)
|
add_file_to_state(file_path)
|
||||||
@@ -84,7 +90,6 @@ def main():
|
|||||||
current_files = new_files
|
current_files = new_files
|
||||||
time.sleep(INTERVAL)
|
time.sleep(INTERVAL)
|
||||||
|
|
||||||
end_recording(con)
|
|
||||||
stop_recording(con)
|
stop_recording(con)
|
||||||
con.disconnect()
|
con.disconnect()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user