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)}"
)
### DEPRECATED ###
class TCGPlayerOrderRequest(BaseModel):
order_ids: List[str]
@ -335,4 +336,6 @@ async def process_orders(
)
except Exception as 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):
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
#self.scheduler.add_job(self.inventory_pricing, 'cron', hour='*', minute='44')
self.logger.info("Scheduled tasks registered.")
@ -35,6 +36,11 @@ class TaskService:
self.pricing_service.cron_load_prices()
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):
self.tcgplayer_api_service.cron_tcgplayer_api_pricing()

View File

@ -16,6 +16,7 @@ class TCGPlayerAPIConfig:
"""Configuration for TCGPlayer API"""
ORDER_BASE_URL: str = "https://order-management-api.tcgplayer.com/orders"
API_VERSION: str = "?api-version=2.0"
SELLER_KEY: str = "e576ed4c"
class TCGPlayerAPIService:
def __init__(self, db: Session):
@ -40,6 +41,25 @@ class TCGPlayerAPIService:
return response.json()
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:
"""Get product IDs from TCGPlayer SKU IDs"""
# convert SKU IDs to integers
@ -101,14 +121,29 @@ class TCGPlayerAPIService:
self.db.add_all(order_products)
return db_order
def process_orders(self, orders: list[str]):
processed_orders = []
for order_id in orders:
order = self.get_order(order_id)
if order:
self.save_order(order)
processed_orders.append(order_id)
return processed_orders
def process_orders_task(self):
# get last 25 orders from tcgplayer
orders = self.get_orders(size=100)
if orders:
# get list of order ids
order_ids = [order['orderNumber'] for order in orders['orders']]
# get a list of order ids that are not in the database
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):
url = f"https://api.scryfall.com/cards/{scryfall_id}?format=json"