V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
liubsyy
V2EX  ›  分享创造

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

  •  
  •   liubsyy · 48 天前 · 1101 次点击
    这是一个创建于 48 天前的主题,其中的信息可能已经有所发展或是发生改变。

    背景

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

    3 条回复    2024-07-31 13:11:45 +08:00
    bookcat
        1
    bookcat  
       48 天前
    你可能需要这个
    https://star-history.com/
    liubsyy
        2
    liubsyy  
    OP
       48 天前
    @bookcat 我用过这个 star 趋势图,挺好的,但是不一定能看到任何一天的 star 数量,而且这个趋势图没有+8h 的时区
    chesha1
        3
    chesha1  
       47 天前
    也许可以了解一下这个:

    https://ossinsight.io/docs/api/stargazers-history
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   998 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:06 · PVG 03:06 · LAX 12:06 · JFK 15:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.