跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

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

发表于

背景

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

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.