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
|
|
|
|
|
|
|
def get_conf():
|
|
|
|
'''return settings in settings.yaml file'''
|
2024-01-28 22:33:42 +01:00
|
|
|
settings = dict()
|
2024-01-25 23:52:23 +01:00
|
|
|
with open(os.path.dirname(os.path.realpath(__file__))+"/settings.yaml", 'r') as conf_file:
|
|
|
|
settings = yaml.safe_load(conf_file)
|
2024-01-28 22:33:42 +01:00
|
|
|
settings["db"] = {
|
|
|
|
"host": os.environ.get('POSTGRES_HOST'),
|
|
|
|
"port": os.environ.get('POSTGRES_PORT'),
|
|
|
|
"database": os.environ.get('POSTGRES_DB'),
|
|
|
|
"user": os.environ.get('POSTGRES_USER'),
|
|
|
|
"password": os.environ.get('POSTGRES_PASSWORD')
|
|
|
|
}
|
|
|
|
|
2024-01-25 23:52:23 +01:00
|
|
|
return settings
|
|
|
|
|
2024-01-28 01:20:50 +01:00
|
|
|
@app.route('/init')
|
|
|
|
def init_db():
|
|
|
|
print("init")
|
|
|
|
settings = get_conf()
|
|
|
|
initialize(settings["db"])
|
|
|
|
return 'Hello, World!'
|
|
|
|
|
|
|
|
@app.route('/update')
|
|
|
|
def update_hist():
|
|
|
|
print("update")
|
|
|
|
settings = get_conf()
|
|
|
|
fill_db(settings["db"], check_items(settings["item"]))
|
2024-01-25 23:52:23 +01:00
|
|
|
return 'Hello, World!'
|
|
|
|
|
2024-01-29 00:42:20 +01:00
|
|
|
@app.route('/add',methods = ['POST'])
|
|
|
|
def add_item():
|
|
|
|
print("adding item")
|
|
|
|
itemid = request.form['itemid']
|
2024-01-29 00:56:02 +01:00
|
|
|
if len(request.form['attributes'])>0:
|
|
|
|
attributes = request.form['attributes'].split(',')
|
|
|
|
else:
|
|
|
|
attributes = []
|
2024-01-29 00:42:20 +01:00
|
|
|
new_item = [(itemid, attributes)]
|
|
|
|
extr = check_items(new_item)
|
2024-01-29 00:56:02 +01:00
|
|
|
return str(extr)
|
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")
|
|
|
|
settings = get_conf()
|
|
|
|
return jsonify(get_history(settings["db"]))
|
|
|
|
|
|
|
|
@app.route('/dataitem',methods = ['POST', 'GET'])
|
|
|
|
def data_item_request():
|
|
|
|
if request.method == 'GET':
|
|
|
|
print("fetching data item")
|
|
|
|
settings = get_conf()
|
|
|
|
return jsonify(get_item(settings["db"]))
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug = True)
|