track new item
This commit is contained in:
parent
4894eae14f
commit
7b1bef3695
26
src/app.py
26
src/app.py
@ -39,14 +39,28 @@ def update_hist():
|
|||||||
@app.route('/add', methods=['POST'])
|
@app.route('/add', methods=['POST'])
|
||||||
def add_item():
|
def add_item():
|
||||||
print("adding item")
|
print("adding item")
|
||||||
itemid = request.form['itemid']
|
|
||||||
if len(request.form['attributes'])>0:
|
data = request.get_json()
|
||||||
attributes = request.form['attributes'].split(',')
|
|
||||||
else:
|
itemid = data.get('itemid')
|
||||||
attributes = []
|
attributes = data.get('attributes', '').split(',') if data.get('attributes') else []
|
||||||
|
|
||||||
new_item = [(itemid, attributes)]
|
new_item = [(itemid, attributes)]
|
||||||
extr = check_items(new_item)
|
extr = check_items(new_item)
|
||||||
return str(extr)
|
|
||||||
|
if len(extr) > 0:
|
||||||
|
settings = get_conf()
|
||||||
|
skuid = list(extr.values())[0]["skuid"]
|
||||||
|
if check_exist(settings["db"], itemid, skuid):
|
||||||
|
# item already exists
|
||||||
|
return jsonify({'status': 3}), 400
|
||||||
|
else:
|
||||||
|
# item is valid
|
||||||
|
fill_db(settings["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'])
|
@app.route('/datahistory',methods = ['POST', 'GET'])
|
||||||
def data_history_request():
|
def data_history_request():
|
||||||
|
30
src/db.py
30
src/db.py
@ -31,14 +31,7 @@ def add_history_entry(db_settings, itemid, skuid, choice, attributes, image, pri
|
|||||||
connection = connect_db(db_settings)
|
connection = connect_db(db_settings)
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
cursor.execute("""
|
if not check_exist(db_settings, itemid, skuid):
|
||||||
SELECT uuid
|
|
||||||
FROM item
|
|
||||||
WHERE itemid = %s
|
|
||||||
AND skuid = %s
|
|
||||||
""", (itemid, skuid))
|
|
||||||
|
|
||||||
if cursor.rowcount == 0:
|
|
||||||
add_item(db_settings, itemid, skuid, choice, attributes, image)
|
add_item(db_settings, itemid, skuid, choice, attributes, image)
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT uuid
|
SELECT uuid
|
||||||
@ -82,6 +75,27 @@ def get_item(db_settings):
|
|||||||
connection.close()
|
connection.close()
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
def check_exist(db_settings, itemid, skuid):
|
||||||
|
'''check if an item is already in the database'''
|
||||||
|
connection = connect_db(db_settings)
|
||||||
|
cursor = connection.cursor()
|
||||||
|
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT uuid
|
||||||
|
FROM item
|
||||||
|
WHERE itemid = %s
|
||||||
|
AND skuid = %s
|
||||||
|
""", (itemid, skuid))
|
||||||
|
|
||||||
|
result = cursor.rowcount
|
||||||
|
cursor.close()
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
if result == 0:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
def export_csv(db_settings):
|
def export_csv(db_settings):
|
||||||
'''join item and history data from database and export it in ./output.csv'''
|
'''join item and history data from database and export it in ./output.csv'''
|
||||||
connection = connect_db(db_settings)
|
connection = connect_db(db_settings)
|
||||||
|
29
web/app.html
29
web/app.html
@ -1,16 +1,25 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<meta charset="utf-8">
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<link rel="stylesheet" href="style.css">
|
||||||
|
<title>Aliexpress price tracker</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
<!-- Load d3.js -->
|
<!-- Load d3.js -->
|
||||||
<script src="https://d3js.org/d3.v6.js"></script>
|
<script src="https://d3js.org/d3.v6.js"></script>
|
||||||
|
|
||||||
<!-- Create a div where the graphs will take place -->
|
<!--banner-->
|
||||||
<div id="graphs"></div>
|
<div class="banner">
|
||||||
|
<p>Lorem ipsum dolor sit amet</p>
|
||||||
|
<input id="additemid" type="text" placeholder="item id" pattern="[0-9]*">
|
||||||
|
<input id="addattributes" type="text" placeholder="attributes (comma separated)" pattern="[0-9a-zA-Z, ]*">
|
||||||
|
<button id="addbutton" type="button">Add</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- <script src="http://127.0.0.1/app.js"></script> -->
|
<!-- div where graphs will take place -->
|
||||||
<script>
|
<div class="content" id="graphs"></div>
|
||||||
let currenturl = (window.location);
|
|
||||||
var s = document.createElement( 'script' );
|
<script src="app.js"></script>
|
||||||
s.setAttribute( 'src', `${currenturl}/app.js` );
|
|
||||||
document.body.appendChild( s );
|
|
||||||
</script>
|
|
||||||
|
40
web/app.js
40
web/app.js
@ -23,13 +23,16 @@ async function fetch_history() {
|
|||||||
try {
|
try {
|
||||||
const response = await fetch(`${currentUrl}app/datahistory`);
|
const response = await fetch(`${currentUrl}app/datahistory`);
|
||||||
const rawData = await response.json();
|
const rawData = await response.json();
|
||||||
|
var dateFormat = d3.timeParse("%a, %d %b %Y %H:%M:%S GMT");
|
||||||
|
console.log(rawData)
|
||||||
// SELECT uuid, quantity, discount_percentage, price, currency, h_timestamp
|
// SELECT uuid, quantity, discount_percentage, price, currency, h_timestamp
|
||||||
let historyData = rawData.map(d => ({
|
let historyData = rawData.map(d => ({
|
||||||
uuid: d[0],
|
uuid: d[0],
|
||||||
value: parseFloat(d[3].replace('$', '')),
|
value: parseFloat(d[3].replace('$', '')),
|
||||||
currency: d[4],
|
currency: d[4],
|
||||||
date: new Date(d[5].replace(' ', 'T')),
|
date: dateFormat(d[5]),
|
||||||
}));
|
}));
|
||||||
|
console.log(historyData)
|
||||||
return historyData;
|
return historyData;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching data history: ', error);
|
console.error('Error fetching data history: ', error);
|
||||||
@ -281,3 +284,38 @@ fetch_history().then(async function(data) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
// add item
|
||||||
|
document.getElementById("addbutton").addEventListener("click", addItem);
|
||||||
|
|
||||||
|
async function addItem() {
|
||||||
|
|
||||||
|
const apiUrl = `${currentUrl}app/add`;
|
||||||
|
|
||||||
|
const postData = {
|
||||||
|
itemid: document.getElementById("additemid").value,
|
||||||
|
attributes: document.getElementById("addattributes").value
|
||||||
|
};
|
||||||
|
|
||||||
|
const requestOptions = {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(postData)
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await fetch(apiUrl, requestOptions)
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Error during POST request:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
console.log(response);
|
||||||
|
} else {
|
||||||
|
throw new Error('Error in server response');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
18
web/style.css
Normal file
18
web/style.css
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.banner {
|
||||||
|
background-color: #177013; /* background color */
|
||||||
|
color: #fff; /* text color */
|
||||||
|
padding: 10px;
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.content {
|
||||||
|
margin-top: 100px;
|
||||||
|
padding: 0px;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user