Theflame47 commited on
Commit
28b9166
·
verified ·
1 Parent(s): 289034d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -35
app.py CHANGED
@@ -12,6 +12,7 @@ import re
12
  PRINTIFY_BASE = "https://api.printify.com"
13
  DEFAULT_BASE_PRICE = 0.001
14
  GRID_FILENAME = "grid.png"
 
15
 
16
 
17
  def _now() -> str:
@@ -708,46 +709,76 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
708
  _log_catalog_variants(logs, product_info, limit=75)
709
  yield flush()
710
 
711
- created = _create_product_all_variants_with_grid(
712
- logs,
713
- shop_id=shop_id,
714
- blob=blob,
715
- product_info=product_info,
716
- upload=upload,
717
- )
718
- created_id = str(created.get("id") or "")
719
- if not created_id:
720
- raise RuntimeError("Created product response missing id.")
721
- yield flush()
722
 
723
- prod1 = _get_product(logs, shop_id, created_id)
724
- yield flush()
725
 
726
- _log(
727
- logs,
728
- f"COMPARE_COUNTS provider={provider_id} "
729
- f"catalog={len(product_info.get('variants') or [])} "
730
- f"shop={len(prod1.get('variants') or [])}",
731
- )
732
- yield flush()
733
 
734
- upd = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod1)
735
- yield flush()
 
 
 
 
 
736
 
737
- prod2 = _get_product(logs, shop_id, created_id)
738
- yield flush()
 
 
 
 
 
 
 
 
 
739
 
740
- provider_runs.append(
741
- {
742
- "providerId": provider_id,
743
- "providerName": provider_name,
744
- "catalogVariantCount": len(product_info.get("variants") or []),
745
- "shopVariantCountAfterCreate": len(prod1.get("variants") or []),
746
- "shopVariantCountFinal": len(prod2.get("variants") or []),
747
- "productId": created_id,
748
- "priceUpdateResponse": upd,
749
- }
750
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751
 
752
  result["providerRuns"] = provider_runs
753
  _log(logs, "PHASE_B_DONE")
 
12
  PRINTIFY_BASE = "https://api.printify.com"
13
  DEFAULT_BASE_PRICE = 0.001
14
  GRID_FILENAME = "grid.png"
15
+ MAX_VARIANTS_PER_PRODUCT = 100
16
 
17
 
18
  def _now() -> str:
 
709
  _log_catalog_variants(logs, product_info, limit=75)
710
  yield flush()
711
 
712
+ variants_all = product_info.get("variants") or []
713
+ if not isinstance(variants_all, list) or not variants_all:
714
+ _log(logs, f"PROVIDER_SKIP provider_id={provider_id} reason=no_variants_after_build")
715
+ yield flush()
716
+ continue
 
 
 
 
 
 
717
 
718
+ total_variants = len(variants_all)
719
+ total_chunks = (total_variants + MAX_VARIANTS_PER_PRODUCT - 1) // MAX_VARIANTS_PER_PRODUCT
720
 
721
+ chunk_index = 0
722
+ offset = 0
723
+ while offset < total_variants:
724
+ chunk = variants_all[offset: offset + MAX_VARIANTS_PER_PRODUCT]
725
+ p_info_chunk = dict(product_info)
726
+ p_info_chunk["variants"] = chunk
 
727
 
728
+ _log(
729
+ logs,
730
+ f"PROVIDER_CHUNK provider_id={provider_id} name={provider_name} "
731
+ f"chunk_index={chunk_index} total_chunks={total_chunks} "
732
+ f"offset={offset} size={len(chunk)} total_variants={total_variants}",
733
+ )
734
+ yield flush()
735
 
736
+ created = _create_product_all_variants_with_grid(
737
+ logs,
738
+ shop_id=shop_id,
739
+ blob=blob,
740
+ product_info=p_info_chunk,
741
+ upload=upload,
742
+ )
743
+ created_id = str(created.get("id") or "")
744
+ if not created_id:
745
+ raise RuntimeError("Created product response missing id.")
746
+ yield flush()
747
 
748
+ prod1 = _get_product(logs, shop_id, created_id)
749
+ yield flush()
750
+
751
+ _log(
752
+ logs,
753
+ f"COMPARE_COUNTS provider={provider_id} chunk_index={chunk_index} "
754
+ f"catalog_chunk={len(chunk)} "
755
+ f"shop={len(prod1.get('variants') or [])}",
756
+ )
757
+ yield flush()
758
+
759
+ upd = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod1)
760
+ yield flush()
761
+
762
+ prod2 = _get_product(logs, shop_id, created_id)
763
+ yield flush()
764
+
765
+ provider_runs.append(
766
+ {
767
+ "providerId": provider_id,
768
+ "providerName": provider_name,
769
+ "chunkIndex": chunk_index,
770
+ "totalChunks": total_chunks,
771
+ "catalogVariantCountTotal": total_variants,
772
+ "catalogVariantCountChunk": len(chunk),
773
+ "shopVariantCountAfterCreate": len(prod1.get("variants") or []),
774
+ "shopVariantCountFinal": len(prod2.get("variants") or []),
775
+ "productId": created_id,
776
+ "priceUpdateResponse": upd,
777
+ }
778
+ )
779
+
780
+ offset += MAX_VARIANTS_PER_PRODUCT
781
+ chunk_index += 1
782
 
783
  result["providerRuns"] = provider_runs
784
  _log(logs, "PHASE_B_DONE")