Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -528,7 +528,19 @@ def _create_product_all_variants_with_grid(
|
|
| 528 |
except Exception:
|
| 529 |
continue
|
| 530 |
|
| 531 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
|
| 533 |
existing = _find_existing_product_by_fp(logs, shop_id, fp)
|
| 534 |
if isinstance(existing, dict) and existing.get("id"):
|
|
@@ -655,6 +667,105 @@ def _create_product_all_variants_with_grid(
|
|
| 655 |
return created
|
| 656 |
|
| 657 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 658 |
def _get_product(logs: List[str], shop_id: str, product_id: str) -> Dict[str, Any]:
|
| 659 |
prod = _req("GET", f"/v1/shops/{shop_id}/products/{product_id}.json", logs=logs)
|
| 660 |
if not isinstance(prod, dict):
|
|
@@ -721,10 +832,16 @@ def _update_prices_to_cost_plus_margin(
|
|
| 721 |
if not payload_variants:
|
| 722 |
raise RuntimeError("No variant prices could be computed from costs.")
|
| 723 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 724 |
upd = _req(
|
| 725 |
"PUT",
|
| 726 |
f"/v1/shops/{shop_id}/products/{product_id}.json",
|
| 727 |
-
json_body=
|
| 728 |
logs=logs,
|
| 729 |
)
|
| 730 |
if not isinstance(upd, dict):
|
|
@@ -909,51 +1026,86 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 909 |
total_chunks = (total_variants + CHUNK_TARGET_VARIANTS - 1) // CHUNK_TARGET_VARIANTS
|
| 910 |
chunk_index = 0
|
| 911 |
offset = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 912 |
while offset < total_variants:
|
| 913 |
chunk = variants_all[offset: offset + CHUNK_TARGET_VARIANTS]
|
| 914 |
-
p_info_chunk = dict(product_info_full)
|
| 915 |
-
p_info_chunk["variants"] = chunk
|
| 916 |
-
p_info_chunk["options"] = _rebuild_options_for_variants(chunk)
|
| 917 |
-
p_info_chunk["_allVariants"] = variants_all
|
| 918 |
-
p_info_chunk["_enabledVariantIds"] = [int(v.get("id")) for v in chunk if v.get("id") is not None]
|
| 919 |
|
| 920 |
_log(
|
| 921 |
logs,
|
| 922 |
-
f"
|
| 923 |
f"chunk_index={chunk_index} total_chunks={total_chunks} "
|
| 924 |
-
f"offset={offset} size={len(chunk)} total_variants={total_variants}",
|
| 925 |
)
|
| 926 |
yield flush()
|
| 927 |
|
| 928 |
-
|
| 929 |
logs,
|
| 930 |
shop_id=shop_id,
|
| 931 |
-
|
| 932 |
-
|
| 933 |
upload=upload,
|
| 934 |
)
|
| 935 |
-
created_id = str(created.get("id") or "")
|
| 936 |
-
if not created_id:
|
| 937 |
-
raise RuntimeError("Created product response missing id.")
|
| 938 |
yield flush()
|
| 939 |
|
| 940 |
-
|
| 941 |
yield flush()
|
| 942 |
|
| 943 |
_log(
|
| 944 |
logs,
|
| 945 |
f"COMPARE_COUNTS provider={provider_id} chunk_index={chunk_index} "
|
| 946 |
f"catalog_chunk={len(chunk)} "
|
| 947 |
-
f"shop={len(
|
| 948 |
)
|
| 949 |
yield flush()
|
| 950 |
|
| 951 |
-
upd = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod1)
|
| 952 |
-
yield flush()
|
| 953 |
-
|
| 954 |
-
prod2 = _get_product(logs, shop_id, created_id)
|
| 955 |
-
yield flush()
|
| 956 |
-
|
| 957 |
provider_runs.append(
|
| 958 |
{
|
| 959 |
"providerId": provider_id,
|
|
@@ -962,16 +1114,40 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 962 |
"totalChunks": total_chunks,
|
| 963 |
"catalogVariantCountTotal": total_variants,
|
| 964 |
"catalogVariantCountChunk": len(chunk),
|
| 965 |
-
"shopVariantCountAfterCreate": len(
|
| 966 |
-
"shopVariantCountFinal": len(
|
| 967 |
"productId": created_id,
|
| 968 |
-
"priceUpdateResponse":
|
| 969 |
}
|
| 970 |
)
|
| 971 |
|
| 972 |
offset += CHUNK_TARGET_VARIANTS
|
| 973 |
chunk_index += 1
|
| 974 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 975 |
result["providerRuns"] = provider_runs
|
| 976 |
_log(logs, "PHASE_B_DONE")
|
| 977 |
yield flush()
|
|
|
|
| 528 |
except Exception:
|
| 529 |
continue
|
| 530 |
|
| 531 |
+
all_ids_for_fp = []
|
| 532 |
+
for v in all_variants:
|
| 533 |
+
if not isinstance(v, dict):
|
| 534 |
+
continue
|
| 535 |
+
vid = v.get("id")
|
| 536 |
+
if vid is None:
|
| 537 |
+
continue
|
| 538 |
+
try:
|
| 539 |
+
all_ids_for_fp.append(int(vid))
|
| 540 |
+
except Exception:
|
| 541 |
+
continue
|
| 542 |
+
|
| 543 |
+
fp = _batch_fingerprint(str(shop_id), int(bp_id), int(provider_id), sorted(all_ids_for_fp))
|
| 544 |
|
| 545 |
existing = _find_existing_product_by_fp(logs, shop_id, fp)
|
| 546 |
if isinstance(existing, dict) and existing.get("id"):
|
|
|
|
| 667 |
return created
|
| 668 |
|
| 669 |
|
| 670 |
+
def _update_product_enable_chunk_with_grid(
|
| 671 |
+
logs: List[str],
|
| 672 |
+
shop_id: str,
|
| 673 |
+
product_id: str,
|
| 674 |
+
enabled_variants: List[Dict[str, Any]],
|
| 675 |
+
upload: Dict[str, Any],
|
| 676 |
+
) -> Dict[str, Any]:
|
| 677 |
+
img_id = upload.get("id")
|
| 678 |
+
img_w = float(upload.get("width") or 0)
|
| 679 |
+
img_h = float(upload.get("height") or 0)
|
| 680 |
+
|
| 681 |
+
variants_payload = []
|
| 682 |
+
print_areas_payload = []
|
| 683 |
+
|
| 684 |
+
enabled_count = 0
|
| 685 |
+
for v in enabled_variants:
|
| 686 |
+
if not isinstance(v, dict):
|
| 687 |
+
continue
|
| 688 |
+
vid = v.get("id")
|
| 689 |
+
if vid is None:
|
| 690 |
+
continue
|
| 691 |
+
try:
|
| 692 |
+
vid_i = int(vid)
|
| 693 |
+
except Exception:
|
| 694 |
+
continue
|
| 695 |
+
enabled_count += 1
|
| 696 |
+
variants_payload.append({
|
| 697 |
+
"id": vid_i,
|
| 698 |
+
"price": 1,
|
| 699 |
+
"is_enabled": True,
|
| 700 |
+
})
|
| 701 |
+
|
| 702 |
+
placeholders = v.get("placeholders") or []
|
| 703 |
+
if not isinstance(placeholders, list) or not placeholders:
|
| 704 |
+
continue
|
| 705 |
+
|
| 706 |
+
ph_payload = []
|
| 707 |
+
for ph in placeholders:
|
| 708 |
+
if not isinstance(ph, dict):
|
| 709 |
+
continue
|
| 710 |
+
pos = ph.get("position")
|
| 711 |
+
pw = ph.get("width")
|
| 712 |
+
phh = ph.get("height")
|
| 713 |
+
if not pos or pw is None or phh is None:
|
| 714 |
+
continue
|
| 715 |
+
try:
|
| 716 |
+
pwf = float(pw)
|
| 717 |
+
phf = float(phh)
|
| 718 |
+
except Exception:
|
| 719 |
+
continue
|
| 720 |
+
scale = _scale_fill(pwf, phf, img_w, img_h)
|
| 721 |
+
ph_payload.append({
|
| 722 |
+
"position": pos,
|
| 723 |
+
"images": [{
|
| 724 |
+
"id": img_id,
|
| 725 |
+
"x": 0.5,
|
| 726 |
+
"y": 0.5,
|
| 727 |
+
"scale": scale,
|
| 728 |
+
"angle": 0,
|
| 729 |
+
}],
|
| 730 |
+
})
|
| 731 |
+
|
| 732 |
+
if ph_payload:
|
| 733 |
+
print_areas_payload.append({
|
| 734 |
+
"variant_ids": [vid_i],
|
| 735 |
+
"placeholders": ph_payload,
|
| 736 |
+
})
|
| 737 |
+
|
| 738 |
+
if not variants_payload:
|
| 739 |
+
raise RuntimeError("No variants in chunk update payload.")
|
| 740 |
+
|
| 741 |
+
if not print_areas_payload:
|
| 742 |
+
_log(logs, f"PHASE_B_UPDATE_WARN product_id={product_id} reason=no_print_areas_for_chunk enabled={enabled_count}")
|
| 743 |
+
|
| 744 |
+
payload = {
|
| 745 |
+
"variants": variants_payload,
|
| 746 |
+
}
|
| 747 |
+
if print_areas_payload:
|
| 748 |
+
payload["print_areas"] = print_areas_payload
|
| 749 |
+
|
| 750 |
+
_log(
|
| 751 |
+
logs,
|
| 752 |
+
f"PHASE_B_UPDATE product_id={product_id} enable_variants={len(variants_payload)} "
|
| 753 |
+
f"print_areas_payload={len(print_areas_payload)}",
|
| 754 |
+
)
|
| 755 |
+
|
| 756 |
+
upd = _req(
|
| 757 |
+
"PUT",
|
| 758 |
+
f"/v1/shops/{shop_id}/products/{product_id}.json",
|
| 759 |
+
json_body=payload,
|
| 760 |
+
logs=logs,
|
| 761 |
+
)
|
| 762 |
+
if not isinstance(upd, dict):
|
| 763 |
+
raise RuntimeError(f"Unexpected update response: {str(upd)[:500]}")
|
| 764 |
+
|
| 765 |
+
_log(logs, f"PHASE_B_UPDATE_DONE product_id={product_id}")
|
| 766 |
+
return upd
|
| 767 |
+
|
| 768 |
+
|
| 769 |
def _get_product(logs: List[str], shop_id: str, product_id: str) -> Dict[str, Any]:
|
| 770 |
prod = _req("GET", f"/v1/shops/{shop_id}/products/{product_id}.json", logs=logs)
|
| 771 |
if not isinstance(prod, dict):
|
|
|
|
| 832 |
if not payload_variants:
|
| 833 |
raise RuntimeError("No variant prices could be computed from costs.")
|
| 834 |
|
| 835 |
+
upd_body: Dict[str, Any] = {"variants": payload_variants}
|
| 836 |
+
if isinstance(product.get("title"), str):
|
| 837 |
+
upd_body["title"] = product.get("title")
|
| 838 |
+
if isinstance(product.get("description"), str):
|
| 839 |
+
upd_body["description"] = product.get("description")
|
| 840 |
+
|
| 841 |
upd = _req(
|
| 842 |
"PUT",
|
| 843 |
f"/v1/shops/{shop_id}/products/{product_id}.json",
|
| 844 |
+
json_body=upd_body,
|
| 845 |
logs=logs,
|
| 846 |
)
|
| 847 |
if not isinstance(upd, dict):
|
|
|
|
| 1026 |
total_chunks = (total_variants + CHUNK_TARGET_VARIANTS - 1) // CHUNK_TARGET_VARIANTS
|
| 1027 |
chunk_index = 0
|
| 1028 |
offset = 0
|
| 1029 |
+
|
| 1030 |
+
first_chunk = variants_all[0:CHUNK_TARGET_VARIANTS]
|
| 1031 |
+
p_info_first = dict(product_info_full)
|
| 1032 |
+
p_info_first["variants"] = first_chunk
|
| 1033 |
+
p_info_first["options"] = _rebuild_options_for_variants(first_chunk)
|
| 1034 |
+
p_info_first["_allVariants"] = variants_all
|
| 1035 |
+
p_info_first["_enabledVariantIds"] = [int(v.get("id")) for v in first_chunk if v.get("id") is not None]
|
| 1036 |
+
|
| 1037 |
+
_log(
|
| 1038 |
+
logs,
|
| 1039 |
+
f"PROVIDER_CHUNK_CREATE provider_id={provider_id} name={provider_name} "
|
| 1040 |
+
f"chunk_index={chunk_index} total_chunks={total_chunks} "
|
| 1041 |
+
f"offset={0} size={len(first_chunk)} total_variants={total_variants}",
|
| 1042 |
+
)
|
| 1043 |
+
yield flush()
|
| 1044 |
+
|
| 1045 |
+
created = _create_product_all_variants_with_grid(
|
| 1046 |
+
logs,
|
| 1047 |
+
shop_id=shop_id,
|
| 1048 |
+
blob=blob,
|
| 1049 |
+
product_info=p_info_first,
|
| 1050 |
+
upload=upload,
|
| 1051 |
+
)
|
| 1052 |
+
created_id = str(created.get("id") or "")
|
| 1053 |
+
if not created_id:
|
| 1054 |
+
raise RuntimeError("Created product response missing id.")
|
| 1055 |
+
yield flush()
|
| 1056 |
+
|
| 1057 |
+
prod0 = _get_product(logs, shop_id, created_id)
|
| 1058 |
+
yield flush()
|
| 1059 |
+
|
| 1060 |
+
provider_runs.append(
|
| 1061 |
+
{
|
| 1062 |
+
"providerId": provider_id,
|
| 1063 |
+
"providerName": provider_name,
|
| 1064 |
+
"chunkIndex": chunk_index,
|
| 1065 |
+
"totalChunks": total_chunks,
|
| 1066 |
+
"catalogVariantCountTotal": total_variants,
|
| 1067 |
+
"catalogVariantCountChunk": len(first_chunk),
|
| 1068 |
+
"shopVariantCountAfterCreate": len(prod0.get("variants") or []),
|
| 1069 |
+
"shopVariantCountFinal": len(prod0.get("variants") or []),
|
| 1070 |
+
"productId": created_id,
|
| 1071 |
+
"priceUpdateResponse": {},
|
| 1072 |
+
}
|
| 1073 |
+
)
|
| 1074 |
+
|
| 1075 |
+
offset = CHUNK_TARGET_VARIANTS
|
| 1076 |
+
chunk_index = 1
|
| 1077 |
+
|
| 1078 |
while offset < total_variants:
|
| 1079 |
chunk = variants_all[offset: offset + CHUNK_TARGET_VARIANTS]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1080 |
|
| 1081 |
_log(
|
| 1082 |
logs,
|
| 1083 |
+
f"PROVIDER_CHUNK_UPDATE provider_id={provider_id} name={provider_name} "
|
| 1084 |
f"chunk_index={chunk_index} total_chunks={total_chunks} "
|
| 1085 |
+
f"offset={offset} size={len(chunk)} total_variants={total_variants} product_id={created_id}",
|
| 1086 |
)
|
| 1087 |
yield flush()
|
| 1088 |
|
| 1089 |
+
upd_enable = _update_product_enable_chunk_with_grid(
|
| 1090 |
logs,
|
| 1091 |
shop_id=shop_id,
|
| 1092 |
+
product_id=created_id,
|
| 1093 |
+
enabled_variants=chunk,
|
| 1094 |
upload=upload,
|
| 1095 |
)
|
|
|
|
|
|
|
|
|
|
| 1096 |
yield flush()
|
| 1097 |
|
| 1098 |
+
prod_u = _get_product(logs, shop_id, created_id)
|
| 1099 |
yield flush()
|
| 1100 |
|
| 1101 |
_log(
|
| 1102 |
logs,
|
| 1103 |
f"COMPARE_COUNTS provider={provider_id} chunk_index={chunk_index} "
|
| 1104 |
f"catalog_chunk={len(chunk)} "
|
| 1105 |
+
f"shop={len(prod_u.get('variants') or [])}",
|
| 1106 |
)
|
| 1107 |
yield flush()
|
| 1108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1109 |
provider_runs.append(
|
| 1110 |
{
|
| 1111 |
"providerId": provider_id,
|
|
|
|
| 1114 |
"totalChunks": total_chunks,
|
| 1115 |
"catalogVariantCountTotal": total_variants,
|
| 1116 |
"catalogVariantCountChunk": len(chunk),
|
| 1117 |
+
"shopVariantCountAfterCreate": len(prod_u.get("variants") or []),
|
| 1118 |
+
"shopVariantCountFinal": len(prod_u.get("variants") or []),
|
| 1119 |
"productId": created_id,
|
| 1120 |
+
"priceUpdateResponse": upd_enable,
|
| 1121 |
}
|
| 1122 |
)
|
| 1123 |
|
| 1124 |
offset += CHUNK_TARGET_VARIANTS
|
| 1125 |
chunk_index += 1
|
| 1126 |
|
| 1127 |
+
prod_before_price = _get_product(logs, shop_id, created_id)
|
| 1128 |
+
yield flush()
|
| 1129 |
+
|
| 1130 |
+
upd_prices = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod_before_price)
|
| 1131 |
+
yield flush()
|
| 1132 |
+
|
| 1133 |
+
prod_after_price = _get_product(logs, shop_id, created_id)
|
| 1134 |
+
yield flush()
|
| 1135 |
+
|
| 1136 |
+
provider_runs.append(
|
| 1137 |
+
{
|
| 1138 |
+
"providerId": provider_id,
|
| 1139 |
+
"providerName": provider_name,
|
| 1140 |
+
"chunkIndex": "PRICE_UPDATE",
|
| 1141 |
+
"totalChunks": total_chunks,
|
| 1142 |
+
"catalogVariantCountTotal": total_variants,
|
| 1143 |
+
"catalogVariantCountChunk": 0,
|
| 1144 |
+
"shopVariantCountAfterCreate": len(prod_before_price.get("variants") or []),
|
| 1145 |
+
"shopVariantCountFinal": len(prod_after_price.get("variants") or []),
|
| 1146 |
+
"productId": created_id,
|
| 1147 |
+
"priceUpdateResponse": upd_prices,
|
| 1148 |
+
}
|
| 1149 |
+
)
|
| 1150 |
+
|
| 1151 |
result["providerRuns"] = provider_runs
|
| 1152 |
_log(logs, "PHASE_B_DONE")
|
| 1153 |
yield flush()
|