使用python获取最新版本的vscode-server

使用python获取最新版本的vscode-server

使用vscode远程连接服务器时,vscode会在服务器端下载安装vscode-server,但有时我们的远程服务器不能直接连接下载地址,或者下载速度十分感人,怎么办呢?

https://update.code.visualstudio.com/commit:$(commit_id)/server-linux-x64/stable"

这是vscode-server的下载地址,$(commit_id)对应的是本地vscode版本的提交号,可以在帮助-关于里查到。vscode版本说明
每次vs更新后都要这么手动下载一次是挺麻烦的,因此写了一个脚本来完成这个下载过程。

思路分析

  1. 首先要获取到vscode的commit_id,vscode仓库在github,因此考虑通过github提供的API获取最新版本的commit_id。
  2. 首先从github API获取最新的release页,再从对应的release页获取commit_id即可。

代码实现

  1. 获取release页:
url = "https://api.github.com/repos/microsoft/vscode/releases/latest"
release_url = requests.get(url).json()["html_url"]

  1. 获取commit_id:
    因为只需要获取一个url,因此就没必要使用bs4、lxml这些库来解析网页了,直接正则查找即可。
commit_id = (re.search("/microsoft/vscode/commit/(.+?)\"",
                requests.get(release_url).text).group(1))

结尾

最后,无论是Linux还是Windows,都可以将脚本添加到计划任务里,定时运行,定时运行可以先检测版本号有无改变。

with open("vscode_commit_id.txt","r+") as cid:
    if cid.read()==commit_id:
        print("exit")
        exit()
    else:
        cid.seek(0)
        cid.truncate()
        cid.write(commit_id)

猜你喜欢

转载自blog.csdn.net/u013943146/article/details/118999174
今日推荐