Theflame47 commited on
Commit
d2d1fb7
·
verified ·
1 Parent(s): e7d4a8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -204
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import time
3
  import json
@@ -581,112 +583,6 @@ def _create_product_all_variants_with_grid(
581
  return created
582
 
583
 
584
- def _update_product_all_variants_with_grid(
585
- logs: List[str],
586
- shop_id: str,
587
- product_id: str,
588
- all_variants: List[Dict[str, Any]],
589
- enabled_ids: set,
590
- upload: Dict[str, Any],
591
- ) -> Dict[str, Any]:
592
- img_id = upload.get("id")
593
- img_w = float(upload.get("width") or 0)
594
- img_h = float(upload.get("height") or 0)
595
-
596
- variants_payload = []
597
- print_areas_payload = []
598
-
599
- enabled_count = 0
600
- for v in all_variants:
601
- if not isinstance(v, dict):
602
- continue
603
- vid = v.get("id")
604
- if vid is None:
605
- continue
606
- try:
607
- vid_i = int(vid)
608
- except Exception:
609
- continue
610
- is_on = vid_i in enabled_ids
611
- if is_on:
612
- enabled_count += 1
613
- variants_payload.append({
614
- "id": vid_i,
615
- "price": 1,
616
- "is_enabled": bool(is_on),
617
- })
618
-
619
- for v in all_variants:
620
- if not isinstance(v, dict):
621
- continue
622
- vid = v.get("id")
623
- if vid is None:
624
- continue
625
- try:
626
- vid_i = int(vid)
627
- except Exception:
628
- continue
629
- if vid_i not in enabled_ids:
630
- continue
631
-
632
- placeholders = v.get("placeholders") or []
633
- if not isinstance(placeholders, list) or not placeholders:
634
- continue
635
-
636
- ph_payload = []
637
- for ph in placeholders:
638
- if not isinstance(ph, dict):
639
- continue
640
- pos = ph.get("position")
641
- pw = ph.get("width")
642
- phh = ph.get("height")
643
- if not pos or pw is None or phh is None:
644
- continue
645
- try:
646
- pwf = float(pw)
647
- phf = float(phh)
648
- except Exception:
649
- continue
650
- scale = _scale_fill(pwf, phf, img_w, img_h)
651
- ph_payload.append({
652
- "position": pos,
653
- "images": [{
654
- "id": img_id,
655
- "x": 0.5,
656
- "y": 0.5,
657
- "scale": scale,
658
- "angle": 0,
659
- }],
660
- })
661
-
662
- if ph_payload:
663
- print_areas_payload.append({
664
- "variant_ids": [vid_i],
665
- "placeholders": ph_payload,
666
- })
667
-
668
- if not variants_payload:
669
- raise RuntimeError("No variants payload could be built for update.")
670
-
671
- _log(
672
- logs,
673
- f"PHASE_B_UPDATE product_id={product_id} variants_payload_total={len(variants_payload)} "
674
- f"enabled={enabled_count} print_areas_payload={len(print_areas_payload)}",
675
- )
676
-
677
- upd = _req(
678
- "PUT",
679
- f"/v1/shops/{shop_id}/products/{product_id}.json",
680
- json_body={"variants": variants_payload, "print_areas": print_areas_payload},
681
- logs=logs,
682
- )
683
- if not isinstance(upd, dict):
684
- raise RuntimeError(f"Unexpected update response: {str(upd)[:500]}")
685
-
686
- _log(logs, f"PHASE_B_UPDATE_DONE product_id={product_id}")
687
- return upd
688
-
689
-
690
  def _get_product(logs: List[str], shop_id: str, product_id: str) -> Dict[str, Any]:
691
  prod = _req("GET", f"/v1/shops/{shop_id}/products/{product_id}.json", logs=logs)
692
  if not isinstance(prod, dict):
@@ -876,19 +772,8 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
876
  continue
877
 
878
  total_variants = len(variants_all)
879
- threshold = int(CHUNK_TARGET_VARIANTS)
880
-
881
- _log(
882
- logs,
883
- f"THRESHOLD_CHECK provider_id={provider_id} total_variants={total_variants} "
884
- f"threshold={threshold} hard_max={HARD_MAX_VARIANTS_PER_PRODUCT}",
885
- )
886
- yield flush()
887
-
888
- if total_variants <= threshold:
889
- _log(logs, f"WITHIN_THRESHOLD provider_id={provider_id} total_variants={total_variants} threshold={threshold}")
890
- yield flush()
891
 
 
892
  chunk_index = 0
893
  total_chunks = 1
894
  p_info_chunk = dict(product_info_full)
@@ -949,104 +834,71 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
949
  )
