2024-01-31 12:01:20 +01:00

61 lines
1.5 KiB
Python

from flask import Flask, request, jsonify
from flask_cors import CORS
import requests, re, json, os, yaml
from db import *
from aliexpress import *
app = Flask(__name__)
CORS(app)
@app.route('/init')
def init_db():
print("init")
initialize()
return 'Hello, World!'
@app.route('/update')
def update_hist():
print("update")
fill_db(check_items())
return 'Hello, World!'
@app.route('/add', methods=['POST'])
def add_item():
print("adding item")
data = request.get_json()
itemid = data.get('itemid')
attributes = data.get('attributes', '').split(',') if data.get('attributes') else []
new_item = [(itemid, attributes)]
extr = check_items(new_item)
if len(extr) > 0:
skuid = list(extr.values())[0]["skuid"]
if check_exist(itemid, skuid):
# item already exists
return jsonify({'status': 3}), 400
else:
# item is valid
fill_db(extr)
return jsonify({'status': 0}), 200
else:
# item not valid or can't be parsed
return jsonify({'status': 1}), 400
@app.route('/datahistory',methods = ['POST', 'GET'])
def data_history_request():
if request.method == 'GET':
print("fetching data history")
return jsonify(get_history())
@app.route('/dataitem',methods = ['POST', 'GET'])
def data_item_request():
if request.method == 'GET':
print("fetching data item")
return jsonify(get_item())
if __name__ == '__main__':
app.run(debug = True)