Update main.py
Browse files
main.py
CHANGED
|
@@ -57,14 +57,20 @@ logger.info(f"OPENAI_API_KEY value: {OPENAI_API_KEY}")
|
|
| 57 |
|
| 58 |
def get_cookie():
|
| 59 |
try:
|
|
|
|
|
|
|
| 60 |
# 创建配置对象
|
| 61 |
options = ChromiumOptions()
|
| 62 |
|
| 63 |
-
# 设置浏览器路径
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# 设置用户数据目录
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# 设置无头模式和其他参数
|
| 70 |
options.set_argument('--headless=new')
|
|
@@ -78,28 +84,28 @@ def get_cookie():
|
|
| 78 |
options.set_argument('--no-zygote')
|
| 79 |
options.set_argument('--single-process')
|
| 80 |
options.set_argument('--remote-debugging-port=9222')
|
| 81 |
-
|
| 82 |
-
# 设置窗口大小
|
| 83 |
options.set_argument('--window-size=1920,1080')
|
| 84 |
-
|
| 85 |
-
# 设置用户代理
|
| 86 |
options.set_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
|
| 87 |
|
| 88 |
-
|
| 89 |
-
page = ChromiumPage(addr_or_opts=options)
|
| 90 |
|
| 91 |
-
|
| 92 |
page.get("https://chat.akash.network/")
|
|
|
|
|
|
|
| 93 |
time.sleep(10)
|
| 94 |
|
| 95 |
-
|
| 96 |
cookies = page.cookies()
|
| 97 |
if not cookies:
|
|
|
|
| 98 |
page.quit()
|
| 99 |
return None
|
| 100 |
|
| 101 |
cookie_dict = {cookie['name']: cookie['value'] for cookie in cookies}
|
| 102 |
if 'cf_clearance' not in cookie_dict:
|
|
|
|
| 103 |
page.quit()
|
| 104 |
return None
|
| 105 |
|
|
@@ -113,11 +119,15 @@ def get_cookie():
|
|
| 113 |
else:
|
| 114 |
global_data["cookie_expires"] = time.time() + 3600
|
| 115 |
|
|
|
|
| 116 |
page.quit()
|
| 117 |
return cookie_str
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
-
logger.error(f"Error fetching cookie: {e}")
|
|
|
|
|
|
|
|
|
|
| 121 |
return None
|
| 122 |
|
| 123 |
# 添加刷新 cookie 的函数
|
|
|
|
| 57 |
|
| 58 |
def get_cookie():
|
| 59 |
try:
|
| 60 |
+
logger.info("Starting cookie retrieval process...")
|
| 61 |
+
|
| 62 |
# 创建配置对象
|
| 63 |
options = ChromiumOptions()
|
| 64 |
|
| 65 |
+
# 设置浏览器路径
|
| 66 |
+
chrome_path = os.getenv('CHROME_PATH', '/usr/bin/google-chrome-stable')
|
| 67 |
+
logger.info(f"Using Chrome path: {chrome_path}")
|
| 68 |
+
options.set_browser_path(chrome_path)
|
| 69 |
|
| 70 |
# 设置用户数据目录
|
| 71 |
+
user_data_dir = '/tmp/chrome-data'
|
| 72 |
+
logger.info(f"Using user data directory: {user_data_dir}")
|
| 73 |
+
options.set_argument(f'--user-data-dir={user_data_dir}')
|
| 74 |
|
| 75 |
# 设置无头模式和其他参数
|
| 76 |
options.set_argument('--headless=new')
|
|
|
|
| 84 |
options.set_argument('--no-zygote')
|
| 85 |
options.set_argument('--single-process')
|
| 86 |
options.set_argument('--remote-debugging-port=9222')
|
|
|
|
|
|
|
| 87 |
options.set_argument('--window-size=1920,1080')
|
|
|
|
|
|
|
| 88 |
options.set_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
|
| 89 |
|
| 90 |
+
logger.info("Creating ChromiumPage instance...")
|
| 91 |
+
page = ChromiumPage(addr_or_opts=options, show_log=True)
|
| 92 |
|
| 93 |
+
logger.info("Navigating to target website...")
|
| 94 |
page.get("https://chat.akash.network/")
|
| 95 |
+
|
| 96 |
+
logger.info("Waiting for page load...")
|
| 97 |
time.sleep(10)
|
| 98 |
|
| 99 |
+
logger.info("Getting cookies...")
|
| 100 |
cookies = page.cookies()
|
| 101 |
if not cookies:
|
| 102 |
+
logger.error("No cookies found")
|
| 103 |
page.quit()
|
| 104 |
return None
|
| 105 |
|
| 106 |
cookie_dict = {cookie['name']: cookie['value'] for cookie in cookies}
|
| 107 |
if 'cf_clearance' not in cookie_dict:
|
| 108 |
+
logger.error("cf_clearance cookie not found")
|
| 109 |
page.quit()
|
| 110 |
return None
|
| 111 |
|
|
|
|
| 119 |
else:
|
| 120 |
global_data["cookie_expires"] = time.time() + 3600
|
| 121 |
|
| 122 |
+
logger.info("Successfully retrieved cookies")
|
| 123 |
page.quit()
|
| 124 |
return cookie_str
|
| 125 |
|
| 126 |
except Exception as e:
|
| 127 |
+
logger.error(f"Error fetching cookie: {str(e)}")
|
| 128 |
+
logger.error(f"Error type: {type(e)}")
|
| 129 |
+
import traceback
|
| 130 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 131 |
return None
|
| 132 |
|
| 133 |
# 添加刷新 cookie 的函数
|