950
 
951
  else:
952
- _log(logs, f"OUTSIDE_THRESHOLD provider_id={provider_id} total_variants={total_variants} threshold={threshold}")
953
- yield flush()
 
 
 
 
 
 
 
 
954
 
955
- cap = int(HARD_MAX_VARIANTS_PER_PRODUCT)
956
- if total_variants > cap:
957
  _log(
958
  logs,
959
- f"CAP_ENFORCED provider_id={provider_id} total_variants={total_variants} cap={cap} dropped={total_variants - cap}",
 
 
960
  )
961
  yield flush()
962
- variants_all = variants_all[:cap]
963
- total_variants = len(variants_all)
964
-
965
- first_chunk = variants_all[:threshold]
966
- surplus = variants_all[threshold:]
967
-
968
- enabled_ids_create = [int(v.get("id")) for v in first_chunk if v.get("id") is not None]
969
- enabled_ids_final = [int(v.get("id")) for v in variants_all if v.get("id") is not None]
970
-
971
- _log(
972
- logs,
973
- f"SURPLUS_COUNT provider_id={provider_id} create_enabled={len(enabled_ids_create)} "
974
- f"surplus={len(surplus)} final_enabled={len(enabled_ids_final)}",
975
- )
976
- _log(
977
- logs,
978
- f"SURPLUS_IDS_SAMPLE provider_id={provider_id} ids={json.dumps(enabled_ids_final[threshold:threshold+25])}",
979
- )
980
- yield flush()
981
-
982
- p_info_create = dict(product_info_full)
983
- p_info_create["variants"] = first_chunk
984
- p_info_create["options"] = _rebuild_options_for_variants(first_chunk)
985
- p_info_create["_allVariants"] = variants_all
986
- p_info_create["_enabledVariantIds"] = enabled_ids_create
987
-
988
- _log(
989
- logs,
990
- f"PROVIDER_CREATE_THEN_UPDATE provider_id={provider_id} name={provider_name} "
991
- f"create_enabled={len(first_chunk)} total_variants={total_variants}",
992
- )
993
- yield flush()
994
-
995
- created = _create_product_all_variants_with_grid(
996
- logs,
997
- shop_id=shop_id,
998
- blob=blob,
999
- product_info=p_info_create,
1000
- upload=upload,
1001
- )
1002
- created_id = str(created.get("id") or "")
1003
- if not created_id:
1004
- raise RuntimeError("Created product response missing id.")
1005
- yield flush()
1006
 
1007
- enabled_set_final = set(enabled_ids_final)
 
 
 
 
 
 
 
 
 
 
1008
 
1009
- upd_variants = _update_product_all_variants_with_grid(
1010
- logs,
1011
- shop_id=shop_id,
1012
- product_id=created_id,
1013
- all_variants=variants_all,
1014
- enabled_ids=enabled_set_final,
1015
- upload=upload,
1016
- )
1017
- yield flush()
1018
 
1019
- prod1 = _get_product(logs, shop_id, created_id)
1020
- yield flush()
 
 
 
 
 
1021
 
1022
- _log(
1023
- logs,
1024
- f"COMPARE_COUNTS provider={provider_id} chunk_index=THRESHOLD_FALLBACK "
1025
- f"catalog_chunk={total_variants} "
1026
- f"shop={len(prod1.get('variants') or [])}",
1027
- )
1028
- yield flush()
1029
 
1030
- upd = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod1)
1031
- yield flush()
1032
 
1033
- prod2 = _get_product(logs, shop_id, created_id)
1034
- yield flush()
 
 
 
 
 
 
 
 
 
 
 
 
1035
 
