StarrySkyWorld commited on
Commit
cf601ce
·
1 Parent(s): 418e423

feat: 添加构建Chrome选项函数

Browse files

添加了一个名为 `build_chrome_options` 的函数,用于构建Chrome选项。该函数根据环境变量和配置设置Chrome选项,包括禁用沙箱、禁用GPU等。这使得Chrome浏览器的配置更加灵活和可维护。

修改了 `register_one_account` 函数,使用 `build_chrome_options` 函数来构建Chrome选项,而不是直接创建 `uc.ChromeOptions()` 对象。

修改了 `main` 函数,在创建Chrome浏览器实例时使用 `build_chrome_options` 函数来构建Chrome选项。此外,在 `main` 函数中多次实例化Chrome浏览器的地方也进行了相应的修改,以确保使用统一的配置函数。

CMD 指令中添加了 `--log-level debug` 和 `--access-log` 参数,以提高日志记录的详细程度,并启用访问日志记录。这有助于调试和跟踪应用程序的行为。

Files changed (2) hide show
  1. Dockerfile +2 -1
  2. main.py +22 -4
Dockerfile CHANGED
@@ -1,6 +1,7 @@
1
  FROM python:3.12-alpine
2
 
3
  WORKDIR /app
 
4
 
5
  COPY . .
6
  COPY example.config.json config.json
@@ -10,4 +11,4 @@ RUN apk add --no-cache chromium chromium-chromedriver xvfb-run
10
 
11
  RUN pip install --no-cache-dir -r requirements.txt
12
 
13
- CMD ["xvfb-run", "-a", "uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
  FROM python:3.12-alpine
2
 
3
  WORKDIR /app
4
+ ENV PYTHONUNBUFFERED=1
5
 
6
  COPY . .
7
  COPY example.config.json config.json
 
11
 
12
  RUN pip install --no-cache-dir -r requirements.txt
13
 
14
+ CMD ["xvfb-run", "-a", "uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "debug", "--access-log"]
main.py CHANGED
@@ -227,8 +227,23 @@ def register(driver):
227
  log(f"注册成功: {email}")
228
  return email, True, config
229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  def register_one_account():
231
- driver = uc.Chrome(options=uc.ChromeOptions(), use_subprocess=True, headless=HEADLESS)
 
232
  email = None
233
  try:
234
  email, ok, cfg = register(driver)
@@ -247,7 +262,8 @@ def main():
247
  apply_config(cfg)
248
  print(f"\n{'='*50}\nGemini Business 批量注册 - 共 {TOTAL_ACCOUNTS} 个\n{'='*50}\n")
249
 
250
- driver = uc.Chrome(options=uc.ChromeOptions(), use_subprocess=True, headless=HEADLESS)
 
251
  success, fail, accounts = 0, 0, []
252
 
253
  for i in range(TOTAL_ACCOUNTS):
@@ -256,7 +272,8 @@ def main():
256
  try:
257
  driver.current_url # 检查driver是否有效
258
  except:
259
- driver = uc.Chrome(options=uc.ChromeOptions(), use_subprocess=True, headless=HEADLESS)
 
260
 
261
  email = None
262
  try:
@@ -270,7 +287,8 @@ def main():
270
  save_error_screenshot(driver, email)
271
  try: driver.quit()
272
  except: pass
273
- driver = uc.Chrome(options=uc.ChromeOptions(), use_subprocess=True, headless=HEADLESS)
 
274
 
275
  print(f"\n进度: {i+1}/{TOTAL_ACCOUNTS} | 成功: {success} | 失败: {fail}")
276
 
 
227
  log(f"注册成功: {email}")
228
  return email, True, config
229
 
230
+ def build_chrome_options():
231
+ options = uc.ChromeOptions()
232
+ options.add_argument("--no-sandbox")
233
+ options.add_argument("--disable-dev-shm-usage")
234
+ options.add_argument("--disable-gpu")
235
+ options.add_argument("--disable-software-rasterizer")
236
+ options.add_argument("--window-size=1280,720")
237
+ if HEADLESS:
238
+ options.add_argument("--headless=new")
239
+ chrome_bin = os.getenv("CHROME_BIN")
240
+ if chrome_bin:
241
+ options.binary_location = chrome_bin
242
+ return options
243
+
244
  def register_one_account():
245
+ options = build_chrome_options()
246
+ driver = uc.Chrome(options=options, use_subprocess=True)
247
  email = None
248
  try:
249
  email, ok, cfg = register(driver)
 
262
  apply_config(cfg)
263
  print(f"\n{'='*50}\nGemini Business 批量注册 - 共 {TOTAL_ACCOUNTS} 个\n{'='*50}\n")
264
 
265
+ options = build_chrome_options()
266
+ driver = uc.Chrome(options=options, use_subprocess=True)
267
  success, fail, accounts = 0, 0, []
268
 
269
  for i in range(TOTAL_ACCOUNTS):
 
272
  try:
273
  driver.current_url # 检查driver是否有效
274
  except:
275
+ options = build_chrome_options()
276
+ driver = uc.Chrome(options=options, use_subprocess=True)
277
 
278
  email = None
279
  try:
 
287
  save_error_screenshot(driver, email)
288
  try: driver.quit()
289
  except: pass
290
+ options = build_chrome_options()
291
+ driver = uc.Chrome(options=options, use_subprocess=True)
292
 
293
  print(f"\n进度: {i+1}/{TOTAL_ACCOUNTS} | 成功: {success} | 失败: {fail}")
294