Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -581,6 +581,114 @@ def _create_product_all_variants_with_grid(
|
|
| 581 |
return created
|
| 582 |
|
| 583 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
def _get_product(logs: List[str], shop_id: str, product_id: str) -> Dict[str, Any]:
|
| 585 |
prod = _req("GET", f"/v1/shops/{shop_id}/products/{product_id}.json", logs=logs)
|
| 586 |
if not isinstance(prod, dict):
|
|
@@ -770,8 +878,16 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 770 |
continue
|
| 771 |
|
| 772 |
total_variants = len(variants_all)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 773 |
|
| 774 |
-
if
|
| 775 |
chunk_index = 0
|
| 776 |
total_chunks = 1
|
| 777 |
p_info_chunk = dict(product_info_full)
|
|
@@ -833,51 +949,87 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 833 |
|
| 834 |
else:
|
| 835 |
total_chunks = (total_variants + CHUNK_TARGET_VARIANTS - 1) // CHUNK_TARGET_VARIANTS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 836 |
chunk_index = 0
|
| 837 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 838 |
while offset < total_variants:
|
| 839 |
chunk = variants_all[offset: offset + CHUNK_TARGET_VARIANTS]
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
p_info_chunk["_enabledVariantIds"] = [int(v.get("id")) for v in chunk if v.get("id") is not None]
|
| 845 |
|
| 846 |
_log(
|
| 847 |
logs,
|
| 848 |
-
f"
|
| 849 |
f"chunk_index={chunk_index} total_chunks={total_chunks} "
|
| 850 |
-
f"offset={offset} size={len(chunk)}
|
| 851 |
)
|
| 852 |
yield flush()
|
| 853 |
|
| 854 |
-
|
| 855 |
logs,
|
| 856 |
shop_id=shop_id,
|
| 857 |
-
|
| 858 |
-
|
| 859 |
upload=upload,
|
| 860 |
)
|
| 861 |
-
created_id = str(created.get("id") or "")
|
| 862 |
-
if not created_id:
|
| 863 |
-
raise RuntimeError("Created product response missing id.")
|
| 864 |
-
yield flush()
|
| 865 |
-
|
| 866 |
-
prod1 = _get_product(logs, shop_id, created_id)
|
| 867 |
-
yield flush()
|
| 868 |
-
|
| 869 |
-
_log(
|
| 870 |
-
logs,
|
| 871 |
-
f"COMPARE_COUNTS provider={provider_id} chunk_index={chunk_index} "
|
| 872 |
-
f"catalog_chunk={len(chunk)} "
|
| 873 |
-
f"shop={len(prod1.get('variants') or [])}",
|
| 874 |
-
)
|
| 875 |
-
yield flush()
|
| 876 |
-
|
| 877 |
-
upd = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod1)
|
| 878 |
-
yield flush()
|
| 879 |
-
|
| 880 |
-
prod2 = _get_product(logs, shop_id, created_id)
|
| 881 |
yield flush()
|
| 882 |
|
| 883 |
provider_runs.append(
|
|
@@ -888,16 +1040,48 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 888 |
"totalChunks": total_chunks,
|
| 889 |
"catalogVariantCountTotal": total_variants,
|
| 890 |
"catalogVariantCountChunk": len(chunk),
|
| 891 |
-
"shopVariantCountAfterCreate":
|
| 892 |
-
"shopVariantCountFinal":
|
| 893 |
"productId": created_id,
|
| 894 |
-
"priceUpdateResponse":
|
| 895 |
}
|
| 896 |
)
|
| 897 |
|
| 898 |
offset += CHUNK_TARGET_VARIANTS
|
| 899 |
chunk_index += 1
|
| 900 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 901 |
result["providerRuns"] = provider_runs
|
| 902 |
_log(logs, "PHASE_B_DONE")
|
| 903 |
yield flush()
|
|
|
|
| 581 |
return created
|
| 582 |
|
| 583 |
|
| 584 |
+
def _update_product_enable_variants_with_grid(
|
| 585 |
+
logs: List[str],
|
| 586 |
+
shop_id: str,
|
| 587 |
+
product_id: str,
|
| 588 |
+
variants_to_enable: List[Dict[str, Any]],
|
| 589 |
+
upload: Dict[str, Any],
|
| 590 |
+
) -> Dict[str, Any]:
|
| 591 |
+
img_id = upload.get("id")
|
| 592 |
+
img_w = float(upload.get("width") or 0)
|
| 593 |
+
img_h = float(upload.get("height") or 0)
|
| 594 |
+
|
| 595 |
+
variants_payload = []
|
| 596 |
+
print_areas_payload = []
|
| 597 |
+
|
| 598 |
+
enable_attempted = 0
|
| 599 |
+
enable_ready = 0
|
| 600 |
+
skipped_no_placeholders = 0
|
| 601 |
+
skipped_bad_placeholder = 0
|
| 602 |
+
|
| 603 |
+
for v in variants_to_enable:
|
| 604 |
+
if not isinstance(v, dict):
|
| 605 |
+
continue
|
| 606 |
+
vid = v.get("id")
|
| 607 |
+
if vid is None:
|
| 608 |
+
continue
|
| 609 |
+
try:
|
| 610 |
+
vid_i = int(vid)
|
| 611 |
+
except Exception:
|
| 612 |
+
continue
|
| 613 |
+
|
| 614 |
+
enable_attempted += 1
|
| 615 |
+
|
| 616 |
+
placeholders = v.get("placeholders") or []
|
| 617 |
+
if not isinstance(placeholders, list) or not placeholders:
|
| 618 |
+
skipped_no_placeholders += 1
|
| 619 |
+
continue
|
| 620 |
+
|
| 621 |
+
ph_payload = []
|
| 622 |
+
for ph in placeholders:
|
| 623 |
+
if not isinstance(ph, dict):
|
| 624 |
+
continue
|
| 625 |
+
pos = ph.get("position")
|
| 626 |
+
pw = ph.get("width")
|
| 627 |
+
phh = ph.get("height")
|
| 628 |
+
if not pos or pw is None or phh is None:
|
| 629 |
+
skipped_bad_placeholder += 1
|
| 630 |
+
continue
|
| 631 |
+
try:
|
| 632 |
+
pwf = float(pw)
|
| 633 |
+
phf = float(phh)
|
| 634 |
+
except Exception:
|
| 635 |
+
skipped_bad_placeholder += 1
|
| 636 |
+
continue
|
| 637 |
+
scale = _scale_fill(pwf, phf, img_w, img_h)
|
| 638 |
+
ph_payload.append({
|
| 639 |
+
"position": pos,
|
| 640 |
+
"images": [{
|
| 641 |
+
"id": img_id,
|
| 642 |
+
"x": 0.5,
|
| 643 |
+
"y": 0.5,
|
| 644 |
+
"scale": scale,
|
| 645 |
+
"angle": 0,
|
| 646 |
+
}],
|
| 647 |
+
})
|
| 648 |
+
|
| 649 |
+
if not ph_payload:
|
| 650 |
+
skipped_bad_placeholder += 1
|
| 651 |
+
continue
|
| 652 |
+
|
| 653 |
+
enable_ready += 1
|
| 654 |
+
variants_payload.append({
|
| 655 |
+
"id": vid_i,
|
| 656 |
+
"price": 1,
|
| 657 |
+
"is_enabled": True,
|
| 658 |
+
})
|
| 659 |
+
print_areas_payload.append({
|
| 660 |
+
"variant_ids": [vid_i],
|
| 661 |
+
"placeholders": ph_payload,
|
| 662 |
+
})
|
| 663 |
+
|
| 664 |
+
if not variants_payload or not print_areas_payload:
|
| 665 |
+
raise RuntimeError(
|
| 666 |
+
f"Update chunk produced no enableable variants. attempted={enable_attempted} "
|
| 667 |
+
f"ready={enable_ready} skipped_no_placeholders={skipped_no_placeholders} skipped_bad_placeholder={skipped_bad_placeholder}"
|
| 668 |
+
)
|
| 669 |
+
|
| 670 |
+
_log(
|
| 671 |
+
logs,
|
| 672 |
+
f"PHASE_B_UPDATE product_id={product_id} enable_attempted={enable_attempted} enable_ready={enable_ready} "
|
| 673 |
+
f"print_areas_payload={len(print_areas_payload)} skipped_no_placeholders={skipped_no_placeholders} skipped_bad_placeholder={skipped_bad_placeholder}",
|
| 674 |
+
)
|
| 675 |
+
|
| 676 |
+
upd = _req(
|
| 677 |
+
"PUT",
|
| 678 |
+
f"/v1/shops/{shop_id}/products/{product_id}.json",
|
| 679 |
+
json_body={
|
| 680 |
+
"variants": variants_payload,
|
| 681 |
+
"print_areas": print_areas_payload,
|
| 682 |
+
},
|
| 683 |
+
logs=logs,
|
| 684 |
+
)
|
| 685 |
+
if not isinstance(upd, dict):
|
| 686 |
+
raise RuntimeError(f"Unexpected update response: {str(upd)[:500]}")
|
| 687 |
+
|
| 688 |
+
_log(logs, f"PHASE_B_UPDATED product_id={product_id} enabled_in_update={len(variants_payload)}")
|
| 689 |
+
return upd
|
| 690 |
+
|
| 691 |
+
|
| 692 |
def _get_product(logs: List[str], shop_id: str, product_id: str) -> Dict[str, Any]:
|
| 693 |
prod = _req("GET", f"/v1/shops/{shop_id}/products/{product_id}.json", logs=logs)
|
| 694 |
if not isinstance(prod, dict):
|
|
|
|
| 878 |
continue
|
| 879 |
|
| 880 |
total_variants = len(variants_all)
|
| 881 |
+
within_threshold = total_variants <= CHUNK_TARGET_VARIANTS
|
| 882 |
+
|
| 883 |
+
_log(
|
| 884 |
+
logs,
|
| 885 |
+
f"THRESHOLD_CHECK threshold={CHUNK_TARGET_VARIANTS} total_variants={total_variants} "
|
| 886 |
+
f"flag={'WITHIN_THRESHOLD' if within_threshold else 'OUTSIDE_THRESHOLD'}",
|
| 887 |
+
)
|
| 888 |
+
yield flush()
|
| 889 |
|
| 890 |
+
if within_threshold:
|
| 891 |
chunk_index = 0
|
| 892 |
total_chunks = 1
|
| 893 |
p_info_chunk = dict(product_info_full)
|
|
|
|
| 949 |
|
| 950 |
else:
|
| 951 |
total_chunks = (total_variants + CHUNK_TARGET_VARIANTS - 1) // CHUNK_TARGET_VARIANTS
|
| 952 |
+
|
| 953 |
+
first_chunk = variants_all[0:CHUNK_TARGET_VARIANTS]
|
| 954 |
+
surplus_variants = variants_all[CHUNK_TARGET_VARIANTS:]
|
| 955 |
+
|
| 956 |
+
surplus_ids = []
|
| 957 |
+
for v in surplus_variants[:50]:
|
| 958 |
+
if isinstance(v, dict) and v.get("id") is not None:
|
| 959 |
+
surplus_ids.append(v.get("id"))
|
| 960 |
+
_log(
|
| 961 |
+
logs,
|
| 962 |
+
f"OUTSIDE_THRESHOLD_SURPLUS threshold={CHUNK_TARGET_VARIANTS} total_variants={total_variants} "
|
| 963 |
+
f"surplus_count={len(surplus_variants)} surplus_ids_sample={json.dumps(surplus_ids)}",
|
| 964 |
+
)
|
| 965 |
+
yield flush()
|
| 966 |
+
|
| 967 |
+
p_info_first = dict(product_info_full)
|
| 968 |
+
p_info_first["variants"] = first_chunk
|
| 969 |
+
p_info_first["options"] = _rebuild_options_for_variants(first_chunk)
|
| 970 |
+
p_info_first["_allVariants"] = variants_all
|
| 971 |
+
p_info_first["_enabledVariantIds"] = [int(v.get("id")) for v in first_chunk if v.get("id") is not None]
|
| 972 |
+
|
| 973 |
+
_log(
|
| 974 |
+
logs,
|
| 975 |
+
f"PROVIDER_CREATE_THEN_UPDATE provider_id={provider_id} name={provider_name} "
|
| 976 |
+
f"first_chunk_size={len(first_chunk)} total_chunks={total_chunks} total_variants={total_variants}",
|
| 977 |
+
)
|
| 978 |
+
yield flush()
|
| 979 |
+
|
| 980 |
+
created = _create_product_all_variants_with_grid(
|
| 981 |
+
logs,
|
| 982 |
+
shop_id=shop_id,
|
| 983 |
+
blob=blob,
|
| 984 |
+
product_info=p_info_first,
|
| 985 |
+
upload=upload,
|
| 986 |
+
)
|
| 987 |
+
created_id = str(created.get("id") or "")
|
| 988 |
+
if not created_id:
|
| 989 |
+
raise RuntimeError("Created product response missing id.")
|
| 990 |
+
yield flush()
|
| 991 |
+
|
| 992 |
chunk_index = 0
|
| 993 |
+
provider_runs.append(
|
| 994 |
+
{
|
| 995 |
+
"providerId": provider_id,
|
| 996 |
+
"providerName": provider_name,
|
| 997 |
+
"chunkIndex": chunk_index,
|
| 998 |
+
"totalChunks": total_chunks,
|
| 999 |
+
"catalogVariantCountTotal": total_variants,
|
| 1000 |
+
"catalogVariantCountChunk": len(first_chunk),
|
| 1001 |
+
"shopVariantCountAfterCreate": 0,
|
| 1002 |
+
"shopVariantCountFinal": 0,
|
| 1003 |
+
"productId": created_id,
|
| 1004 |
+
"priceUpdateResponse": {},
|
| 1005 |
+
}
|
| 1006 |
+
)
|
| 1007 |
+
|
| 1008 |
+
offset = CHUNK_TARGET_VARIANTS
|
| 1009 |
+
chunk_index = 1
|
| 1010 |
+
|
| 1011 |
while offset < total_variants:
|
| 1012 |
chunk = variants_all[offset: offset + CHUNK_TARGET_VARIANTS]
|
| 1013 |
+
chunk_ids = []
|
| 1014 |
+
for v in chunk[:50]:
|
| 1015 |
+
if isinstance(v, dict) and v.get("id") is not None:
|
| 1016 |
+
chunk_ids.append(v.get("id"))
|
|
|
|
| 1017 |
|
| 1018 |
_log(
|
| 1019 |
logs,
|
| 1020 |
+
f"PROVIDER_UPDATE_CHUNK provider_id={provider_id} name={provider_name} "
|
| 1021 |
f"chunk_index={chunk_index} total_chunks={total_chunks} "
|
| 1022 |
+
f"offset={offset} size={len(chunk)} product_id={created_id} ids_sample={json.dumps(chunk_ids)}",
|
| 1023 |
)
|
| 1024 |
yield flush()
|
| 1025 |
|
| 1026 |
+
upd_chunk = _update_product_enable_variants_with_grid(
|
| 1027 |
logs,
|
| 1028 |
shop_id=shop_id,
|
| 1029 |
+
product_id=created_id,
|
| 1030 |
+
variants_to_enable=chunk,
|
| 1031 |
upload=upload,
|
| 1032 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1033 |
yield flush()
|
| 1034 |
|
| 1035 |
provider_runs.append(
|
|
|
|
| 1040 |
"totalChunks": total_chunks,
|
| 1041 |
"catalogVariantCountTotal": total_variants,
|
| 1042 |
"catalogVariantCountChunk": len(chunk),
|
| 1043 |
+
"shopVariantCountAfterCreate": 0,
|
| 1044 |
+
"shopVariantCountFinal": 0,
|
| 1045 |
"productId": created_id,
|
| 1046 |
+
"priceUpdateResponse": upd_chunk,
|
| 1047 |
}
|
| 1048 |
)
|
| 1049 |
|
| 1050 |
offset += CHUNK_TARGET_VARIANTS
|
| 1051 |
chunk_index += 1
|
| 1052 |
|
| 1053 |
+
prod1 = _get_product(logs, shop_id, created_id)
|
| 1054 |
+
yield flush()
|
| 1055 |
+
|
| 1056 |
+
_log(
|
| 1057 |
+
logs,
|
| 1058 |
+
f"COMPARE_COUNTS provider={provider_id} chunk_index=FINAL "
|
| 1059 |
+
f"catalog_total={len(variants_all)} "
|
| 1060 |
+
f"shop={len(prod1.get('variants') or [])}",
|
| 1061 |
+
)
|
| 1062 |
+
yield flush()
|
| 1063 |
+
|
| 1064 |
+
upd_prices = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod1)
|
| 1065 |
+
yield flush()
|
| 1066 |
+
|
| 1067 |
+
prod2 = _get_product(logs, shop_id, created_id)
|
| 1068 |
+
yield flush()
|
| 1069 |
+
|
| 1070 |
+
provider_runs.append(
|
| 1071 |
+
{
|
| 1072 |
+
"providerId": provider_id,
|
| 1073 |
+
"providerName": provider_name,
|
| 1074 |
+
"chunkIndex": "PRICE_UPDATE",
|
| 1075 |
+
"totalChunks": total_chunks,
|
| 1076 |
+
"catalogVariantCountTotal": total_variants,
|
| 1077 |
+
"catalogVariantCountChunk": 0,
|
| 1078 |
+
"shopVariantCountAfterCreate": len(prod1.get("variants") or []),
|
| 1079 |
+
"shopVariantCountFinal": len(prod2.get("variants") or []),
|
| 1080 |
+
"productId": created_id,
|
| 1081 |
+
"priceUpdateResponse": upd_prices,
|
| 1082 |
+
}
|
| 1083 |
+
)
|
| 1084 |
+
|
| 1085 |
result["providerRuns"] = provider_runs
|
| 1086 |
_log(logs, "PHASE_B_DONE")
|
| 1087 |
yield flush()
|