handling items already existing, use attributes instead of skuid
This commit is contained in:
parent
59f7b31ea4
commit
bad3edfa0e
31
src/app.py
31
src/app.py
@ -37,25 +37,28 @@ def add_item():
|
|||||||
|
|
||||||
itemid = data.get('itemid')
|
itemid = data.get('itemid')
|
||||||
attributes = data.get('attributes', '').split(',') if data.get('attributes') else []
|
attributes = data.get('attributes', '').split(',') if data.get('attributes') else []
|
||||||
|
attributes = sorted(attributes)
|
||||||
new_item = [itemid, 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:
|
# check if item already exists
|
||||||
skuid = list(extr.values())[0]["skuid"]
|
formatted_attributes = re.sub("'", '"', re.sub(r'\]', '}', re.sub(r'\[', '{', f'{attributes}')))
|
||||||
if check_exist(itemid, skuid):
|
if check_exist(itemid, formatted_attributes):
|
||||||
# item already exists
|
# item already exists
|
||||||
return jsonify({'status': 3, "info": "item already exists"}), 400
|
return jsonify({'status': 3, "info": "item already exists"}), 400
|
||||||
else:
|
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
|
# item is valid
|
||||||
fill_db(extr)
|
fill_db(extr)
|
||||||
return jsonify({'status': 0, "info": "item added to database"}), 200
|
return jsonify({'status': 0, "info": "item added to database"}), 200
|
||||||
else:
|
else:
|
||||||
# item not valid or can't be parsed
|
# item not valid or can't be parsed
|
||||||
return jsonify({'status': 1, "info": "item not valid or can't be parsed"}), 400
|
return jsonify({'status': 1, "info": "item not valid or can't be parsed"}), 400
|
||||||
|
|
||||||
@app.route('/app/delete', methods=['POST'])
|
@app.route('/app/delete', methods=['POST'])
|
||||||
def del_item():
|
def del_item():
|
||||||
|
10
src/db.py
10
src/db.py
@ -2,6 +2,7 @@
|
|||||||
import psycopg2
|
import psycopg2
|
||||||
import csv
|
import csv
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
def get_conf():
|
def get_conf():
|
||||||
'''return db connection settings'''
|
'''return db connection settings'''
|
||||||
@ -46,7 +47,8 @@ def add_history_entry(itemid, skuid, choice, attributes, image, price, currency,
|
|||||||
connection = connect_db()
|
connection = connect_db()
|
||||||
cursor = connection.cursor()
|
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)
|
add_item(itemid, skuid, choice, attributes, image)
|
||||||
|
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
@ -180,7 +182,7 @@ def toggle_show(uuid):
|
|||||||
connection.commit()
|
connection.commit()
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
def check_exist(itemid, skuid):
|
def check_exist(itemid, attributes):
|
||||||
'''check if an item is already in the database'''
|
'''check if an item is already in the database'''
|
||||||
connection = connect_db()
|
connection = connect_db()
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
@ -189,8 +191,8 @@ def check_exist(itemid, skuid):
|
|||||||
SELECT uuid
|
SELECT uuid
|
||||||
FROM item
|
FROM item
|
||||||
WHERE itemid = %s
|
WHERE itemid = %s
|
||||||
AND skuid = %s
|
AND attributes = %s
|
||||||
""", (itemid, skuid))
|
""", (itemid, attributes))
|
||||||
|
|
||||||
result = cursor.rowcount
|
result = cursor.rowcount
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user