import os import requests import sys import time url = "https://conf2024.aiacademy.tw/email_validation/valid_session.php" file_path = "/tmp/conf2024/" expiration_time = 1800 # Step 1: Get sessions string from the URL response = requests.get(url) sessions_string = response.text.strip() # Step 2: Split the string by "```" ids = set(sessions_string.split("```")) # Loop through files in directory and remove if not in ids or expiration for file_name in os.listdir(file_path): file_path_name = os.path.join(file_path, file_name) # remove file by expiration file_stat = os.stat(file_path_name) if (file_stat.st_mtime + expiration_time) < time.time(): os.remove(file_path_name) continue # rmove file if not in ids if file_name.startswith("sess_") and file_name[5:] not in ids: os.remove(file_path_name)