From 8d1ca7cf5052db4bdbec01b36edf38e433af3b0f Mon Sep 17 00:00:00 2001 From: archon0ne Date: Wed, 3 Jul 2024 17:59:06 +0200 Subject: [PATCH] faster and more api-friendly item caching (sorry tarkov.dev) --- app.py | 76 ++++++++++++++++++++++++++++++++------ templates/build_cache.html | 31 ++++++++++++++++ 2 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 templates/build_cache.html diff --git a/app.py b/app.py index 6dc77b7..ab1b6b6 100644 --- a/app.py +++ b/app.py @@ -11,12 +11,13 @@ app = Flask(__name__) # Setup logging to file logging.basicConfig(filename='log.log', level=logging.DEBUG, format='%(asctime)s %(levelname)s:%(message)s') -# Load the assort data +# File paths ASSORT_FILE_PATH = 'assort.json' QUEST_ASSORT_FILE_PATH = 'questassort.json' CACHE_FILE_PATH = 'item_cache.json' RUBLE_TPL_ID = '5449016a4bdc2d6f028b456f' +# Load assort data try: with open(ASSORT_FILE_PATH) as f: assort_data = json.load(f) @@ -34,18 +35,22 @@ except Exception as e: logging.error(traceback.format_exc()) # Load cache or initialize an empty dictionary -if os.path.exists(CACHE_FILE_PATH): - try: - with open(CACHE_FILE_PATH) as f: - item_cache = json.load(f) - logging.debug("Item cache loaded successfully") - except Exception as e: - logging.error(f"Error loading item cache: {e}") - logging.error(traceback.format_exc()) +def load_item_cache(): + global item_cache + if os.path.exists(CACHE_FILE_PATH): + try: + with open(CACHE_FILE_PATH) as f: + item_cache = json.load(f) + logging.debug("Item cache loaded successfully") + except Exception as e: + logging.error(f"Error loading item cache: {e}") + logging.error(traceback.format_exc()) + item_cache = {} + else: item_cache = {} -else: - item_cache = {} - logging.debug("Initialized empty item cache") + logging.debug("Initialized empty item cache") + +load_item_cache() # Tarkov.dev API URL TARKOV_API_URL = "https://api.tarkov.dev/graphql" @@ -55,6 +60,9 @@ def get_ruble_image(): @app.route('/') def index(): + if not is_cache_complete(): + return redirect(url_for('build_cache')) + try: items = get_main_items_with_details(assort_data) ruble_image = get_ruble_image() @@ -144,6 +152,38 @@ def get_quest_requirement(item_id): } return None +def get_all_item_ids(assort_data): + ids = set() + for item in assort_data['items']: + ids.add(item['_tpl']) + for part in item.get('parts', []): + ids.add(part['_tpl']) + for schemes in assort_data.get('barter_scheme', {}).values(): + for scheme in schemes: + for req in scheme: + ids.add(req['_tpl']) + return list(ids) + +def is_cache_complete(): + item_ids = get_all_item_ids(assort_data) + for item_id in item_ids: + if item_id not in item_cache: + return False + return True + +def build_item_cache(): + try: + item_ids = get_all_item_ids(assort_data) + item_details = get_items_details(item_ids) + for tpl, details in item_details.items(): + item_cache[tpl] = details + save_item_cache() + return True + except Exception as e: + logging.error(f"Error building item cache: {e}") + logging.error(traceback.format_exc()) + return False + @app.route('/edit/', methods=['GET', 'POST']) def edit_item(item_id): try: @@ -258,5 +298,17 @@ def search_items(): logging.error(traceback.format_exc()) return jsonify([]), 500 +@app.route('/build_cache', methods=['GET']) +def build_cache(): + return render_template('build_cache.html') + +@app.route('/start_build_cache', methods=['POST']) +def start_build_cache(): + success = build_item_cache() + if success: + return jsonify({"status": "success"}) + else: + return jsonify({"status": "error"}), 500 + if __name__ == '__main__': app.run(debug=True) diff --git a/templates/build_cache.html b/templates/build_cache.html new file mode 100644 index 0000000..f1ccb4c --- /dev/null +++ b/templates/build_cache.html @@ -0,0 +1,31 @@ + + + + + Building Cache + + + +
+

Building Cache

+

Please wait while we build the item cache. This may take a few moments.

+
+
+
+
+ + + + +