2024-01-25 23:52:23 +01:00
|
|
|
from flask import Flask, request, jsonify
|
2024-01-30 00:28:04 +01:00
|
|
|
from flask_cors import CORS
|
2024-01-25 23:52:23 +01:00
|
|
|
import requests, re, json, os, yaml
|
|
|
|
from db import *
|
|
|
|
from aliexpress import *
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
2024-01-30 00:28:04 +01:00
|
|
|
CORS(app)
|
2024-01-25 23:52:23 +01:00
|
|
|
|
2024-01-28 01:20:50 +01:00
|
|
|
@app.route('/init')
|
|
|
|
def init_db():
|
|
|
|
print("init")
|
2024-01-31 12:01:20 +01:00
|
|
|
initialize()
|
2024-01-28 01:20:50 +01:00
|
|
|
return 'Hello, World!'
|
|
|
|
|
|
|
|
@app.route('/update')
|
|
|
|
def update_hist():
|
|
|
|
print("update")
|
2024-01-31 21:56:16 +01:00
|
|
|
update_items()
|
|
|
|
return 'items updated'
|
2024-01-25 23:52:23 +01:00
|
|
|
|
2024-01-31 01:06:26 +01:00
|
|
|
@app.route('/add', methods=['POST'])
|
2024-01-29 00:42:20 +01:00
|
|
|
def add_item():
|
|
|
|
print("adding item")
|
2024-01-31 01:06:26 +01:00
|
|
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
|
|
|
itemid = data.get('itemid')
|
|
|
|
attributes = data.get('attributes', '').split(',') if data.get('attributes') else []
|
|
|
|
|
2024-01-29 00:42:20 +01:00
|
|
|
new_item = [(itemid, attributes)]
|
|
|
|
extr = check_items(new_item)
|
2024-01-31 01:06:26 +01:00
|
|
|
|
|
|
|
if len(extr) > 0:
|
|
|
|
skuid = list(extr.values())[0]["skuid"]
|
2024-01-31 12:01:20 +01:00
|
|
|
if check_exist(itemid, skuid):
|
2024-01-31 01:06:26 +01:00
|
|
|
# item already exists
|
|
|
|
return jsonify({'status': 3}), 400
|
|
|
|
else:
|
|
|
|
# item is valid
|
2024-01-31 12:01:20 +01:00
|
|
|
fill_db(extr)
|
2024-01-31 01:06:26 +01:00
|
|
|
return jsonify({'status': 0}), 200
|
|
|
|
else:
|
|
|
|
# item not valid or can't be parsed
|
|
|
|
return jsonify({'status': 1}), 400
|
2024-01-29 00:42:20 +01:00
|
|
|
|
2024-01-25 23:52:23 +01:00
|
|
|
@app.route('/datahistory',methods = ['POST', 'GET'])
|
|
|
|
def data_history_request():
|
|
|
|
if request.method == 'GET':
|
|
|
|
print("fetching data history")
|
2024-01-31 12:01:20 +01:00
|
|
|
return jsonify(get_history())
|
2024-01-25 23:52:23 +01:00
|
|
|
|
|
|
|
@app.route('/dataitem',methods = ['POST', 'GET'])
|
|
|
|
def data_item_request():
|
|
|
|
if request.method == 'GET':
|
|
|
|
print("fetching data item")
|
2024-01-31 12:01:20 +01:00
|
|
|
return jsonify(get_item())
|
2024-01-25 23:52:23 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug = True)
|