handling items already existing, use attributes instead of skuid

This commit is contained in:
Sam Hadow 2024-07-20 23:13:01 +02:00
parent 59f7b31ea4
commit bad3edfa0e
2 changed files with 23 additions and 18 deletions

View File

@ -37,25 +37,28 @@ def add_item():
itemid = data.get('itemid')
attributes = data.get('attributes', '').split(',') if data.get('attributes') else []
attributes = sorted(attributes)
new_item = [itemid, attributes]
try:
extr = check_item(new_item)
except ValueError:
return jsonify({'status': 4, "info": "aliexpress punish page"}), 400
if len(extr) > 0:
skuid = list(extr.values())[0]["skuid"]
if check_exist(itemid, skuid):
# item already exists
return jsonify({'status': 3, "info": "item already exists"}), 400
else:
# check if item already exists
formatted_attributes = re.sub("'", '"', re.sub(r'\]', '}', re.sub(r'\[', '{', f'{attributes}')))
if check_exist(itemid, formatted_attributes):
# item already exists
return jsonify({'status': 3, "info": "item already exists"}), 400
else:
# if it doesn't already exist try to scrape this item
try:
extr = check_item(new_item)
except ValueError:
return jsonify({'status': 4, "info": "aliexpress punish page"}), 400
if len(extr) > 0:
# item is valid
fill_db(extr)
return jsonify({'status': 0, "info": "item added to database"}), 200
else:
# item not valid or can't be parsed
return jsonify({'status': 1, "info": "item not valid or can't be parsed"}), 400
else:
# item not valid or can't be parsed
return jsonify({'status': 1, "info": "item not valid or can't be parsed"}), 400
@app.route('/app/delete', methods=['POST'])
def del_item():

View File

@ -2,6 +2,7 @@
import psycopg2
import csv
import os
import re
def get_conf():
'''return db connection settings'''
@ -46,7 +47,8 @@ def add_history_entry(itemid, skuid, choice, attributes, image, price, currency,
connection = connect_db()
cursor = connection.cursor()
if not check_exist(itemid, skuid):
formatted_attributes = re.sub("'", '"', re.sub(r'\]', '}', re.sub(r'\[', '{', f'{attributes}')))
if not check_exist(itemid, formatted_attributes):
add_item(itemid, skuid, choice, attributes, image)
cursor.execute("""
@ -180,7 +182,7 @@ def toggle_show(uuid):
connection.commit()
connection.close()
def check_exist(itemid, skuid):
def check_exist(itemid, attributes):
'''check if an item is already in the database'''
connection = connect_db()
cursor = connection.cursor()
@ -189,8 +191,8 @@ def check_exist(itemid, skuid):
SELECT uuid
FROM item
WHERE itemid = %s
AND skuid = %s
""", (itemid, skuid))
AND attributes = %s
""", (itemid, attributes))
result = cursor.rowcount
cursor.close()