GitHub API是一个强大的工具,可以让开发者和用户方便地获取各种有用的信息,例如仓库的releases信息。releases信息可以用于检测更新,让用户知道仓库的最新版本和变化。本文将介绍如何使用GitHub API获取仓库releases信息用于检测更新。

获取对应仓库信息

我的博客仓库:https://github.com/OuOumm/omo

首先,我们需要知道仓库的owner和name,例如,获取我的仓库releases信息,那么owner就是OuOumm,name就是omo。然后,我们可以使用以下URL来请求GitHub API:

这个URL会返回一个JSON格式的数据,包含了仓库的所有releases信息,每个release都有以下属性:

  • id: release的唯一标识符
  • tag_name: release的版本号
  • name: release的名称
  • body: release的描述
  • created_at: release的创建时间
  • published_at: release的发布时间
  • assets: release的附件,例如安装包或源码

编写请求代码

我们可以使用任何支持HTTP请求和JSON解析的编程语言或工具来获取和处理这些数据,例如Python、JavaScript、Postman等。下面是一个简单的Python示例,使用requests库来发送请求,使用json库来解析数据:

import requests
import json

# 设置owner和name
owner = "OuOumm"
name = "omo"

# 构造URL
url = f"https://api.github.com/repos/{owner}/{name}/releases/latest"

# 发送GET请求
response = requests.get(url)

# 检查状态码
if response.status_code == 200:
    # 解析JSON数据
    data = json.loads(response.text)
    # 遍历每个release
    for release in data:
        # 打印release的版本号和发布时间
        print(release["tag_name"], release["published_at"])
else:
    # 打印错误信息
    print(f"Error: {response.status_code}")

运行这段代码,我们可以得到类似以下的输出:

v1.0.0,一次博客文件的更新

不过由于我的仓库并没有创建releases版本信息,所以这里是获取不到的,下面使用一个通用库进行获取:

Typecho:https://api.github.com/repos/typecho/typecho/releases/latest

我们可以获得到以下信息:

