From d8ae45c0259370974717c73745206335086ad193 Mon Sep 17 00:00:00 2001 From: zman Date: Sun, 20 Apr 2025 00:32:25 -0400 Subject: [PATCH] kinda doesnt work yet --- app.log | 173 ++++++++++++++++++++------------ app/main.py | 4 +- app/routes/manabox_routes.py | 29 ++++++ app/routes/routes.py | 4 + app/services/manabox_service.py | 91 ++++++++++++++++- app/services/service_manager.py | 3 +- 6 files changed, 233 insertions(+), 71 deletions(-) create mode 100644 app/routes/manabox_routes.py diff --git a/app.log b/app.log index 8429ef0..cac99e3 100644 --- a/app.log +++ b/app.log @@ -1,66 +1,107 @@ -2025-04-19 21:40:53,869 - INFO - app.main - Application starting up... -2025-04-19 21:40:53,914 - INFO - app.main - Database initialized successfully -2025-04-19 21:40:53,914 - INFO - app.services.service_manager - Service OrderManagementService registered -2025-04-19 21:40:53,914 - INFO - app.services.service_manager - Service TCGPlayerInventoryService registered -2025-04-19 21:40:53,915 - INFO - app.services.service_manager - Service LabelPrinterService registered -2025-04-19 21:40:53,915 - INFO - app.services.service_manager - Service RegularPrinterService registered -2025-04-19 21:40:53,918 - INFO - app.services.service_manager - Service AddressLabelService registered -2025-04-19 21:40:53,920 - INFO - app.services.service_manager - Service PullSheetService registered -2025-04-19 21:40:53,920 - INFO - app.services.service_manager - Service SetLabelService registered -2025-04-19 21:40:53,920 - INFO - app.services.service_manager - Service DataInitializationService registered -2025-04-19 21:40:53,920 - DEBUG - tzlocal - /etc/localtime found -2025-04-19 21:40:53,921 - DEBUG - tzlocal - 1 found: - {'/etc/localtime is a symlink to': 'US/Michigan'} -2025-04-19 21:40:53,921 - INFO - app.services.service_manager - Service SchedulerService registered -2025-04-19 21:40:53,921 - INFO - app.services.service_manager - Service FileService registered -2025-04-19 21:40:53,921 - INFO - app.services.service_manager - Service TCGCSVService registered -2025-04-19 21:40:53,921 - INFO - app.services.service_manager - Service MTGJSONService registered -2025-04-19 21:40:53,921 - INFO - app.services.service_manager - All services initialized successfully -2025-04-19 21:40:53,921 - INFO - app.services.data_initialization - Starting data initialization process -2025-04-19 21:40:53,921 - INFO - app.services.data_initialization - Initializing MTGJSON data... -2025-04-19 21:40:53,921 - INFO - app.services.data_initialization - Starting MTGJSON initialization -2025-04-19 21:40:53,947 - DEBUG - app.services.external_api.mtgjson.mtgjson_service - Loaded SKUs from cache: app/data/cache/mtgjson/skus/TcgplayerSkus.json -2025-04-19 22:38:54,248 - INFO - app.services.data_initialization - Data initialization completed -2025-04-19 22:38:54,249 - INFO - app.main - Data initialization results: {'mtgjson': {'identifiers_processed': 0, 'skus_processed': 4764017}} -2025-04-19 22:38:54,249 - INFO - apscheduler.scheduler - Adding job tentatively -- it will be properly scheduled when the scheduler starts -2025-04-19 22:38:54,249 - INFO - app.services.scheduler.base_scheduler - Scheduled task update_open_orders_hourly to run every 3600 seconds -2025-04-19 22:38:54,249 - INFO - apscheduler.scheduler - Adding job tentatively -- it will be properly scheduled when the scheduler starts -2025-04-19 22:38:54,249 - INFO - app.services.scheduler.base_scheduler - Scheduled task update_all_orders_daily to run every 86400 seconds -2025-04-19 22:38:54,250 - INFO - apscheduler.scheduler - Added job "SchedulerService.start_scheduled_tasks.." to job store "default" -2025-04-19 22:38:54,250 - INFO - apscheduler.scheduler - Added job "SchedulerService.start_scheduled_tasks.." to job store "default" -2025-04-19 22:38:54,250 - INFO - apscheduler.scheduler - Scheduler started -2025-04-19 22:38:54,250 - INFO - app.services.scheduler.base_scheduler - Scheduler started -2025-04-19 22:38:54,250 - INFO - app.services.scheduler.scheduler_service - All scheduled tasks started -2025-04-19 22:38:54,250 - INFO - app.main - Scheduler started successfully -2025-04-19 22:38:54,251 - DEBUG - apscheduler.scheduler - Looking for jobs to run -2025-04-19 22:38:54,251 - DEBUG - apscheduler.scheduler - Next wakeup is due at 2025-04-19 23:38:54.249141-04:00 (in 3599.997778 seconds) -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service MTGJSONService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service mtgjson cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service TCGCSVService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service tcgcsv cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service FileService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service file cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.scheduler.base_scheduler - Scheduler stopped -2025-04-19 22:53:56,053 - INFO - app.services.scheduler.scheduler_service - Scheduler services closed -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service SchedulerService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.scheduler.scheduler_service - Scheduler services closed -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service scheduler cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service DataInitializationService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service data_initialization cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service SetLabelService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service set_label cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service PullSheetService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service pull_sheet cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service AddressLabelService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service address_label cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service RegularPrinterService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service regular_printer cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service LabelPrinterService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service label_printer cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service TCGPlayerInventoryService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service tcgplayer_inventory cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service OrderManagementService cleaned up -2025-04-19 22:53:56,053 - INFO - app.services.service_manager - Service order_management cleaned up -2025-04-19 22:53:56,054 - INFO - app.services.service_manager - All services cleaned up successfully -2025-04-19 22:53:56,054 - INFO - app.main - All services cleaned up successfully -2025-04-19 22:53:56,054 - INFO - apscheduler.scheduler - Scheduler has been shut down +2025-04-20 00:26:23,206 - INFO - app.main - Application starting up... +2025-04-20 00:26:23,441 - INFO - app.main - Database initialized successfully +2025-04-20 00:26:23,441 - INFO - app.services.service_manager - Service OrderManagementService registered +2025-04-20 00:26:23,442 - INFO - app.services.service_manager - Service TCGPlayerInventoryService registered +2025-04-20 00:26:23,442 - INFO - app.services.service_manager - Service LabelPrinterService registered +2025-04-20 00:26:23,443 - INFO - app.services.service_manager - Service RegularPrinterService registered +2025-04-20 00:26:23,447 - INFO - app.services.service_manager - Service AddressLabelService registered +2025-04-20 00:26:23,450 - INFO - app.services.service_manager - Service PullSheetService registered +2025-04-20 00:26:23,450 - INFO - app.services.service_manager - Service SetLabelService registered +2025-04-20 00:26:23,450 - INFO - app.services.service_manager - Service DataInitializationService registered +2025-04-20 00:26:23,450 - DEBUG - tzlocal - /etc/localtime found +2025-04-20 00:26:23,452 - DEBUG - tzlocal - 1 found: + {'/etc/localtime is a symlink to': 'America/Detroit'} +2025-04-20 00:26:23,452 - INFO - app.services.service_manager - Service SchedulerService registered +2025-04-20 00:26:23,452 - INFO - app.services.service_manager - Service FileService registered +2025-04-20 00:26:23,452 - INFO - app.services.service_manager - Service TCGCSVService registered +2025-04-20 00:26:23,452 - INFO - app.services.service_manager - Service MTGJSONService registered +2025-04-20 00:26:23,455 - INFO - app.services.service_manager - Service ManaboxService registered +2025-04-20 00:26:23,455 - INFO - app.services.service_manager - All services initialized successfully +2025-04-20 00:26:23,455 - INFO - app.services.data_initialization - Starting data initialization process +2025-04-20 00:26:23,456 - INFO - app.services.data_initialization - Data initialization completed +2025-04-20 00:26:23,456 - INFO - app.main - Data initialization results: {} +2025-04-20 00:26:23,456 - INFO - apscheduler.scheduler - Adding job tentatively -- it will be properly scheduled when the scheduler starts +2025-04-20 00:26:23,456 - INFO - app.services.scheduler.base_scheduler - Scheduled task update_open_orders_hourly to run every 3600 seconds +2025-04-20 00:26:23,456 - INFO - apscheduler.scheduler - Adding job tentatively -- it will be properly scheduled when the scheduler starts +2025-04-20 00:26:23,456 - INFO - app.services.scheduler.base_scheduler - Scheduled task update_all_orders_daily to run every 86400 seconds +2025-04-20 00:26:23,457 - INFO - apscheduler.scheduler - Added job "SchedulerService.start_scheduled_tasks.." to job store "default" +2025-04-20 00:26:23,457 - INFO - apscheduler.scheduler - Added job "SchedulerService.start_scheduled_tasks.." to job store "default" +2025-04-20 00:26:23,457 - INFO - apscheduler.scheduler - Scheduler started +2025-04-20 00:26:23,457 - INFO - app.services.scheduler.base_scheduler - Scheduler started +2025-04-20 00:26:23,457 - INFO - app.services.scheduler.scheduler_service - All scheduled tasks started +2025-04-20 00:26:23,457 - INFO - app.main - Scheduler started successfully +2025-04-20 00:26:23,457 - DEBUG - apscheduler.scheduler - Looking for jobs to run +2025-04-20 00:26:23,457 - DEBUG - apscheduler.scheduler - Next wakeup is due at 2025-04-20 01:26:23.456315-04:00 (in 3599.998465 seconds) +2025-04-20 00:26:39,542 - DEBUG - python_multipart.multipart - Calling on_part_begin with no data +2025-04-20 00:26:39,543 - DEBUG - python_multipart.multipart - Calling on_header_field with data[63:82] +2025-04-20 00:26:39,543 - DEBUG - python_multipart.multipart - Calling on_header_value with data[84:126] +2025-04-20 00:26:39,543 - DEBUG - python_multipart.multipart - Calling on_header_end with no data +2025-04-20 00:26:39,543 - DEBUG - python_multipart.multipart - Calling on_header_field with data[128:140] +2025-04-20 00:26:39,543 - DEBUG - python_multipart.multipart - Calling on_header_value with data[142:150] +2025-04-20 00:26:39,544 - DEBUG - python_multipart.multipart - Calling on_header_end with no data +2025-04-20 00:26:39,544 - DEBUG - python_multipart.multipart - Calling on_headers_finished with no data +2025-04-20 00:26:39,544 - DEBUG - python_multipart.multipart - Calling on_part_data with data[154:13032] +2025-04-20 00:26:39,551 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:27512] +2025-04-20 00:26:39,561 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:46336] +2025-04-20 00:26:39,562 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:7240] +2025-04-20 00:26:39,577 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:50680] +2025-04-20 00:26:39,578 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:46336] +2025-04-20 00:26:39,597 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:47784] +2025-04-20 00:26:39,599 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:37704] +2025-04-20 00:26:39,603 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:55024] +2025-04-20 00:26:39,613 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:45109] +2025-04-20 00:26:39,614 - DEBUG - python_multipart.multipart - Calling on_part_end with no data +2025-04-20 00:26:39,614 - DEBUG - python_multipart.multipart - Calling on_part_begin with no data +2025-04-20 00:26:39,614 - DEBUG - python_multipart.multipart - Calling on_header_field with data[45174:45193] +2025-04-20 00:26:39,614 - DEBUG - python_multipart.multipart - Calling on_header_value with data[45195:45219] +2025-04-20 00:26:39,614 - DEBUG - python_multipart.multipart - Calling on_header_end with no data +2025-04-20 00:26:39,614 - DEBUG - python_multipart.multipart - Calling on_headers_finished with no data +2025-04-20 00:26:39,614 - DEBUG - python_multipart.multipart - Calling on_part_data with data[45223:45230] +2025-04-20 00:26:39,614 - DEBUG - python_multipart.multipart - Calling on_part_end with no data +2025-04-20 00:26:39,615 - DEBUG - python_multipart.multipart - Calling on_part_begin with no data +2025-04-20 00:26:39,615 - DEBUG - python_multipart.multipart - Calling on_header_field with data[45295:45314] +2025-04-20 00:26:39,615 - DEBUG - python_multipart.multipart - Calling on_header_value with data[45316:45338] +2025-04-20 00:26:39,615 - DEBUG - python_multipart.multipart - Calling on_header_end with no data +2025-04-20 00:26:39,615 - DEBUG - python_multipart.multipart - Calling on_headers_finished with no data +2025-04-20 00:26:39,615 - DEBUG - python_multipart.multipart - Calling on_part_data with data[45342:45358] +2025-04-20 00:26:39,615 - DEBUG - python_multipart.multipart - Calling on_part_end with no data +2025-04-20 00:26:39,616 - DEBUG - python_multipart.multipart - Calling on_end with no data +2025-04-20 00:26:48,984 - ERROR - app.services.manabox_service - Error: 2 TCGplayer SKUs found for Sword Coast Serpent // Capsizing Wave near_mint +2025-04-20 00:31:30,890 - DEBUG - python_multipart.multipart - Calling on_part_begin with no data +2025-04-20 00:31:30,891 - DEBUG - python_multipart.multipart - Calling on_header_field with data[63:82] +2025-04-20 00:31:30,892 - DEBUG - python_multipart.multipart - Calling on_header_value with data[84:126] +2025-04-20 00:31:30,892 - DEBUG - python_multipart.multipart - Calling on_header_end with no data +2025-04-20 00:31:30,892 - DEBUG - python_multipart.multipart - Calling on_header_field with data[128:140] +2025-04-20 00:31:30,892 - DEBUG - python_multipart.multipart - Calling on_header_value with data[142:150] +2025-04-20 00:31:30,892 - DEBUG - python_multipart.multipart - Calling on_header_end with no data +2025-04-20 00:31:30,892 - DEBUG - python_multipart.multipart - Calling on_headers_finished with no data +2025-04-20 00:31:30,893 - DEBUG - python_multipart.multipart - Calling on_part_data with data[154:13032] +2025-04-20 00:31:30,893 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:1448] +2025-04-20 00:31:30,904 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:28960] +2025-04-20 00:31:30,919 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:47784] +2025-04-20 00:31:30,920 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:8688] +2025-04-20 00:31:30,938 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:47784] +2025-04-20 00:31:30,939 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:50680] +2025-04-20 00:31:30,940 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:1448] +2025-04-20 00:31:30,962 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:4344] +2025-04-20 00:31:30,962 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:50680] +2025-04-20 00:31:30,963 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:60280] +2025-04-20 00:31:30,967 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:26064] +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_part_data with data[0:35565] +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_part_end with no data +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_part_begin with no data +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_header_field with data[35630:35649] +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_header_value with data[35651:35675] +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_header_end with no data +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_headers_finished with no data +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_part_data with data[35679:35686] +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_part_end with no data +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_part_begin with no data +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_header_field with data[35751:35770] +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_header_value with data[35772:35794] +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_header_end with no data +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_headers_finished with no data +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_part_data with data[35798:35814] +2025-04-20 00:31:30,979 - DEBUG - python_multipart.multipart - Calling on_part_end with no data +2025-04-20 00:31:30,980 - DEBUG - python_multipart.multipart - Calling on_end with no data +2025-04-20 00:31:40,209 - ERROR - app.services.manabox_service - Error: 2 TCGplayer SKUs found for Sword Coast Serpent // Capsizing Wave near_mint diff --git a/app/main.py b/app/main.py index 62680e6..6f14a17 100644 --- a/app/main.py +++ b/app/main.py @@ -58,7 +58,7 @@ async def lifespan(app: FastAPI): db = SessionLocal() try: data_init_service = service_manager.get_service('data_initialization') - data_init = await data_init_service.initialize_data(db, game_ids=[1, 3], use_cache=True, init_categories=False, init_products=False, init_groups=False, init_archived_prices=False, init_mtgjson=True, archived_prices_start_date="2024-03-05", archived_prices_end_date="2025-04-17") + data_init = await data_init_service.initialize_data(db, game_ids=[1, 3], use_cache=True, init_categories=False, init_products=False, init_groups=False, init_archived_prices=False, init_mtgjson=False, archived_prices_start_date="2024-03-05", archived_prices_end_date="2025-04-17") logger.info(f"Data initialization results: {data_init}") # Start the scheduler @@ -110,7 +110,7 @@ async def read_app_js(): # Configure CORS with specific origins in production app.add_middleware( CORSMiddleware, - allow_origins=["http://localhost:3000"], # Update with your frontend URL + allow_origins=["http://localhost:3000", "http://192.168.1.124:3000"], # Update with your frontend URL allow_credentials=True, allow_methods=["*"], allow_headers=["*"], diff --git a/app/routes/manabox_routes.py b/app/routes/manabox_routes.py new file mode 100644 index 0000000..b527d2d --- /dev/null +++ b/app/routes/manabox_routes.py @@ -0,0 +1,29 @@ +from fastapi import APIRouter, Depends, UploadFile, File, HTTPException +from sqlalchemy.orm import Session +from app.db.database import get_db +from app.services.service_manager import ServiceManager + +router = APIRouter(prefix="/manabox") + +service_manager = ServiceManager() + +@router.post("/process-csv") +async def process_manabox_csv( + file: UploadFile = File(...), + db: Session = Depends(get_db) +): + try: + + content = await file.read() + + manabox_service = service_manager.get_service("manabox") + + success = await manabox_service.process_manabox_csv(db, content) + + if not success: + raise HTTPException(status_code=400, detail="Failed to process CSV file") + + return {"message": "CSV processed successfully"} + + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) \ No newline at end of file diff --git a/app/routes/routes.py b/app/routes/routes.py index 4a2960f..f5de9a0 100644 --- a/app/routes/routes.py +++ b/app/routes/routes.py @@ -5,6 +5,7 @@ from app.models.file import File as FileModel from app.schemas.file import FileCreate, FileUpdate, FileDelete, FileList, FileInDB from app.routes.set_label_routes import router as set_label_router from app.routes.order_routes import router as order_router +from app.routes.manabox_routes import router as manabox_router router = APIRouter(prefix="/api") @@ -14,6 +15,9 @@ router.include_router(set_label_router) # Include order routes router.include_router(order_router) +# Include manabox routes +router.include_router(manabox_router) + # ============================================================================ # Health Check & Root Endpoints # ============================================================================ diff --git a/app/services/manabox_service.py b/app/services/manabox_service.py index 2cb7a36..612a59b 100644 --- a/app/services/manabox_service.py +++ b/app/services/manabox_service.py @@ -1,15 +1,102 @@ from app.services.base_service import BaseService from sqlalchemy.orm import Session +from app.db.database import transaction from app.schemas.file import FileInDB +from app.models.mtgjson_card import MTGJSONCard +from app.models.mtgjson_sku import MTGJSONSKU +from app.models.tcgplayer_inventory import TCGPlayerInventory +from app.models.tcgplayer_product import TCGPlayerProduct +from app.models.tcgplayer_category import TCGPlayerCategory +from app.models.tcgplayer_group import TCGPlayerGroup from typing import Dict, Any import csv +import logging +from datetime import datetime +logger = logging.getLogger(__name__) class ManaboxService(BaseService): def __init__(self): super().__init__(None) - async def process_manabox_csv(self, db: Session, csv_file: FileInDB) -> bool: - + async def process_manabox_csv(self, db: Session, bytes: bytes) -> bool: + # save file + file = await self.file_service.save_file( + db=db, + file_data=bytes, + filename=f"manabox_{datetime.now().strftime('%Y%m%d%H%M%S')}.csv", + subdir="manabox", + file_type="manabox", + content_type="text/csv" + ) + # Read the CSV file + with open(file.path, 'r') as file: + reader = csv.DictReader(file) + # validate headers + if reader.fieldnames != ['Name', 'Set code', 'Set name', 'Collector number', 'Foil', 'Rarity', 'Quantity', 'ManaBox ID', 'Scryfall ID', 'Purchase price', 'Misprint', 'Altered', 'Condition', 'Language', 'Purchase price currency']: + logger.error("Invalid headers") + return False + # skip header row + next(reader) + for row in reader: + # match scryfall id to mtgjson scryfall id, make sure only one distinct tcgplayer id + mtg_json = db.query(MTGJSONCard).filter(MTGJSONCard.scryfall_id == row['Scryfall ID']).all() + # count distinct tcgplayer ids + cd_tcgplayer_ids = db.query(MTGJSONCard.tcgplayer_product_id).filter(MTGJSONCard.scryfall_id == row['Scryfall ID']).distinct().count() + if cd_tcgplayer_ids != 1: + logger.error(f"Error: {cd_tcgplayer_ids} TCGplayer IDs found for {row['Scryfall ID']}") + return False + else: + mtg_json = mtg_json[0] + # get tcgplayer sku id from mtgjson skus + language = 'ENGLISH' if row['Language'] == 'en' else 'JAPANESE' if row['Language'] == 'ja' else None + if row['Foil'].lower() == 'etched': + printing = 'FOIL' + tcgplayer_sku = db.query(MTGJSONSKU.sku_id).filter(MTGJSONSKU.product_id == mtg_json.tcgplayer_etched_product_id).filter(MTGJSONSKU.condition == row['Condition'].replace('_', ' ').upper()).filter(MTGJSONSKU.printing == printing).filter(MTGJSONSKU.language == language).all().distinct() + else: + printing = 'FOIL' if row['Foil'].lower() == 'foil' else 'NON FOIL' + tcgplayer_sku = db.query(MTGJSONSKU.sku_id).filter(MTGJSONSKU.product_id == mtg_json.tcgplayer_product_id).filter(MTGJSONSKU.condition == row['Condition'].replace('_', ' ').upper()).filter(MTGJSONSKU.printing == printing).filter(MTGJSONSKU.language == language).all().distinct() + # count distinct tcgplayer skus + if len(tcgplayer_sku) == 0: + logger.error(f"Error: No TCGplayer SKU found for {mtg_json.name} {row['Condition']}") + logger.debug(row) + logger.debug(language) + logger.debug(row['Condition'].replace('_', ' ').upper()) + logger.debug(mtg_json.tcgplayer_product_id) + logger.debug(printing) + return False + elif len(tcgplayer_sku) > 1: + logger.error(f"Error: {len(tcgplayer_sku)} TCGplayer SKUs found for {mtg_json.name} {row['Condition']}") + return False + else: + tcgplayer_sku = tcgplayer_sku[0] + # look up tcgplayer product data for sku + tcgplayer_product = db.query(TCGPlayerProduct).filter(TCGPlayerProduct.product_id == tcgplayer_sku.product_id).first() + # temp just dump into tcgplayer inventory + condition = f'{tcgplayer_sku.condition.title()} Foil' if 'Foil' in tcgplayer_product.sub_type_name else f'{tcgplayer_sku.condition.title()}' + # join tcgplaeyer product on tcgplayer category on category_id and get name + product_line = db.query(TCGPlayerCategory).filter(TCGPlayerCategory.category_id == tcgplayer_product.category_id).first().name + # join tcgplaeyer product on tcgplayer group on group_id and get name + set_name = db.query(TCGPlayerGroup).filter(TCGPlayerGroup.group_id == tcgplayer_product.group_id).first().name + with transaction(db): + tcgplayer_inventory = TCGPlayerInventory( + tcgplayer_id=tcgplayer_sku.sku_id, + product_line=product_line, + set_name=set_name, + product_name=tcgplayer_product.name, + title=None, + number=tcgplayer_product.ext_number, + rarity=tcgplayer_product.ext_rarity, + condition=condition, + tcg_market_price=tcgplayer_product.market_price, + tcg_direct_low=tcgplayer_product.direct_low_price, + tcg_low_price_with_shipping=tcgplayer_product.low_price, + tcg_low_price=tcgplayer_product.low_price, + total_quantity=row['Quantity'], + add_to_quantity=row['Quantity'] + ) + db.add(tcgplayer_inventory) + + return True # Name,Set code,Set name,Collector number,Foil,Rarity,Quantity,ManaBox ID,Scryfall ID,Purchase price,Misprint,Altered,Condition,Language,Purchase price currency \ No newline at end of file diff --git a/app/services/service_manager.py b/app/services/service_manager.py index 5379d2c..0a37d39 100644 --- a/app/services/service_manager.py +++ b/app/services/service_manager.py @@ -28,7 +28,8 @@ class ServiceManager: 'scheduler': 'app.services.scheduler.scheduler_service.SchedulerService', 'file': 'app.services.file_service.FileService', 'tcgcsv': 'app.services.external_api.tcgcsv.tcgcsv_service.TCGCSVService', - 'mtgjson': 'app.services.external_api.mtgjson.mtgjson_service.MTGJSONService' + 'mtgjson': 'app.services.external_api.mtgjson.mtgjson_service.MTGJSONService', + 'manabox': 'app.services.manabox_service.ManaboxService' } self._service_configs = { 'label_printer': {'printer_api_url': "http://192.168.1.110:8000"},