From c10c3a0beb25586d6d3db7a151fafc23bc4f9e88 Mon Sep 17 00:00:00 2001 From: zman Date: Sat, 8 Feb 2025 08:56:19 -0500 Subject: [PATCH] cookies --- app/routes/routes.py | 9 ++++----- send_cookie.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 send_cookie.py diff --git a/app/routes/routes.py b/app/routes/routes.py index c2cfb38..7376804 100644 --- a/app/routes/routes.py +++ b/app/routes/routes.py @@ -290,21 +290,20 @@ async def update_cookies( cookie_data: CookieUpdate ): try: + # see if cookie file exists + if not os.path.exists('cookies') and os.path.exists('cookies/tcg_cookies.json'): + logger.info("Cannot find cookies") # 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' + cookie_path = '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: diff --git a/send_cookie.py b/send_cookie.py new file mode 100644 index 0000000..24d607f --- /dev/null +++ b/send_cookie.py @@ -0,0 +1,38 @@ +import browser_cookie3 +import requests +import json + +def send_tcg_cookies(api_url: str, browser_type='brave'): + """Get TCGPlayer cookies and send them to the API""" + try: + # Get cookies from browser + cookie_getter = getattr(browser_cookie3, browser_type) + cookie_jar = cookie_getter(domain_name='tcgplayer.com') + + # Filter essential cookies + cookies = {} + for cookie in cookie_jar: + if any(key in cookie.name.lower() for key in ['.aspnet', 'tcg', 'session']): + cookies[cookie.name] = cookie.value + + # Send to API + headers = { + 'Content-Type': 'application/json' + } + + response = requests.post( + f"{api_url}", + headers=headers, + json={'cookies': cookies} + ) + + response.raise_for_status() + print("Cookies updated successfully!") + + except Exception as e: + print(f"Error updating cookies: {e}") + +if __name__ == "__main__": + API_URL = "http://192.168.1.41:8000/api/cookies" # Update with your API URL + + send_tcg_cookies(API_URL) \ No newline at end of file