asdf
This commit is contained in:
parent
8c3cd423fe
commit
949c795fd1
1
.gitignore
vendored
1
.gitignore
vendored
@ -173,3 +173,4 @@ cython_debug/
|
|||||||
temp/
|
temp/
|
||||||
.DS_Store
|
.DS_Store
|
||||||
*.db-journal
|
*.db-journal
|
||||||
|
cookies/
|
@ -4,6 +4,8 @@ from typing import Optional, List
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
from app.schemas.file import (
|
from app.schemas.file import (
|
||||||
FileSchema,
|
FileSchema,
|
||||||
@ -276,3 +278,36 @@ async def create_inventory_update_file(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Create inventory update file failed: {str(e)}")
|
logger.error(f"Create inventory update file failed: {str(e)}")
|
||||||
raise HTTPException(status_code=400, detail=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)}"
|
||||||
|
)
|
||||||
|
@ -20,6 +20,7 @@ from typing import List, Dict, Optional
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from app.schemas.file import CreateFileRequest
|
from app.schemas.file import CreateFileRequest
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -53,6 +54,14 @@ class TCGPlayerService:
|
|||||||
self.df_util = DataframeUtil()
|
self.df_util = DataframeUtil()
|
||||||
self.file_service = file_service
|
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):
|
def _insert_groups(self, groups):
|
||||||
for group in groups:
|
for group in groups:
|
||||||
db_group = TCGPlayerGroups(
|
db_group = TCGPlayerGroups(
|
||||||
@ -118,9 +127,11 @@ class TCGPlayerService:
|
|||||||
logger.info(f"Waiting 10 seconds before next request...")
|
logger.info(f"Waiting 10 seconds before next request...")
|
||||||
time.sleep(10 - time_diff)
|
time.sleep(10 - time_diff)
|
||||||
headers = self._set_headers(method)
|
headers = self._set_headers(method)
|
||||||
|
# only get cookies on mac os not in docker container
|
||||||
if not self.cookies:
|
if not self.cookies and os.name != 'nt':
|
||||||
self.cookies = self._get_browser_cookies()
|
self.cookies = self._get_browser_cookies()
|
||||||
|
else:
|
||||||
|
self.cookies = self.get_cookies_from_file()
|
||||||
if not self.cookies:
|
if not self.cookies:
|
||||||
raise ValueError("Failed to retrieve browser cookies")
|
raise ValueError("Failed to retrieve browser cookies")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user