首页 > 解决方案 > 如何从 GitHub API 获取所有后续版本并将它们包含在 Python 中的列表中?

问题描述

我正在制作一个程序并在其中包含一个更新检查器。更新检查器应该从我的 GitHub 存储库中获取最新版本的标签信息,然后如果标签名称比当前更新,它应该下载资产文件。我正在使用requests库来访问 GitHub API 并urllib.request下载资产文件。但我不想只下载最新版本,我还想列出所有比当前版本更新的先前版本,并让用户选择下载旧版本。但我被困在获取所有以前的版本并将它们包含在列表中。可能只有一个先前版本或 10 个版本。我想我可以使用 for 循环,但我不擅长 for 循环。

https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases

这是我的存储库的 API 链接。

所有版本 API 列表

当前有 4 个版本,它们在图像中从 0 到 3 列出。它们就像文件夹一样,当我单击它们时,它们会扩展到有关其各自版本的所有信息。

标签名

当我展开其中一个时,每个文件夹都有一个名为的变量tag_name,这显示了此版本的版本。

from requests import get
Version = get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases/latest") #The 'latest' line on the end of the link redirects to latest release, for example v0.2.2 release of my repository.
Version.json()["tag_name"]

我用上面的代码得到了这个变量。

下载链接

此外,存储库中的所有版本都有资产下载链接,如上图所示。

from requests import get
import urllib.request
Version = get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases/latest")
with urllib.request.urlopen(str(Version.json()["browser_download_url"])) as f:
    html = f.read().decode('utf-8')

browser_download_url我使用上面的代码下载了带有这个 url 的资产文件。

我想做的只是了解有多少版本比当前版本更新,并获取它们的tag_name,namebrowser_download_url变量,并将它们列在 tkinter 窗口的下拉选择菜单中。我怎样才能做到这一点?

这是我检查更新的完整代码:

def CheckUpdates():
    Version = get("https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases/latest")
    if Version.json()["tag_name"] == version:messagebox.showinfo("No updates available","There are currently no updates available. Please check again later.\n\nYour version: {}\nLatest version: {}".format(version, Version.json()["tag_name"]))
    else:
        if version[1:] > (Version.json()["tag_name"])[1:]:
            messagebox.showinfo("Interesting.","It looks like you're using a newer version than official GitHub page. Your version may be a beta, or you're the author of this program :)\n\nYour version: {}\nLatest version: {}".format(version, Version.json()["tag_name"]))
        else:
            # This is the part that should create tkinter window, get all releases and list them.

标签: pythongithubgithub-api

解决方案


推荐阅读