import os import yaml import json from datetime import datetime import config weights = None def check_conditions(): folders = ["questions", "questions_custom"] found_one = False for folder in folders: if os.path.exists(folder): found_one = True break if not found_one: print("No questions folder found.") exit(1) def get_questions(): folders = ["questions", "questions_custom"] questions = {} for folder in folders: if not os.path.exists(folder): continue for filename in os.listdir(folder): if filename.endswith(".yml") or filename.endswith(".yaml"): file_path = os.path.join(folder, filename) with open(file_path, "r") as f: file_questions = yaml.safe_load(f) for category, category_questions in file_questions.items(): if category not in questions: questions[category] = [] questions[category].extend(category_questions) questions[category] = list(set(questions[category])) return questions def get_categories(): return get_questions().keys() def get_questions_by_category(category): questions = get_questions() if category not in questions: return [] return questions[category] def load_weights(): os.makedirs("data/", exist_ok=True) fname = "data/weights.json" if os.path.exists(fname): with open(fname, "r") as fp: return json.load(fp) return {} def get_all_weights(): global weights if weights is None: weights = load_weights() return weights def save_weights(): weights = get_all_weights() os.makedirs("data/", exist_ok=True) fname = "data/weights.json" with open(fname, "w") as fp: json.dump(weights, fp, indent=2) def get_weight_index(c, q): return f"{c}_{q}" def get_question_weights(c, q): max_weight = config.get_max_last_accessed_days() min_weight = 1 weights = get_all_weights() c_index = get_weight_index(c, q) last_selected_ts = 0 if c_index in weights: last_selected_ts = weights[c_index] last_selected = datetime.fromtimestamp(last_selected_ts) now = datetime.now() days_passed = (now - last_selected).days days_passed = min(max_weight, days_passed) days_passed = max(min_weight, days_passed) return days_passed def set_question_selected(c, q): weights = get_all_weights() c_index = get_weight_index(c, q) ts = datetime.timestamp(datetime.now()) weights[c_index] = int(ts)