{
    "url": "https://api.github.com/repos/typecho/typecho/releases/107460469",
    "assets_url": "https://api.github.com/repos/typecho/typecho/releases/107460469/assets",
    "upload_url": "https://uploads.github.com/repos/typecho/typecho/releases/107460469/assets{?name,label}",
    "html_url": "https://github.com/typecho/typecho/releases/tag/v1.2.1",
    "id": 107460469,
    "author": {
        "login": "joyqi",
        "id": 59437,
        "node_id": "MDQ6VXNlcjU5NDM3",
        "avatar_url": "https://avatars.githubusercontent.com/u/59437?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/joyqi",
        "html_url": "https://github.com/joyqi",
        "followers_url": "https://api.github.com/users/joyqi/followers",
        "following_url": "https://api.github.com/users/joyqi/following{/other_user}",
        "gists_url": "https://api.github.com/users/joyqi/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/joyqi/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/joyqi/subscriptions",
        "organizations_url": "https://api.github.com/users/joyqi/orgs",
        "repos_url": "https://api.github.com/users/joyqi/repos",
        "events_url": "https://api.github.com/users/joyqi/events{/privacy}",
        "received_events_url": "https://api.github.com/users/joyqi/received_events",
        "type": "User",
        "site_admin": false
    },
    "node_id": "RE_kwDOAK77k84GZ7d1",
    "tag_name": "v1.2.1",
    "target_commitish": "master",
    "name": "v1.2.1",
    "draft": false,
    "prerelease": false,
    "created_at": "2023-06-05T07:15:24Z",
    "published_at": "2023-06-06T06:36:47Z",
    "assets": [
        {
            "url": "https://api.github.com/repos/typecho/typecho/releases/assets/111471302",
            "id": 111471302,
            "node_id": "RA_kwDOAK77k84GpOrG",
            "name": "typecho.zip",
            "label": "",
            "uploader": {
                "login": "github-actions[bot]",
                "id": 41898282,
                "node_id": "MDM6Qm90NDE4OTgyODI=",
                "avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4",
                "gravatar_id": "",
                "url": "https://api.github.com/users/github-actions%5Bbot%5D",
                "html_url": "https://github.com/apps/github-actions",
                "followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
                "following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
                "gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
                "starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
                "subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
                "organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
                "repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
                "events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
                "received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
                "type": "Bot",
                "site_admin": false
            },
            "content_type": "application/zip",
            "state": "uploaded",
            "size": 610472,
            "download_count": 39764,
            "created_at": "2023-06-06T06:37:01Z",
            "updated_at": "2023-06-06T06:37:01Z",
            "browser_download_url": "https://github.com/typecho/typecho/releases/download/v1.2.1/typecho.zip"
        }
    ],
    "tarball_url": "https://api.github.com/repos/typecho/typecho/tarball/v1.2.1",
    "zipball_url": "https://api.github.com/repos/typecho/typecho/zipball/v1.2.1",
    "body": "## What's Changed\r\n* Fix checkVersion by @sy-records in https://github.com/typecho/typecho/pull/1356\r\n* 修复Sqlite目录带.校验不通过问题 by @maoxian-1 in https://github.com/typecho/typecho/pull/1357\r\n* Pr/1344 by @joyqi in https://github.com/typecho/typecho/pull/1360\r\n* Fix pgsql reset id error by @sy-records in https://github.com/typecho/typecho/pull/1369\r\n* Update admin welcome tip by @sy-records in https://github.com/typecho/typecho/pull/1389\r\n* Enhancement of Typecho\\Cookie by @sy-records in https://github.com/typecho/typecho/pull/1399\r\n* fix CORS issues in preview page. 修复文章预览页面跨域问题 by @Valpha in https://github.com/typecho/typecho/pull/1400\r\n* 模板缩略图支持识别webp图片后缀 by @jrotty in https://github.com/typecho/typecho/pull/1403\r\n* 修正注释 by @jrotty in https://github.com/typecho/typecho/pull/1411\r\n* Fix notice not clear by @sy-records in https://github.com/typecho/typecho/pull/1416\r\n* 修复管理员进入其他用户文章列表时显示所有文章的bug by @jrotty in https://github.com/typecho/typecho/pull/1415\r\n* Fix QUIC/https Mixed Content by @MBRjun in https://github.com/typecho/typecho/pull/1423\r\n* Add admin/footer.php begin plugin by @sy-records in https://github.com/typecho/typecho/pull/1426\r\n* Fix missing change themeUrl by @sy-records in https://github.com/typecho/typecho/pull/1431\r\n* Fix category creation error when using xmlrpc by @sy-records in https://github.com/typecho/typecho/pull/1443\r\n* Fix #1449 by @sy-records in https://github.com/typecho/typecho/pull/1450\r\n* Minor update by @vndroid in https://github.com/typecho/typecho/pull/1451\r\n* Minor update by @vndroid in https://github.com/typecho/typecho/pull/1460\r\n* Fix the error of getting request parameters by @sy-records in https://github.com/typecho/typecho/pull/1464\r\n* Fix multiple calls returning the same object (#1412) by @benzBrake in https://github.com/typecho/typecho/pull/1478\r\n* Fix use SQLite error of windows install by @sy-records in https://github.com/typecho/typecho/pull/1471\r\n* Adjust style of edit comments by @sy-records in https://github.com/typecho/typecho/pull/1483\r\n* Fix comments feed jump error by @sy-records in https://github.com/typecho/typecho/pull/1491\r\n* Fix #1495 by @sy-records in https://github.com/typecho/typecho/pull/1496\r\n* Fix unsafe use of jQuery .html() by @l2dy in https://github.com/typecho/typecho/pull/1382\r\n* Fix htmlspecialchars error for feed by @sy-records in https://github.com/typecho/typecho/pull/1522\r\n* Use https links by @l2dy in https://github.com/typecho/typecho/pull/1280\r\n* Support ssl for pdo_mysql and mysqli by @sy-records in https://github.com/typecho/typecho/pull/1525\r\n* Fix: php 8.1 strtolower not allow null value by @benzBrake in https://github.com/typecho/typecho/pull/1559\r\n* Fix an XSS vulnerability in v1.2.1-rc by @FaithPatrick in https://github.com/typecho/typecho/pull/1561\r\n* fix php 8.1 Deprecated: htmlspecialchars(): Passing null to parameter #1 by @benzBrake in https://github.com/typecho/typecho/pull/1570\r\n* Add a prompt message for manual database creation by @sy-records in https://github.com/typecho/typecho/pull/1348\r\n* fix #1574 by @joyqi in https://github.com/typecho/typecho/pull/1575\r\n* improve release ci, upload built asset after new release published. by @joyqi in https://github.com/typecho/typecho/pull/1576\r\n* 重复执行判断的优化 by @logdd in https://github.com/typecho/typecho/pull/1586\r\n\r\n## New Contributors\r\n* @maoxian-1 made their first contribution in https://github.com/typecho/typecho/pull/1357\r\n* @Valpha made their first contribution in https://github.com/typecho/typecho/pull/1400\r\n* @MBRjun made their first contribution in https://github.com/typecho/typecho/pull/1423\r\n* @vndroid made their first contribution in https://github.com/typecho/typecho/pull/1451\r\n* @benzBrake made their first contribution in https://github.com/typecho/typecho/pull/1478\r\n* @FaithPatrick made their first contribution in https://github.com/typecho/typecho/pull/1561\r\n* @logdd made their first contribution in https://github.com/typecho/typecho/pull/1586\r\n\r\n**Full Changelog**: https://github.com/typecho/typecho/compare/v1.2.0...v1.2.1",
    "reactions": {
        "url": "https://api.github.com/repos/typecho/typecho/releases/107460469/reactions",
        "total_count": 37,
        "+1": 26,
        "-1": 0,
        "laugh": 0,
        "hooray": 11,
        "confused": 0,
        "heart": 0,
        "rocket": 0,
        "eyes": 0
    },
    "mentions_count": 11
}
最后修改:2023 年 11 月 29 日

感谢看完,可以点个赞~请作者喝杯咖啡~ヾ(◍°∇°◍)ノ゙❤