faster and more api-friendly item caching (sorry tarkov.dev)

This commit is contained in:
archon0ne 2024-07-03 17:59:06 +02:00
parent 14901477a3
commit 8d1ca7cf50
2 changed files with 95 additions and 12 deletions

54
app.py
View File

@ -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,6 +35,8 @@ except Exception as e:
logging.error(traceback.format_exc())
# Load cache or initialize an empty dictionary
def load_item_cache():
global item_cache
if os.path.exists(CACHE_FILE_PATH):
try:
with open(CACHE_FILE_PATH) as f:
@ -47,6 +50,8 @@ else:
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/<item_id>', 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)

View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Building Cache</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Building Cache</h1>
<p>Please wait while we build the item cache. This may take a few moments.</p>
<div id="progress" class="progress">
<div id="progress-bar" class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" style="width: 0%"></div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$.post('/start_build_cache', function(data) {
if (data.status === "success") {
$('#progress-bar').css('width', '100%').attr('aria-valuenow', 100);
window.location.href = '/';
} else {
alert('Error building cache');
}
});
});
</script>
</body>
</html>