diff --git a/.gitignore b/.gitignore index f5fce5b..e81ab8f 100644 --- a/.gitignore +++ b/.gitignore @@ -172,4 +172,5 @@ cython_debug/ *.db temp/ .DS_Store -*.db-journal \ No newline at end of file +*.db-journal +cookies/ \ No newline at end of file diff --git a/app/routes/routes.py b/app/routes/routes.py index bbd48ce..d6d6f14 100644 --- a/app/routes/routes.py +++ b/app/routes/routes.py @@ -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, @@ -275,4 +277,37 @@ 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)) \ No newline at end of file + 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)}" + ) diff --git a/app/services/tcgplayer.py b/app/services/tcgplayer.py index 293803f..6de6d43 100644 --- a/app/services/tcgplayer.py +++ b/app/services/tcgplayer.py @@ -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")