This commit is contained in:
zman 2025-02-07 21:17:53 -05:00
parent 8c3cd423fe
commit 949c795fd1
3 changed files with 51 additions and 4 deletions

1
.gitignore vendored
View File

@ -173,3 +173,4 @@ cython_debug/
temp/
.DS_Store
*.db-journal
cookies/

View File

@ -4,6 +4,8 @@ from typing import Optional, List
from io import BytesIO
import logging
from datetime import datetime
import os
import json
from app.schemas.file import (
FileSchema,
@ -276,3 +278,36 @@ async def create_inventory_update_file(
except Exception as e:
logger.error(f"Create inventory update file failed: {str(e)}")
raise HTTPException(status_code=400, detail=str(e))
class CookieUpdate():
cookies: dict
# cookies
@router.post("/cookies", response_model=dict)
async def update_cookies(
cookie_data: CookieUpdate
):
try:
# Create cookies directory if it doesn't exist
os.makedirs('cookies', exist_ok=True)
# Save cookies with timestamp
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
cookie_path = f'cookies/tcg_cookies.json'
# Save new cookies
with open(cookie_path, 'w') as f:
json.dump(cookie_data.cookies, f, indent=2)
# Update the "latest" cookies file
with open('cookies/tcg_cookies_latest.json', 'w') as f:
json.dump(cookie_data.cookies, f, indent=2)
return {"message": "Cookies updated successfully"}
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Failed to update cookies: {str(e)}"
)

View File

@ -20,6 +20,7 @@ from typing import List, Dict, Optional
import pandas as pd
from sqlalchemy.exc import SQLAlchemyError
from app.schemas.file import CreateFileRequest
import os
logger = logging.getLogger(__name__)
@ -53,6 +54,14 @@ class TCGPlayerService:
self.df_util = DataframeUtil()
self.file_service = file_service
def get_cookies_from_file(self) -> Dict:
# check if cookies file exists
if not os.path.exists('cookies/tcg_cookies.json'):
raise ValueError("Cookies file not found")
with open('cookies/tcg_cookies.json', 'r') as f:
cookies = json.load(f)
return cookies
def _insert_groups(self, groups):
for group in groups:
db_group = TCGPlayerGroups(
@ -118,9 +127,11 @@ class TCGPlayerService:
logger.info(f"Waiting 10 seconds before next request...")
time.sleep(10 - time_diff)
headers = self._set_headers(method)
if not self.cookies:
# only get cookies on mac os not in docker container
if not self.cookies and os.name != 'nt':
self.cookies = self._get_browser_cookies()
else:
self.cookies = self.get_cookies_from_file()
if not self.cookies:
raise ValueError("Failed to retrieve browser cookies")