Files
missbleue/final_api_woocommerce/wcctl.py

167 lines
7.8 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

print(f"📦 Script lancé : __name__ = {__name__}")
import argparse
from woocommerce import API as WoocommerceApi
#from api_woocommerce import AuthentificationWpApi, MediaManager, CategoryManager, ProductManager, AttributeManager, VariationsManager, TabManager, WooCommerceManager
from new_api_woocommerce import AuthentificationWpApi, MediaManager, CategoryManager, ProductManager, AttributeManager, VariationsManager, TabManager, WooCommerceManager
import pprint
import base64
def import_medias_ods(args, media_manager):
if args.media:
if args.media_regex:
try:
media_manager.upload_media(args.media_regex)
except ValueError:
print("error name product_regex")
elif args.media_range:
try:
parts = args.media_range.split(':')
start = int(parts[0]) -1 if parts[0] else 0
end = int(parts[1]) if len(parts) > 1 and parts[1] else None
print(f"start = {start}, end = {end or 'fin'}")
media_manager.upload_media_from_to(start, end)
except ValueError:
print("❌ Mauvais format pour --media-range. Utilisez par exemple --media-range=1:40")
else:
start, end = 0, None
print(" --media activé, mais aucune plage spécifiée.")
def import_products_ods(args, woocommerce_manager):
if args.product:
if args.product_regex:
try:
woocommerce_manager.process_file(args.product_regex)
except ValueError:
print("error name product_regex")
elif args.product_range:
try:
parts = args.product_range.split(':')
start = int(parts[0]) -1 if parts[0] else 0
end = int(parts[1]) if len(parts) > 1 and parts[1] else None
print(f"start = {start}, end = {end or 'fin'}")
woocommerce_manager.process_file_from_to(start, end)
except ValueError:
print("❌ Mauvais format pour --product-range. Utilisez par exemple --product-range=1:40")
else:
start, end = 0, None
print(" --product activé, mais aucune plage spécifiée.")
def main():
#ath = AuthentificationWpApi()
parser = argparse.ArgumentParser(prog='wcctl', description='WooCommerce CLI controller')
# 🌐 Options globales
parser.add_argument('--wc-url', required=True, help='WooCommerce site URL')
parser.add_argument('--wc-key', required=True, help='WooCommerce API consumer key')
parser.add_argument('--wc-secret', required=True, help='WooCommerce API consumer secret')
#parser.add_argument('--media', action='store_true', help='Process media items')
#parser.add_argument('--media-range', type=str, help='Range of media rows to process (e.g., 10:30)')
# 🧱 Sous-commandes
subparsers = parser.add_subparsers(dest='command', required=True)
# 📥 Commande : import-ods
import_parser = subparsers.add_parser('import-ods', help='Import ODS file data')
import_parser.add_argument('--ods-path', required=True, help='Path to the ODS file')
# media
import_parser.add_argument('--media', action='store_true', help='Process media items')
import_parser.add_argument('--media-range', type=str, help='Range of media rows to process (e.g., 10:30)')
import_parser.add_argument('--media-regex', type=str, help='Regex to filter and import media by name')
import_parser.add_argument('--logo', action='store_true', help='Process logo')
# category
import_parser.add_argument('--category', action='store_true', help='import all categories')
import_parser.add_argument('--category-regex', type=str, help='Regex to filter and import categories by name')
# attribute
import_parser.add_argument('--attribute', action='store_true', help='import all attributes and terms')
import_parser.add_argument('--attribute-regex', type=str, help='Regex to filter and import attribute by name')
# tab
#import_parser.add_argument('--tab', action='store_true', help='import all tabs')
#import_parser.add_argument('--tab-regex', type=str, help='Regex to filter and import tab by name')
# product
import_parser.add_argument('--product', action='store_true', help='import all products')
import_parser.add_argument('--product-regex', type=str, help='Regex to filter and import product by name')
import_parser.add_argument('--product-range', type=str, help='Range of product rows to process (e.g., 10:30)')
# delete all informations
import_parser.add_argument('--delete-all', action='store_true', help='Delete media, categories, products, attributes, tabs')
# Analyse des arguments
args = parser.parse_args()
wcapi = WoocommerceApi(
url=args.wc_url,
consumer_key=args.wc_key,
consumer_secret=args.wc_secret,
wp_api=True,
version="wc/v3",
verify_ssl=False, # Désactive la vérification SSL pour le développement
timeout=30
)
ath = AuthentificationWpApi()
media_manager = MediaManager(ath, filename_ods=args.ods_path)
category_manager = CategoryManager(wcapi, ath, filename_ods=args.ods_path)
product_manager = ProductManager(wcapi, ath, filename_ods=args.ods_path)
attribute_manager = AttributeManager(wcapi, filename_ods=args.ods_path)
tab_manager = TabManager(wcapi, filename_ods=args.ods_path)
variation_manager = VariationsManager(wcapi, filename_ods=args.ods_path)
woocommerce_manager = WooCommerceManager(wcapi=wcapi,
media_manager=media_manager,
category_manager=category_manager,
product_manager=product_manager,
tab_manager=tab_manager,
attribute_manager=attribute_manager,
variation_manager=variation_manager,
filename_ods=args.ods_path)
# Dispatch en fonction de la commande
#if args.command == 'import-ods':
# import_medias_ods(args)
#print(f"🔍 args.media = {args.media}")
#print(f"🔍 args.media_range = {args.media_range}")
print(f"args = {args}")
if args.media:
import_medias_ods(args, media_manager)
if args.delete_all:
#woocommerce_manager.delete_all_informations()
media_manager.delete_all_images()
if args.category:
medias = media_manager.get_all_as_slug_dict()
category_manager.medias = medias
regex = args.category_regex if args.category_regex else None
category_manager.update_data_categories(regex)
if args.attribute:
regex = args.attribute_regex if args.attribute_regex else None
attribute_manager.create(regex)
attribute_manager.configure_term()
if args.product:
import_products_ods(args, woocommerce_manager)
if args.logo:
media_manager.assign_image_logo()
if __name__ == "__main__":
main()
# wcctl --wc-url=https://lescreationsdemissbleue.local --wc-key=<consumer_key> --wc-secret=<consumer_secret> import-ods --ods-path=fichier.ods
# ods_file = donnees_site_internet_missbleue_corrige.ods
#python wcctl.py --wc-url="https://lescreationsdemissbleue.local" --wc-key="ck_604e9b7b5d290cce72346efade6b31cb9a1ff28e" --wc-secret="cs_563974c7e59532c1ae1d0f8bbf61f0500d6bc768" import-ods --ods-path="donnees_site_internet_missbleue_corrige.ods"
#python wcctl.py --wc-url="https://les-creations-de-missbleue.local" --wc-key="ck_604e9b7b5d290cce72346efade6b31cb9a1ff28e" --wc-secret="cs_563974c7e59532c1ae1d0f8bbf61f0500d6bc768" import-ods --ods-path="donnees_site_internet_missbleue_corrige.ods"