瀏覽代碼

Merge pull request #116 from zhyim0712/dev-1

fix cant download webdriver before 115
Naibo Wang 2 年之前
父節點
當前提交
0bb8dedca8
共有 1 個文件被更改,包括 94 次插入4 次删除
  1. 94 4
      ElectronJS/update_chrome.py

+ 94 - 4
ElectronJS/update_chrome.py

@@ -7,6 +7,9 @@ import platform
 import shutil
 import zipfile
 import urllib.request
+import winreg
+import re
+
 
 def download_and_extract_zip(url, destination_folder):
     # 下载ZIP文件
@@ -19,21 +22,59 @@ def download_and_extract_zip(url, destination_folder):
     # 删除临时ZIP文件
     os.remove("temp.zip")
 
+
 def copy_file(source_file, destination_file):
     # 使用copy2()函数复制文件
     shutil.copy2(source_file, destination_file)
 
+
 def copy_folder(source_folder, destination_folder):
     # 使用copytree()函数复制文件夹及其内容
     shutil.copytree(source_folder, destination_folder)
 
-update_version = "115" # 要更新的chromedriver版本
+
+def get_chrome_version():
+    if sys.platform == "win32":
+        version_re = re.compile(r"^[1-9]\d*\.\d*.\d*")
+        try:
+            key = winreg.OpenKey(
+                winreg.HKEY_CURRENT_USER, r"Software\Google\Chrome\BLBeacon"
+            )
+            _v, type = winreg.QueryValueEx(key, "version")
+            return version_re.findall(_v)[0][:3]
+        except WindowsError as e:
+            print("check Chrome failed:{}".format(e))
+    elif sys.platform == "linux":
+        pass
+    elif sys.platform == "darwin":
+        pass
+    return "115"
+
+
+update_version = get_chrome_version()  # 要更新的chromedriver版本
 
 chrome_driver_url = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
 win64_chrome_path = "C:\\Program Files\\Google\\Chrome\\Application"
 win32_chrome_path = "C:\\Program Files\\Google\\Chrome\\Application"
 mac_chrome_path = "/Applications/Google Chrome.app/Contents/MacOS"
 linux_chrome_path = "/opt/google/chrome"
+old_driver_version = {
+    "100":"100.0.4896.60",
+    "101":"101.0.4951.41",
+    "102":"102.0.5005.61",
+    "103":"103.0.5060.134",
+    "104":"104.0.5112.79",
+    "105":"105.0.5195.52",
+    "106":"106.0.5249.61",
+    "107":"107.0.5304.62",
+    "108":"108.0.5359.71",
+    "109":"109.0.5414.74",
+    "110":"110.0.5481.77",
+    "111":"111.0.5563.64",
+    "112":"112.0.5615.49",
+    "113":"113.0.5672.63",
+    "114":"114.0.5735.90",
+}
 
 if __name__ == "__main__":
     driver_downloads = []
@@ -52,6 +93,33 @@ if __name__ == "__main__":
     else:
         print("Error: " + response.status_code)
         exit(1)
+    if not driver_downloads and int(update_version) < 115:
+        if update_version not in old_driver_version:
+            print("没有可用的chromedriver")
+            exit(1)
+        full_version = old_driver_version[update_version]
+        driver_downloads = [
+            {
+                "platform": "linux64",
+                "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_linux64.zip",
+            },
+            {
+                "platform": "mac-arm64",
+                "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_mac_arm64.zip",
+            },
+            {
+                "platform": "mac-x64",
+                "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_mac64.zip",
+            },
+            {
+                "platform": "win32",
+                "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_win32.zip",
+            },
+            {
+                "platform": "win64",
+                "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_win32.zip",
+            },
+        ]
     if os.path.exists("./chromedrivers"):
         shutil.rmtree("./chromedrivers")
     os.mkdir("./chromedrivers")
@@ -70,7 +138,18 @@ if __name__ == "__main__":
                 shutil.rmtree("./chrome_win64/"+folder+"/Installer") # 删除Installer文件夹
         copy_file("./execute.bat", "./chrome_win64/execute.bat")
         copy_file("./stealth.min.js", "./chrome_win64/stealth.min.js")
-        copy_file("./chromedrivers/chromedriver-win64/chromedriver.exe", "./chrome_win64/chromedriver_win64.exe")
+        try:
+            copy_file(
+                "./chromedrivers/chromedriver-win64/chromedriver.exe",
+                "./chrome_win64/chromedriver_win64.exe",
+            )
+        except:
+            copy_file(
+                "./chromedrivers/chromedriver.exe",
+                "./chrome_win64/chromedriver_win64.exe",
+            )
+        finally:
+            shutil.rmtree("./chromedrivers")
     elif sys.platform == "win32" and platform.architecture()[0] == "32bit":
         for download in driver_downloads:
             if download["platform"] == "win32":
@@ -86,10 +165,21 @@ if __name__ == "__main__":
                 shutil.rmtree("./chrome_win32/"+folder+"/Installer") # 删除Installer文件夹
         copy_file("./execute.bat", "./chrome_win32/execute.bat")
         copy_file("./stealth.min.js", "./chrome_win32/stealth.min.js")
-        copy_file("./chromedrivers/chromedriver-win32/chromedriver.exe", "./chrome_win32/chromedriver_win32.exe")
+        try:
+            copy_file(
+                "./chromedrivers/chromedriver-win64/chromedriver.exe",
+                "./chrome_win64/chromedriver_win64.exe",
+            )
+        except:
+            copy_file(
+                "./chromedrivers/chromedriver.exe",
+                "./chrome_win64/chromedriver_win64.exe",
+            )
+        finally:
+            shutil.rmtree("./chromedrivers")
     elif sys.platform == "linux" and platform.architecture()[0] == "64bit":
         pass
     elif sys.platform == "darwin" and platform.architecture()[0] == "64bit":
         pass
 
-    print("Done and don't forget to generate executestage EXEcutable program!")
+    print("Done and don't forget to generate executestage EXEcutable program!")