githubapi:Get';车身';适用于Python中的所有新版本

githubapi:Get';车身';适用于Python中的所有新版本,python,python-3.x,github-api,Python,Python 3.x,Github Api,我有一个python脚本,可以从GitHub获取更新,并在更新时显示最新版本的补丁注释 我希望脚本在当前版本/发布之前显示所有版本的修补程序说明。 我的github版本的版本如下:v1.2.3 这是我当前使用的代码,仅显示最新版本的修补程序说明: version = 2.1.5 import urllib.request, json with urllib.request.urlopen("https://api.github.com/repos/:author/:repo/releases

我有一个python脚本,可以从GitHub获取更新,并在更新时显示最新版本的补丁注释

我希望脚本在当前版本/发布之前显示所有版本的修补程序说明。

我的github版本的版本如下:
v1.2.3

这是我当前使用的代码,仅显示最新版本的修补程序说明:

version = 2.1.5

import urllib.request, json

with urllib.request.urlopen("https://api.github.com/repos/:author/:repo/releases/latest") as url:
  data = json.loads(url.read().decode())
  latest = data['tag_name'][1:] # "v2.3.6" -> "2.3.6"
  patchNotes = data['body']

if latest > version:
    print('\nUpdate available!')
    print(f'Latest Version: v{latest}')
    print('\n'+str(patchNotes)+'\n') #display latest (v2.3.6) patch notes
    input(str('Update now? [Y/n] ')).upper()
    #code to download the latest version
这是一个我必须得到我需要的想法:

  • 获取所有发布版本号(例如1.2.3)
  • 将当前版本之前的所有版本号添加到数组中
  • 对于数组中的版本,从相应的github api json页面获取
    数据[body]
  • 按正确顺序(最旧(比当前版本早一个)到最新(最新版本)打印修补程序说明

  • 我不知道如何实现上述想法,如果有更有效的方法来实现我的目标,我愿意听取建议。

    您可以使用以下方法:

    import requests
    from packaging import version
    
    maxVersion = version.parse("3.9.2")
    repoWithOwner= "labstack/echo"
    
    r = requests.get("https://api.github.com/repos/{}/releases?per_page=100".format(repoWithOwner))
    releases = [ 
        (t["tag_name"],t["body"]) 
        for t in r.json() 
        if version.parse(t["tag_name"]) >= maxVersion
    ][::-1]
    
    for r in releases:
        print("{} : {}".format(r[0],r[1]))
    
    它获取100个最新版本并检查它是否>={您指定的版本},反转数组并打印标签和正文

    灵感来源,根据我的需要进行编辑

    我使用了Python3的预装库urllib.requestjson(这样它更便于移植),以及
    f-strings
    而不是
    。format

    import urllib.request, json
    
    ver='3.0.0' #Current scripts version
    author='labstack'
    repo='echo'
    
    release = json.loads(urllib.request.urlopen(f'https://api.github.com/repos/{author}/{repo}/releases?per_page=5').read().decode())
    releases = [
        (data['tag_name'],data['body'])
        for data in release
        if data['tag_name'] > ver][::-1]
    for release in releases:
        print(f'{release[0]}:\n{release[1]}\n')
    

    变量r和t有什么意义(它们包含/表示什么)以及它们用于什么?@OverflowingStack r是请求的结果,t是