update username X
This commit is contained in:
+47
-31
@@ -32,42 +32,58 @@ IMAGE_UPLOAD_TIMEOUT = 30 # Added to fix NameError
|
||||
|
||||
IMAGE_EXPIRATION_DAYS = 7 # 7 days, consistent with foodie_automator_rss.py
|
||||
|
||||
def load_json_file(file_path, expiration_hours):
|
||||
entries = []
|
||||
cutoff = datetime.now(timezone.utc) - timedelta(hours=expiration_hours)
|
||||
|
||||
if not os.path.exists(file_path):
|
||||
logging.info(f"File {file_path} does not exist, returning empty list")
|
||||
return entries
|
||||
|
||||
def load_json_file(file_path, expiration_hours=None):
|
||||
try:
|
||||
if not os.path.exists(file_path):
|
||||
logging.info(f"File {file_path} does not exist, initializing with empty list")
|
||||
with open(file_path, 'w') as f:
|
||||
json.dump([], f)
|
||||
return []
|
||||
|
||||
with open(file_path, 'r') as f:
|
||||
lines = f.readlines()
|
||||
|
||||
for i, line in enumerate(lines, 1):
|
||||
try:
|
||||
entry = json.loads(line.strip())
|
||||
if not isinstance(entry, dict) or "title" not in entry or "timestamp" not in entry:
|
||||
logging.warning(f"Skipping malformed entry in {file_path} at line {i}: {line.strip()}")
|
||||
continue
|
||||
|
||||
timestamp = datetime.fromisoformat(entry["timestamp"])
|
||||
if timestamp > cutoff:
|
||||
entries.append(entry)
|
||||
else:
|
||||
logging.debug(f"Entry expired in {file_path}: {entry['title']}")
|
||||
data = json.load(f)
|
||||
except json.JSONDecodeError as e:
|
||||
logging.warning(f"Skipping invalid JSON line in {file_path} at line {i}: {e}")
|
||||
continue
|
||||
except Exception as e:
|
||||
logging.warning(f"Skipping malformed entry in {file_path} at line {i}: {line.strip()}")
|
||||
continue
|
||||
|
||||
logging.info(f"Loaded {len(entries)} entries from {file_path}, {len(entries)} valid after expiration check")
|
||||
return entries
|
||||
logging.warning(f"Invalid JSON in {file_path}: {e}. Attempting line-by-line parsing.")
|
||||
data = []
|
||||
f.seek(0)
|
||||
for line_number, line in enumerate(f, 1):
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
try:
|
||||
entry = json.loads(line)
|
||||
data.append(entry)
|
||||
except json.JSONDecodeError as e:
|
||||
logging.warning(f"Skipping invalid JSON line in {file_path} at line {line_number}: {e}")
|
||||
continue
|
||||
|
||||
if not isinstance(data, list):
|
||||
logging.warning(f"Data in {file_path} is not a list, resetting to empty list")
|
||||
data = []
|
||||
|
||||
valid_entries = []
|
||||
if expiration_hours:
|
||||
cutoff = datetime.now(timezone.utc) - timedelta(hours=expiration_hours)
|
||||
for entry in data:
|
||||
try:
|
||||
timestamp_str = entry.get("timestamp")
|
||||
if timestamp_str:
|
||||
timestamp = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00'))
|
||||
if timestamp < cutoff:
|
||||
continue
|
||||
valid_entries.append(entry)
|
||||
except (ValueError, TypeError) as e:
|
||||
logging.warning(f"Skipping malformed entry in {file_path}: {e}")
|
||||
continue
|
||||
else:
|
||||
valid_entries = data
|
||||
|
||||
logging.info(f"Loaded {len(valid_entries)} entries from {file_path}, {len(valid_entries)} valid after expiration check")
|
||||
return valid_entries
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to load {file_path}: {e}")
|
||||
return entries
|
||||
logging.error(f"Failed to load JSON file {file_path}: {e}")
|
||||
return []
|
||||
|
||||
def save_json_file(file_path, title, timestamp):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user