Repository navigation
Expand file tree
/
Copy pathtracker.py
More file actions
48 lines (37 loc) · 1.63 KB
/
Copy pathtracker.py
File metadata and controls
48 lines (37 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import json
from datetime import date, datetime
from pathlib import Path
from sender import choose_photos
class PhotoTracker:
def __init__(self, log_file: str | Path):
self.log_file = Path(log_file)
self.data = self._load()
def _load(self) -> dict:
if not self.log_file.exists():
return {"sent": []}
with open(self.log_file, "r", encoding="utf-8") as f:
return json.load(f)
def _save(self) -> None:
self.log_file.parent.mkdir(parents=True, exist_ok=True)
with open(self.log_file, "w", encoding="utf-8") as f:
json.dump(self.data, f, indent=2, ensure_ascii=False)
def get_sent_photos(self) -> list[str]:
return [Path(entry["path"]).as_posix() for entry in self.data["sent"]]
def get_sent_set(self) -> set[str]:
return set(self.get_sent_photos())
def mark_sent(self, path: str | Path, date_str: str | None = None) -> None:
if date_str is None:
date_str = date.today().isoformat()
self.data["sent"].append({
"path": Path(path).as_posix(),
"date": date_str,
"timestamp": datetime.now().isoformat(),
})
self._save()
def is_sent(self, path: str | Path) -> bool:
return Path(path).as_posix() in self.get_sent_set()
def get_today_count(self) -> int:
today = date.today().isoformat()
return sum(1 for entry in self.data["sent"] if entry["date"] == today)
def get_pending_photos(self, photos_dir: str | Path, count: int) -> list[str]:
return [p.as_posix() for p in choose_photos(photos_dir, self.get_sent_set(), count)]