分享一个统计 github 项目每天的 star 数量脚本

14次阅读

共计 1969 个字符,预计需要花费 5 分钟才能阅读完成。

背景

github 开源项目可以看到 star 总数,但是看不到每个 star 的时间,也没有统计每天的 star 的数据,有时我们想看看某天一共有多少个 star,于是我写了个脚本统计每天的 star 数量。

实现

github 项目的每个 star 的时间可以通过 github 的 API https://api.github.com/repos/${author}/${repository}/stargazers 获取每个 star 的时间,下面是一个简单的例子:

curl -s -H "Accept: application/vnd.github.v3.star+json" 
        "https://api.github.com/repos/Liubsyy/FindInstancesOfClass/stargazers?per_page=3&page=1"

可获得以下结果:

[
  {
    "starred_at": "2023-10-25T01:51:45Z",
    "user": {}},
  {
    "starred_at": "2023-12-03T09:04:53Z",
    "user": {}},
  {
    "starred_at": "2023-12-18T06:52:31Z",
    "user": {}}
]

其中 starred_at 就是 star 的 UTC 时间,这个时间再加上 8 个小时的时区差,就是北京时间,然后按天进行统计即可。

以下是具体的脚本:

#!/bin/bash

#repository
stat_repository="Liubsyy/FindInstancesOfClass"
token=""

function fetch_stargazers {
    local page=1
    local per_page=100
    local data

    while true
    do
        data=$(curl -s -H "Accept: application/vnd.github.v3.star+json" 
        -H "Authorization: ${token:+token $token}" 
        "https://api.github.com/repos/$stat_repository/stargazers?per_page=$per_page&page=$page")

        if [${#data} -lt 10 ]; then
            break
        fi

        starred_at=$(echo "$data" | grep -o '"starred_at": "[^"]*"'| awk -F'"' '{print $4}')

        if [${#starred_at} -lt 10 ]; then
            break
        fi

        # UTC +8h
        for timestamp in $starred_at
        do
            #linux
            #new_time=$(date -u -d "$timestamp 8 hours" +"%Y-%m-%d")

            #mac
            new_time=$(date -v +8H -j -f "%Y-%m-%dT%H:%M:%SZ" "$timestamp" "+%Y-%m-%d")

            echo "$new_time"
        done
        ((page++))
    done
}

try_data=$(curl -s -H "Accept: application/vnd.github.v3.star+json" 
-H "Authorization: ${token:+token $token}" 
"https://api.github.com/repos/$stat_repository/stargazers?per_page=1&page=1")
if echo "$try_data" | grep -q "API rate limit"; then
    echo "$try_data"
    exit 1
fi

if echo "$try_data" | grep -q "Not Found"; then
    echo "$try_data"
    exit 1
fi

echo "date   stars"
fetch_stargazers | sort | uniq -c | awk '{print $2 , $1}'

执行脚本可得到每天统计的结果:

date   stars
2023-10-25 1
2023-12-03 1
2023-12-18 1
2023-12-22 1
2024-01-02 1
2024-01-09 1
2024-01-16 3
2024-01-17 2
2024-01-31 1
2024-02-18 1
2024-05-07 1
2024-05-11 2
2024-05-17 1
2024-05-21 1
2024-06-12 1
2024-07-08 1
2024-07-09 1
2024-07-12 1
2024-07-26 1

这个 API 访问频率有限制,最好是带上 token 进行访问统计,另外 linux 和 mac 的 date 命令有差异,linux 系统 new_time 这里可去掉注释用 linux 的命令。

本脚本只提供一种思路,根据本思路用任何编程语言和脚本都可以实现 star 数量的统计。

正文完
 0