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

50 天前
 liubsyy

背景

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 数量的统计。

1102 次点击
所在节点    分享创造
3 条回复
bookcat
50 天前
你可能需要这个
https://star-history.com/
liubsyy
50 天前
@bookcat 我用过这个 star 趋势图,挺好的,但是不一定能看到任何一天的 star 数量,而且这个趋势图没有+8h 的时区
chesha1
49 天前
也许可以了解一下这个:

https://ossinsight.io/docs/api/stargazers-history

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1061150

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX