hourly orders

This commit is contained in:
zman 2025-04-06 18:20:24 -04:00
parent 8de5bec523
commit 9d11adaf6c
3 changed files with 53 additions and 9 deletions

View File

@ -317,6 +317,7 @@ async def update_cookies(
detail=f"Failed to update cookies: {str(e)}" detail=f"Failed to update cookies: {str(e)}"
) )
### DEPRECATED ###
class TCGPlayerOrderRequest(BaseModel): class TCGPlayerOrderRequest(BaseModel):
order_ids: List[str] order_ids: List[str]
@ -335,4 +336,6 @@ async def process_orders(
) )
except Exception as e: except Exception as e:
logger.error(f"Process orders failed: {str(e)}") logger.error(f"Process orders failed: {str(e)}")
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
### END DEPRECATED ###

View File

@ -26,6 +26,7 @@ class TaskService:
def register_scheduled_tasks(self): def register_scheduled_tasks(self):
self.scheduler.add_job(self.hourly_pricing, 'cron', minute='36') self.scheduler.add_job(self.hourly_pricing, 'cron', minute='36')
self.scheduler.add_job(self.hourly_orders, 'cron', hour='*', minute='24')
# every 5 hours on the 24th minute # every 5 hours on the 24th minute
#self.scheduler.add_job(self.inventory_pricing, 'cron', hour='*', minute='44') #self.scheduler.add_job(self.inventory_pricing, 'cron', hour='*', minute='44')
self.logger.info("Scheduled tasks registered.") self.logger.info("Scheduled tasks registered.")
@ -35,6 +36,11 @@ class TaskService:
self.pricing_service.cron_load_prices() self.pricing_service.cron_load_prices()
self.logger.info("Finished hourly pricing task") self.logger.info("Finished hourly pricing task")
def hourly_orders(self):
self.logger.info("Running hourly orders task")
self.tcgplayer_api_service.process_orders_task()
self.logger.info("Finished hourly orders task")
def inventory_pricing(self): def inventory_pricing(self):
self.tcgplayer_api_service.cron_tcgplayer_api_pricing() self.tcgplayer_api_service.cron_tcgplayer_api_pricing()

View File

@ -16,6 +16,7 @@ class TCGPlayerAPIConfig:
"""Configuration for TCGPlayer API""" """Configuration for TCGPlayer API"""
ORDER_BASE_URL: str = "https://order-management-api.tcgplayer.com/orders" ORDER_BASE_URL: str = "https://order-management-api.tcgplayer.com/orders"
API_VERSION: str = "?api-version=2.0" API_VERSION: str = "?api-version=2.0"
SELLER_KEY: str = "e576ed4c"
class TCGPlayerAPIService: class TCGPlayerAPIService:
def __init__(self, db: Session): def __init__(self, db: Session):
@ -40,6 +41,25 @@ class TCGPlayerAPIService:
return response.json() return response.json()
return None return None
def get_orders(self, size: int = 25) -> dict:
url = f"{self.config.ORDER_BASE_URL}/search{self.config.API_VERSION}"
payload = {
"searchRange": "LastThreeMonths",
"filters": {
"sellerKey": self.config.SELLER_KEY
},
"sortBy": [
{"sortingType": "orderStatus", "direction": "ascending"},
{"sortingType": "orderDate", "direction": "ascending"}
],
"from": 0,
"size": size
}
response = self.requests_util.send_request(url, method='POST', cookies=self.cookies, data=payload)
if response:
return response.json()
return None
def get_product_ids_from_sku(self, sku_ids: list[str]) -> dict: def get_product_ids_from_sku(self, sku_ids: list[str]) -> dict:
"""Get product IDs from TCGPlayer SKU IDs""" """Get product IDs from TCGPlayer SKU IDs"""
# convert SKU IDs to integers # convert SKU IDs to integers
@ -101,14 +121,29 @@ class TCGPlayerAPIService:
self.db.add_all(order_products) self.db.add_all(order_products)
return db_order return db_order
def process_orders(self, orders: list[str]): def process_orders_task(self):
processed_orders = [] # get last 25 orders from tcgplayer
for order_id in orders: orders = self.get_orders(size=100)
order = self.get_order(order_id) if orders:
if order: # get list of order ids
self.save_order(order) order_ids = [order['orderNumber'] for order in orders['orders']]
processed_orders.append(order_id) # get a list of order ids that are not in the database
return processed_orders existing_orders = self.db.query(Orders).filter(Orders.order_id.in_(order_ids)).all()
existing_order_ids = [order.order_id for order in existing_orders]
# get a list of order ids that are not in the database
new_order_ids = [order_id for order_id in order_ids if order_id not in existing_order_ids]
# process new orders
processed_orders = []
if new_order_ids:
logger.info(f"Processing {len(new_order_ids)} new orders")
new_orders = [order for order in orders['orders'] if order['orderNumber'] in new_order_ids]
for order in new_orders:
self.save_order(order)
processed_orders.append(order['orderNumber'])
logger.info(f"Processed {len(processed_orders)} new orders")
return processed_orders
else:
logger.info("No new orders to process")
def get_scryfall_data(self, scryfall_id: str): def get_scryfall_data(self, scryfall_id: str):
url = f"https://api.scryfall.com/cards/{scryfall_id}?format=json" url = f"https://api.scryfall.com/cards/{scryfall_id}?format=json"