update_chrome.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import json
  2. import os
  3. import re
  4. import subprocess
  5. import sys
  6. import requests
  7. import platform
  8. import shutil
  9. import zipfile
  10. import urllib.request
  11. if sys.platform == "win32":
  12. import winreg
  13. import re
  14. def get_processor_info():
  15. if os.uname().sysname == 'Darwin':
  16. processor_info = subprocess.check_output(['sysctl', '-n', 'machdep.cpu.brand_string']).strip()
  17. processor_info = str(processor_info)
  18. if 'Intel' in processor_info:
  19. return 'Intel'
  20. elif 'Apple' in processor_info:
  21. return 'Apple'
  22. else:
  23. return 'Unknown'
  24. else:
  25. return 'This method is only implemented for macOS.'
  26. def download_and_extract_zip(url, destination_folder):
  27. # 下载ZIP文件
  28. urllib.request.urlretrieve(url, "temp.zip")
  29. # 解压ZIP文件
  30. with zipfile.ZipFile("temp.zip", "r") as zip_ref:
  31. zip_ref.extractall(destination_folder)
  32. # 删除临时ZIP文件
  33. os.remove("temp.zip")
  34. def copy_file(source_file, destination_file):
  35. # 使用copy2()函数复制文件
  36. shutil.copy2(source_file, destination_file)
  37. def copy_folder(source_folder, destination_folder):
  38. # 使用copytree()函数复制文件夹及其内容
  39. shutil.copytree(source_folder, destination_folder)
  40. def get_chrome_version():
  41. version = "131"
  42. if sys.platform == "win32":
  43. version_re = re.compile(r"^[1-9]\d*\.\d*.\d*")
  44. try:
  45. key = winreg.OpenKey(
  46. winreg.HKEY_CURRENT_USER, r"Software\Google\Chrome\BLBeacon"
  47. )
  48. _v, type = winreg.QueryValueEx(key, "version")
  49. return version_re.findall(_v)[0][:3]
  50. except WindowsError as e:
  51. print("check Chrome failed:{}".format(e))
  52. elif sys.platform == 'darwin':
  53. # 通过读取 /Applications/Google Chrome.app/Contents/Frameworks/Google Chrome Framework.framework/Versions/Current 这一符号连接指向的文件夹的名称,获得 Chrome 版本号
  54. try:
  55. full_version = os.path.basename(os.path.abspath(os.readlink("/Applications/Google Chrome.app/Contents/Frameworks/Google "
  56. "Chrome Framework.framework/Versions/Current")))
  57. return full_version.split(".")[0]
  58. # 如果找不到 Chrome 程序或者获取错误,回退到默认的 131 版本
  59. except (OSError, IndexError):
  60. return version
  61. else:
  62. return version
  63. chrome_version = get_chrome_version() # 要更新的chromedriver版本
  64. print("Detected your chrome version is: ", chrome_version)
  65. chrome_driver_url = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
  66. win64_chrome_path = "C:\\Program Files\\Google\\Chrome\\Application"
  67. win32_chrome_path = "C:\\Program Files\\Google\\Chrome\\Application"
  68. mac_chrome_path = "/Applications/Google Chrome.app"
  69. linux_chrome_path = "/opt/google/chrome"
  70. old_driver_version = {
  71. "100":"100.0.4896.60",
  72. "101":"101.0.4951.41",
  73. "102":"102.0.5005.61",
  74. "103":"103.0.5060.134",
  75. "104":"104.0.5112.79",
  76. "105":"105.0.5195.52",
  77. "106":"106.0.5249.61",
  78. "107":"107.0.5304.62",
  79. "108":"108.0.5359.71",
  80. "109":"109.0.5414.74",
  81. "110":"110.0.5481.77",
  82. "111":"111.0.5563.64",
  83. "112":"112.0.5615.49",
  84. "113":"113.0.5672.63",
  85. "114":"114.0.5735.90",
  86. }
  87. if __name__ == "__main__":
  88. os.system("npm install -g extract-stealth-evasions") # 安装stealth.min.js
  89. os.system("npx extract-stealth-evasions") # 提取stealth.min.js
  90. # chromedriver 在 chrome 114 版本之后,提供了一个新的 API 来获取已知的 Chrome 版本和下载链接
  91. # 之前的版本需要手动维护下载链接
  92. # 现在可以通过访问 https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json
  93. # 来获取最新的 Chrome 版本和下载链接
  94. driver_downloads = []
  95. if int(chrome_version.split(".")[0]) >= 114:
  96. response = requests.get(chrome_driver_url)
  97. if response.status_code == 200:
  98. versions = json.loads(response.content)["versions"]
  99. versions = versions[::-1] # 倒序排列数组
  100. for info in versions:
  101. version = info["version"].split(".")[0]
  102. if version.find(chrome_version) == 0:
  103. downloads = info["downloads"]
  104. if "chromedriver" in downloads:
  105. print(info["version"])
  106. driver_downloads = downloads["chromedriver"]
  107. break
  108. else:
  109. print("Error: ", response.status_code)
  110. exit(1)
  111. else:
  112. if chrome_version not in old_driver_version:
  113. print("没有可用的chromedriver")
  114. exit(1)
  115. full_version = old_driver_version[chrome_version]
  116. driver_downloads = [
  117. {
  118. "platform": "linux64",
  119. "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_linux64.zip",
  120. },
  121. {
  122. "platform": "mac-arm64",
  123. "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_mac_arm64.zip",
  124. },
  125. {
  126. "platform": "mac-x64",
  127. "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_mac64.zip",
  128. },
  129. {
  130. "platform": "win32",
  131. "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_win32.zip",
  132. },
  133. {
  134. "platform": "win64",
  135. "url": f"http://chromedriver.storage.googleapis.com/{full_version}/chromedriver_win32.zip",
  136. },
  137. ]
  138. if os.path.exists("./chromedrivers"):
  139. shutil.rmtree("./chromedrivers")
  140. os.mkdir("./chromedrivers")
  141. if sys.platform == "win32" and platform.architecture()[0] == "64bit":
  142. for download in driver_downloads:
  143. if download["platform"] == "win64":
  144. url = download["url"]
  145. print("ChromeDriver will be downloaded from: ", url)
  146. break
  147. download_and_extract_zip(url, "./chromedrivers")
  148. if os.path.exists("./chrome_win64"):
  149. shutil.rmtree("./chrome_win64")
  150. copy_folder(win64_chrome_path, "./chrome_win64")
  151. for folder in os.listdir("./chrome_win64"):
  152. if folder[0].isdigit() and os.path.isdir("./chrome_win64/"+folder):
  153. shutil.rmtree("./chrome_win64/"+folder+"/Installer") # 删除Installer文件夹
  154. copy_file("./execute_win64.bat", "./chrome_win64/execute_win64.bat")
  155. copy_file("./stealth.min.js", "./chrome_win64/stealth.min.js")
  156. try:
  157. copy_file(
  158. "./chromedrivers/chromedriver-win64/chromedriver.exe",
  159. "./chrome_win64/chromedriver_win64.exe",
  160. )
  161. except:
  162. copy_file(
  163. "./chromedrivers/chromedriver.exe",
  164. "./chrome_win64/chromedriver_win64.exe",
  165. )
  166. finally:
  167. shutil.rmtree("./chromedrivers")
  168. elif sys.platform == "win32" and platform.architecture()[0] == "32bit":
  169. for download in driver_downloads:
  170. if download["platform"] == "win32":
  171. url = download["url"]
  172. print("ChromeDriver will be downloaded from: ", url)
  173. break
  174. download_and_extract_zip(url, "./chromedrivers")
  175. if os.path.exists("./chrome_win32"):
  176. shutil.rmtree("./chrome_win32")
  177. copy_folder(win64_chrome_path, "./chrome_win32")
  178. for folder in os.listdir("./chrome_win32"):
  179. if folder[0].isdigit() and os.path.isdir("./chrome_win32/"+folder):
  180. shutil.rmtree("./chrome_win32/"+folder+"/Installer") # 删除Installer文件夹
  181. copy_file("./execute_win32.bat", "./chrome_win32/execute_win32.bat")
  182. copy_file("./stealth.min.js", "./chrome_win32/stealth.min.js")
  183. try:
  184. copy_file(
  185. "./chromedrivers/chromedriver-win32/chromedriver.exe",
  186. "./chrome_win32/chromedriver_win32.exe",
  187. )
  188. except:
  189. copy_file(
  190. "./chromedrivers/chromedriver.exe",
  191. "./chrome_win32/chromedriver_win64.exe",
  192. )
  193. finally:
  194. shutil.rmtree("./chromedrivers")
  195. elif sys.platform == "linux" and platform.architecture()[0] == "64bit":
  196. for download in driver_downloads:
  197. if download["platform"] == "linux64":
  198. url = download["url"]
  199. print("ChromeDriver will be downloaded from: ", url)
  200. break
  201. download_and_extract_zip(url, "./chromedrivers")
  202. if os.path.exists("./chrome_linux64"):
  203. shutil.rmtree("./chrome_linux64")
  204. copy_folder(linux_chrome_path, "./chrome_linux64")
  205. copy_file("./execute_linux64.sh", "./chrome_linux64/execute_linux64.sh")
  206. copy_file("./stealth.min.js", "./chrome_linux64/stealth.min.js")
  207. try:
  208. copy_file(
  209. "./chromedrivers/chromedriver-linux64/chromedriver",
  210. "./chrome_linux64/chromedriver_linux64",
  211. )
  212. except:
  213. copy_file(
  214. "./chromedrivers/chromedriver",
  215. "./chrome_linux64/chromedriver_linux64",
  216. )
  217. finally:
  218. # Change Linux file permissions
  219. os.chmod("./chrome_linux64/chromedriver_linux64", 0o755)
  220. os.chmod("./chrome_linux64/execute_linux64.sh", 0o755)
  221. shutil.rmtree("./chromedrivers")
  222. elif sys.platform == "darwin" and platform.architecture()[0] == "64bit":
  223. processor = get_processor_info()
  224. if processor == "Intel":
  225. driver_arch = "mac-x64"
  226. elif processor == "Apple":
  227. driver_arch = "mac-arm64"
  228. for download in driver_downloads:
  229. if download["platform"] == driver_arch:
  230. url = download["url"]
  231. print("ChromeDriver will be downloaded from: ", url)
  232. break
  233. download_and_extract_zip(url, "./chromedrivers")
  234. if os.path.exists("./chrome_mac64.app"):
  235. shutil.rmtree("./chrome_mac64.app")
  236. # copy_folder(mac_chrome_path, "./chrome_mac64.app")
  237. subprocess.call(["cp", "-R", mac_chrome_path, "./chrome_mac64.app"])
  238. try:
  239. copy_file(
  240. "./chromedrivers/chromedriver-%s/chromedriver" % driver_arch,
  241. "./chromedriver_mac64",
  242. )
  243. except:
  244. copy_file(
  245. "./chromedrivers/chromedriver",
  246. "./chromedriver_mac64",
  247. )
  248. finally:
  249. shutil.rmtree("./chromedrivers")
  250. os.chmod("./chromedriver_mac64", 0o755)
  251. os.chmod("./chrome_mac64.app", 0o755)
  252. os.chmod("./chrome_mac64.app/Contents/MacOS/Google Chrome", 0o755)
  253. print("Done and don't forget to generate executestage EXEcutable program!")