python - 如何从 GitHub API 获取所有后续版本并将它们包含在 Python 中的列表中?
问题描述
我正在制作一个程序并在其中包含一个更新检查器。更新检查器应该从我的 GitHub 存储库中获取最新版本的标签信息,然后如果标签名称比当前更新,它应该下载资产文件。我正在使用requests
库来访问 GitHub API 并urllib.request
下载资产文件。但我不想只下载最新版本,我还想列出所有比当前版本更新的先前版本,并让用户选择下载旧版本。但我被困在获取所有以前的版本并将它们包含在列表中。可能只有一个先前版本或 10 个版本。我想我可以使用 for 循环,但我不擅长 for 循环。
https://api.github.com/repos/Yilmaz4/Encrypt-n-Decrypt/releases
这是我的存储库的 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
,name
和browser_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.
解决方案
推荐阅读
- forms - Symfony 学说 DateTime 表单 TimeType
- linux - 使用 awk 创建具有特定列的平方根的新文件
- android - EmojiTextView 中的字体不起作用
- html - img:hover 上未显示文本
- android - Android YUV420/NV21图像水平翻转使用前置摄像头反光板
- android - RecylerView 中的 ViewPager,单击不起作用
- java - 正则表达式匹配不被空格包围
- javascript - 不太清楚为什么我从 moment.js 和 moment recur 得到错误的时间
- javascript - 如何在 Ionic 3 的库回调(intro.js)中修改页面范围“this”的变量?
- json - npm中的“上面的附加日志输出”是什么意思?