import os
import re
import subprocess
import traceback
from playwright.sync_api import Playwright, sync_playwright, expect# 执行CMD命令
cmd_command = "code --list-extensions" # 获取已安装扩展列表
process = subprocess.Popen(cmd_command, stdout=subprocess.PIPE, shell=True)# 获取命令结果
output, error = process.communicate()pluginList = []
# 输出结果
if error:print(f"错误: {error}")raise Exception(error)
else:res = output.decode('utf-8')pluginList = res.split('\n')print(f"命令输出: \n{res}")# pluginList = [
# '766b.go-outliner',
# 'aaron-bond.better-comments',
# 'achil.vscode-javascript-repl',
# 'aksharpatel47.vscode-flutter-helper',
# 'albymor.increment-selection',
# 'alefragnani.project-manager',
# 'alexisvt.flutter-snippets',
# 'Angular.ng-template',
# 'AykutSarac.jsoncrack-vscode',
# 'batisteo.vscode-django',
# 'bierner.color-info',
# 'BriteSnow.vscode-toggle-quotes',
# 'ChakrounAnas.turbo-console-log',
# 'changkaiyan.tf2snippets',
# 'checkmarx.ast-results',
# 'christian-kohler.npm-intellisense',
# 'christian-kohler.path-intellisense',
# 'chrmarti.regex',
# 'cipchk.zh-hans-tt-hant-vscode',
# 'circlecodesolution.ccs-flutter-color',
# 'codezombiech.gitignore',
# 'cssho.vscode-svgviewer',
# 'csstools.postcss',
# 'Dart-Code.dart-code',
# 'Dart-Code.flutter',
# 'dbaeumer.vscode-eslint',
# 'devzstudio.emoji-snippets',
# 'docsmsft.docs-markdown',
# 'docsmsft.docs-yaml',
# 'donjayamanne.git-extension-pack',
# 'donjayamanne.githistory',
# 'donjayamanne.python-environment-manager',
# 'donjayamanne.python-extension-pack',
# 'DotJoshJohnson.xml',
# 'eamodio.gitlens',
# 'ecmel.vscode-html-css',
# 'EditorConfig.EditorConfig',
# 'esbenp.prettier-vscode',
# 'felipecaputo.git-project-manager',
# 'FelixAngelov.bloc',
# 'firefox-devtools.vscode-firefox-debug',
# 'flesler.url-encode',
# 'folke.vscode-monorepo-workspace',
# 'formulahendry.auto-close-tag',
# 'formulahendry.auto-rename-tag',
# 'formulahendry.code-runner',
# 'fwcd.kotlin',
# 'gamunu.vscode-yarn',
# 'gaoliang.visual-zookeeper',
# 'GitHub.remotehub',
# 'golang.go',
# 'HarryHopkinson.vs-code-runner',
# 'hediet.debug-visualizer',
# 'helixquar.randomeverything',
# 'hollowtree.vue-snippets',
# 'huangjianke.fish-redux-template',
# 'humao.rest-client',
# 'ivangabriele.vscode-git-add-and-commit',
# 'IWANABETHATGUY.vue-this-store',
# 'JFrog.jfrog-vscode-extension',
# 'johnpapa.angular-essentials',
# 'johnpapa.Angular2',
# 'johnpapa.vscode-peacock',
# 'johnpapa.winteriscoming',
# 'KevinRose.vsc-python-indent',
# 'Kimseungtae.aicodehelper',
# 'lihui.vs-color-picker',
# 'magicstack.MagicPython',
# 'marlon407.code-groovy',
# 'mathiasfrohlich.Kotlin',
# 'MellowMarshmallow.groovy',
# 'mhutchie.git-graph',
# 'mindaro-dev.file-downloader',
# 'mindaro.mindaro',
# 'ms-azuretools.vscode-docker',
# 'MS-CEINTL.vscode-language-pack-zh-hans',
# 'ms-edgedevtools.vscode-edge-devtools',
# 'ms-kubernetes-tools.vscode-kubernetes-tools',
# 'ms-mssql.data-workspace-vscode',
# 'ms-mssql.mssql',
# 'ms-mssql.sql-bindings-vscode',
# 'ms-mssql.sql-database-projects-vscode',
# 'ms-ossdata.vscode-postgresql',
# 'ms-playwright.playwright',
# 'ms-python.autopep8',
# 'ms-python.black-formatter',
# 'ms-python.flake8',
# 'ms-python.isort',
# 'ms-python.pylint',
# 'ms-python.python',
# 'ms-python.vscode-pylance',
# 'ms-toolsai.jupyter',
# 'ms-toolsai.jupyter-keymap',
# 'ms-toolsai.jupyter-renderers',
# 'ms-toolsai.vscode-jupyter-cell-tags',
# 'ms-toolsai.vscode-jupyter-slideshow',
# 'ms-vscode-remote.remote-containers',
# 'ms-vscode-remote.remote-ssh',
# 'ms-vscode-remote.remote-ssh-edit',
# 'ms-vscode-remote.remote-wsl',
# 'ms-vscode-remote.vscode-remote-extensionpack',
# 'ms-vscode.azure-account',
# 'ms-vscode.azure-repos',
# 'ms-vscode.cmake-tools',
# 'ms-vscode.cpptools-extension-pack',
# 'ms-vscode.cpptools-themes',
# 'ms-vscode.hexeditor',
# 'ms-vscode.live-server',
# 'ms-vscode.powershell',
# 'ms-vscode.remote-explorer',
# 'ms-vscode.remote-repositories',
# 'ms-vscode.remote-server',
# 'ms-vscode.wordcount',
# 'msyrus.go-doc',
# 'Nash.awesome-flutter-snippets',
# 'Natizyskunk.sftp',
# 'NicolasVuillamy.vscode-groovy-lint',
# 'njpwerner.autodocstring',
# 'overtrue.miniapp-helper',
# 'PKief.material-icon-theme',
# 'Postman.postman-for-vscode',
# 'redhat.java',
# 'redhat.vscode-commons',
# 'redhat.vscode-yaml',
# 'ritwickdey.LiveServer',
# 'rogalmic.bash-debug',
# 'sdras.vue-vscode-snippets',
# 'tauri-apps.tauri-vscode',
# 'techer.open-in-browser',
# 'thekalinga.bootstrap4-vscode',
# 'twxs.cmake',
# 'vadimcn.vscode-lldb',
# 'vahidk.tensorflow-snippets',
# 'vformAdmin.vform-maker',
# 'VisualStudioExptTeam.intellicode-api-usage-examples',
# 'VisualStudioExptTeam.vscodeintellicode',
# 'VisualStudioExptTeam.vscodeintellicode-completions',
# 'vmware.vscode-boot-dev-pack',
# 'vmware.vscode-spring-boot',
# 'vscjava.vscode-gradle',
# 'vscjava.vscode-java-debug',
# 'vscjava.vscode-java-dependency',
# 'vscjava.vscode-java-pack',
# 'vscjava.vscode-java-test',
# 'vscjava.vscode-maven',
# 'vscjava.vscode-spring-boot-dashboard',
# 'vscjava.vscode-spring-initializr',
# 'Vue.volar',
# 'Vue.vscode-typescript-vue-plugin',
# 'waderyan.gitblame',
# 'WallabyJs.console-ninja',
# 'wellchang.fishreduxhelper',
# 'wellchang.fonttodartts',
# 'wholroyd.jinja',
# 'wowbox.code-debuger',
# 'Wscats.eno',
# 'xabikos.JavaScriptSnippets',
# 'yzane.markdown-pdf',
# 'yzhang.markdown-all-in-one',
# 'Zignd.html-css-class-completion',
# 'ziyasal.vscode-open-in-github']# 判断是否已下载
def isExsit(prefix):# 指定目录directory = os.getcwd()# 正则表达式匹配文件名pattern = prefix + '-.*\.vsix$' # 这将匹配所有.vsix文件# 遍历目录for filename in os.listdir(directory):# 如果文件名匹配正则表达式if re.match(pattern, filename):# 拼接完整的文件路径file_path = os.path.join(directory, filename)# 如果文件存在if os.path.exists(file_path):# print(f'文件 {file_path} 存在')return Trueelse:# print(f'文件 {file_path} 不存在')return Falsedef run(playwright: Playwright) -> None:browser = playwright.chromium.launch(headless=False, timeout=1200 * 1000.0)# 设置页面查找元素的超时时间为 5 秒browser.locator_timeout = 5000context = browser.new_context()context.set_default_timeout(5000)page = context.new_page()page.set_default_timeout(5000)failList = []for plugin in pluginList:try:# 判断文件是否存在if not isExsit(plugin):# print(plugin + ".vsix文件存在")# else:page.goto("https://marketplace.visualstudio.com/search?term="+plugin+"&target=VSCode&category=All%20categories&sortBy=Relevance", wait_until='commit')page.wait_for_timeout(300)first = page.locator('xpath=/html/body/div[2]/div[3]/div[3]/div/div/div[2]/div[2]/div[2]/div/div/div/div/a/div')first.click()# page.goto("https://marketplace.visualstudio.com/items?itemName=" + plugin, wait_until='commit')page.wait_for_timeout(300)ele = page.get_by_role("button", name="Version History")ele.click()page.wait_for_timeout(300)ele.click()page.wait_for_timeout(1000)version = page.locator("#version-history-tab-content div table tbody tr").nth(0).locator('td').nth(0).inner_text()page.wait_for_timeout(300)with page.expect_download() as download_info:# page.locator("xpath=/html/body/div[4]/div/div/div[2]/div/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div/div[2]/table/tbody/tr[1]/td[3]/a").click()page.locator("#version-history-tab-content div table tbody tr").nth(0).locator('a').nth(0).click()download = download_info.value# Wait for the download process to completeprint(download.path())# Save downloaded file somewheredownload.save_as(plugin + "-" + version + ".vsix")# breakexcept Exception as e:failList.append(plugin)print(plugin + ".vsix下载失败")print(e)# traceback.print_exc()# ---------------------context.close()browser.close()print('下载失败扩展列表:')print(failList)with sync_playwright() as playwright:run(playwright)