Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,9 @@ import re
|
|
| 12 |
PRINTIFY_BASE = "https://api.printify.com"
|
| 13 |
DEFAULT_BASE_PRICE = 0.001
|
| 14 |
GRID_FILENAME = "grid.png"
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
def _now() -> str:
|
|
@@ -615,6 +617,15 @@ def _update_prices_to_cost_plus_margin(
|
|
| 615 |
return upd
|
| 616 |
|
| 617 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 618 |
def run(currency: str) -> Generator[Tuple[str, str], None, None]:
|
| 619 |
logs: List[str] = []
|
| 620 |
result: Dict[str, Any] = {}
|
|
@@ -705,31 +716,29 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 705 |
)
|
| 706 |
yield flush()
|
| 707 |
|
| 708 |
-
|
| 709 |
-
_log_catalog_variants(logs,
|
| 710 |
yield flush()
|
| 711 |
|
| 712 |
-
variants_all =
|
| 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 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
p_info_chunk =
|
| 726 |
-
p_info_chunk["
|
| 727 |
|
| 728 |
_log(
|
| 729 |
logs,
|
| 730 |
-
f"
|
| 731 |
-
f"
|
| 732 |
-
f"offset={offset} size={len(chunk)} total_variants={total_variants}",
|
| 733 |
)
|
| 734 |
yield flush()
|
| 735 |
|
|
@@ -751,7 +760,7 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 751 |
_log(
|
| 752 |
logs,
|
| 753 |
f"COMPARE_COUNTS provider={provider_id} chunk_index={chunk_index} "
|
| 754 |
-
f"catalog_chunk={len(
|
| 755 |
f"shop={len(prod1.get('variants') or [])}",
|
| 756 |
)
|
| 757 |
yield flush()
|
|
@@ -769,7 +778,7 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 769 |
"chunkIndex": chunk_index,
|
| 770 |
"totalChunks": total_chunks,
|
| 771 |
"catalogVariantCountTotal": total_variants,
|
| 772 |
-
"catalogVariantCountChunk": len(
|
| 773 |
"shopVariantCountAfterCreate": len(prod1.get("variants") or []),
|
| 774 |
"shopVariantCountFinal": len(prod2.get("variants") or []),
|
| 775 |
"productId": created_id,
|
|
@@ -777,8 +786,70 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 777 |
}
|
| 778 |
)
|
| 779 |
|
| 780 |
-
|
| 781 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 782 |
|
| 783 |
result["providerRuns"] = provider_runs
|
| 784 |
_log(logs, "PHASE_B_DONE")
|
|
|
|
| 12 |
PRINTIFY_BASE = "https://api.printify.com"
|
| 13 |
DEFAULT_BASE_PRICE = 0.001
|
| 14 |
GRID_FILENAME = "grid.png"
|
| 15 |
+
|
| 16 |
+
HARD_MAX_VARIANTS_PER_PRODUCT = 100
|
| 17 |
+
CHUNK_TARGET_VARIANTS = 85
|
| 18 |
|
| 19 |
|
| 20 |
def _now() -> str:
|
|
|
|
| 617 |
return upd
|
| 618 |
|
| 619 |
|
| 620 |
+
def _rebuild_options_for_variants(variants: List[Dict[str, Any]]) -> Dict[str, List[str]]:
|
| 621 |
+
colors = sorted({v.get("color") for v in variants if v.get("color")})
|
| 622 |
+
sizes = sorted({v.get("size") for v in variants if v.get("size")})
|
| 623 |
+
return {
|
| 624 |
+
"Color": colors,
|
| 625 |
+
"Size": sizes,
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
|
| 629 |
def run(currency: str) -> Generator[Tuple[str, str], None, None]:
|
| 630 |
logs: List[str] = []
|
| 631 |
result: Dict[str, Any] = {}
|
|
|
|
| 716 |
)
|
| 717 |
yield flush()
|
| 718 |
|
| 719 |
+
product_info_full = _build_product(blob, currency_code, logs)
|
| 720 |
+
_log_catalog_variants(logs, product_info_full, limit=75)
|
| 721 |
yield flush()
|
| 722 |
|
| 723 |
+
variants_all = product_info_full.get("variants") or []
|
| 724 |
if not isinstance(variants_all, list) or not variants_all:
|
| 725 |
_log(logs, f"PROVIDER_SKIP provider_id={provider_id} reason=no_variants_after_build")
|
| 726 |
yield flush()
|
| 727 |
continue
|
| 728 |
|
| 729 |
total_variants = len(variants_all)
|
|
|
|
| 730 |
|
| 731 |
+
if total_variants <= HARD_MAX_VARIANTS_PER_PRODUCT:
|
| 732 |
+
chunk_index = 0
|
| 733 |
+
total_chunks = 1
|
| 734 |
+
p_info_chunk = dict(product_info_full)
|
| 735 |
+
p_info_chunk["variants"] = variants_all
|
| 736 |
+
p_info_chunk["options"] = _rebuild_options_for_variants(variants_all)
|
| 737 |
|
| 738 |
_log(
|
| 739 |
logs,
|
| 740 |
+
f"PROVIDER_SINGLE product provider_id={provider_id} name={provider_name} "
|
| 741 |
+
f"variants={total_variants}",
|
|
|
|
| 742 |
)
|
| 743 |
yield flush()
|
| 744 |
|
|
|
|
| 760 |
_log(
|
| 761 |
logs,
|
| 762 |
f"COMPARE_COUNTS provider={provider_id} chunk_index={chunk_index} "
|
| 763 |
+
f"catalog_chunk={len(variants_all)} "
|
| 764 |
f"shop={len(prod1.get('variants') or [])}",
|
| 765 |
)
|
| 766 |
yield flush()
|
|
|
|
| 778 |
"chunkIndex": chunk_index,
|
| 779 |
"totalChunks": total_chunks,
|
| 780 |
"catalogVariantCountTotal": total_variants,
|
| 781 |
+
"catalogVariantCountChunk": len(variants_all),
|
| 782 |
"shopVariantCountAfterCreate": len(prod1.get("variants") or []),
|
| 783 |
"shopVariantCountFinal": len(prod2.get("variants") or []),
|
| 784 |
"productId": created_id,
|
|
|
|
| 786 |
}
|
| 787 |
)
|
| 788 |
|
| 789 |
+
else:
|
| 790 |
+
total_chunks = (total_variants + CHUNK_TARGET_VARIANTS - 1) // CHUNK_TARGET_VARIANTS
|
| 791 |
+
chunk_index = 0
|
| 792 |
+
offset = 0
|
| 793 |
+
while offset < total_variants:
|
| 794 |
+
chunk = variants_all[offset: offset + CHUNK_TARGET_VARIANTS]
|
| 795 |
+
p_info_chunk = dict(product_info_full)
|
| 796 |
+
p_info_chunk["variants"] = chunk
|
| 797 |
+
p_info_chunk["options"] = _rebuild_options_for_variants(chunk)
|
| 798 |
+
|
| 799 |
+
_log(
|
| 800 |
+
logs,
|
| 801 |
+
f"PROVIDER_CHUNK provider_id={provider_id} name={provider_name} "
|
| 802 |
+
f"chunk_index={chunk_index} total_chunks={total_chunks} "
|
| 803 |
+
f"offset={offset} size={len(chunk)} total_variants={total_variants}",
|
| 804 |
+
)
|
| 805 |
+
yield flush()
|
| 806 |
+
|
| 807 |
+
created = _create_product_all_variants_with_grid(
|
| 808 |
+
logs,
|
| 809 |
+
shop_id=shop_id,
|
| 810 |
+
blob=blob,
|
| 811 |
+
product_info=p_info_chunk,
|
| 812 |
+
upload=upload,
|
| 813 |
+
)
|
| 814 |
+
created_id = str(created.get("id") or "")
|
| 815 |
+
if not created_id:
|
| 816 |
+
raise RuntimeError("Created product response missing id.")
|
| 817 |
+
yield flush()
|
| 818 |
+
|
| 819 |
+
prod1 = _get_product(logs, shop_id, created_id)
|
| 820 |
+
yield flush()
|
| 821 |
+
|
| 822 |
+
_log(
|
| 823 |
+
logs,
|
| 824 |
+
f"COMPARE_COUNTS provider={provider_id} chunk_index={chunk_index} "
|
| 825 |
+
f"catalog_chunk={len(chunk)} "
|
| 826 |
+
f"shop={len(prod1.get('variants') or [])}",
|
| 827 |
+
)
|
| 828 |
+
yield flush()
|
| 829 |
+
|
| 830 |
+
upd = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod1)
|
| 831 |
+
yield flush()
|
| 832 |
+
|
| 833 |
+
prod2 = _get_product(logs, shop_id, created_id)
|
| 834 |
+
yield flush()
|
| 835 |
+
|
| 836 |
+
provider_runs.append(
|
| 837 |
+
{
|
| 838 |
+
"providerId": provider_id,
|
| 839 |
+
"providerName": provider_name,
|
| 840 |
+
"chunkIndex": chunk_index,
|
| 841 |
+
"totalChunks": total_chunks,
|
| 842 |
+
"catalogVariantCountTotal": total_variants,
|
| 843 |
+
"catalogVariantCountChunk": len(chunk),
|
| 844 |
+
"shopVariantCountAfterCreate": len(prod1.get("variants") or []),
|
| 845 |
+
"shopVariantCountFinal": len(prod2.get("variants") or []),
|
| 846 |
+
"productId": created_id,
|
| 847 |
+
"priceUpdateResponse": upd,
|
| 848 |
+
}
|
| 849 |
+
)
|
| 850 |
+
|
| 851 |
+
offset += CHUNK_TARGET_VARIANTS
|
| 852 |
+
chunk_index += 1
|
| 853 |
|
| 854 |
result["providerRuns"] = provider_runs
|
| 855 |
_log(logs, "PHASE_B_DONE")
|