62 lines
1.8 KiB
Python
Raw Normal View History

from flask import Flask, request, jsonify
import requests, re, json, os, yaml
from db import *
from aliexpress import *
app = Flask(__name__)
def get_conf():
'''return settings in settings.yaml file'''
2024-01-28 22:33:42 +01:00
settings = dict()
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')
}
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"]))
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']
attributes = request.form['attributes'].split(',')
new_item = [(itemid, attributes)]
extr = check_items(new_item)
return str(extr['skuid'])
@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)