1036
- provider_runs.append(
1037
- {
1038
- "providerId": provider_id,
1039
- "providerName": provider_name,
1040
- "chunkIndex": "THRESHOLD_FALLBACK",
1041
- "totalChunks": 1,
1042
- "catalogVariantCountTotal": total_variants,
1043
- "catalogVariantCountChunk": total_variants,
1044
- "shopVariantCountAfterCreate": len(prod1.get("variants") or []),
1045
- "shopVariantCountFinal": len(prod2.get("variants") or []),
1046
- "productId": created_id,
1047
- "priceUpdateResponse": {"enableUpdate": upd_variants, "priceUpdate": upd},
1048
- }
1049
- )
1050
 
1051
  result["providerRuns"] = provider_runs
1052
  _log(logs, "PHASE_B_DONE")
 
1
+ I am going to give you the script. Before modifying it, recite to me what we're updating briefly.
2
+
3
  import os
4
  import time
5
  import json
 
583
  return created
584
 
585
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586
  def _get_product(logs: List[str], shop_id: str, product_id: str) -> Dict[str, Any]:
587
  prod = _req("GET", f"/v1/shops/{shop_id}/products/{product_id}.json", logs=logs)
588
  if not isinstance(prod, dict):
 
772
  continue
773
 
774
  total_variants = len(variants_all)
 
 
 
 
 
 
 
 
 
 
 
 
775
 
776
+ if total_variants <= HARD_MAX_VARIANTS_PER_PRODUCT:
777
  chunk_index = 0
778
  total_chunks = 1
779
  p_info_chunk = dict(product_info_full)
 
834
  )
835
 
836
  else:
837
+ total_chunks = (total_variants + CHUNK_TARGET_VARIANTS - 1) // CHUNK_TARGET_VARIANTS
838
+ chunk_index = 0
839
+ offset = 0
840
+ while offset < total_variants:
841
+ chunk = variants_all[offset: offset + CHUNK_TARGET_VARIANTS]
842
+ p_info_chunk = dict(product_info_full)
843
+ p_info_chunk["variants"] = chunk
844
+ p_info_chunk["options"] = _rebuild_options_for_variants(chunk)
845
+ p_info_chunk["_allVariants"] = variants_all
846
+ p_info_chunk["_enabledVariantIds"] = [int(v.get("id")) for v in chunk if v.get("id") is not None]
847
 
 
 
848
  _log(
849
  logs,
850
+ f"PROVIDER_CHUNK provider_id={provider_id} name={provider_name} "
851
+ f"chunk_index={chunk_index} total_chunks={total_chunks} "
852
+ f"offset={offset} size={len(chunk)} total_variants={total_variants}",
853
  )
854
  yield flush()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
855
 
856
+ created = _create_product_all_variants_with_grid(
857
+ logs,
858
+ shop_id=shop_id,
859
+ blob=blob,
860
+ product_info=p_info_chunk,
861
+ upload=upload,
862
+ )
863
+ created_id = str(created.get("id") or "")
864
+ if not created_id:
865
+ raise RuntimeError("Created product response missing id.")
866
+ yield flush()
867
 
868
+ prod1 = _get_product(logs, shop_id, created_id)
869
+ yield flush()
 
 
 
 
 
 
 
870
 
871
+ _log(
872
+ logs,
873
+ f"COMPARE_COUNTS provider={provider_id} chunk_index={chunk_index} "
874
+ f"catalog_chunk={len(chunk)} "
875
+ f"shop={len(prod1.get('variants') or [])}",
876
+ )
877
+ yield flush()
878
 
879
+ upd = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod1)
880
+ yield flush()
 
 
 
 
 
881
 
882
+ prod2 = _get_product(logs, shop_id, created_id)
883
+ yield flush()
884
 
885
+ provider_runs.append(
886
+ {
887
+ "providerId": provider_id,
888
+ "providerName": provider_name,
889
+ "chunkIndex": chunk_index,
890
+ "totalChunks": total_chunks,
891
+ "catalogVariantCountTotal": total_variants,
892
+ "catalogVariantCountChunk": len(chunk),
893
+ "shopVariantCountAfterCreate": len(prod1.get("variants") or []),
894
+ "shopVariantCountFinal": len(prod2.get("variants") or []),
895
+ "productId": created_id,
896
+ "priceUpdateResponse": upd,
897
+ }
898
+ )
899
 
900
+ offset += CHUNK_TARGET_VARIANTS
901
+ chunk_index += 1
 
 
 
 
 
 
 
 
 
 
 
 
902
 
903
  result["providerRuns"] = provider_runs
904
  _log(logs, "PHASE_B_DONE")