Update usercount/jsDelivr shields in chatgpt/ READMEs #17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update usercount/jsDelivr shields in chatgpt/ READMEs | |
on: | |
schedule: | |
- cron: "45 1 * * 5" # every Fri @ 1:45 AM | |
permissions: | |
contents: read | |
jobs: | |
update-chatgpt-usercount-jsd-shields: | |
runs-on: ubuntu-latest | |
env: | |
TZ: PST8PDT | |
steps: | |
- name: Checkout adamlui/userscripts | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.REPO_SYNC_PAT }} | |
repository: adamlui/userscripts | |
path: adamlui/userscripts | |
- name: Fetch/sum GF script user + JSD request counts | |
run: | | |
declare -A GF_SCRIPTS=( # for Greasy Fork script user counts | |
["460805"]="Autoclear ChatGPT History" | |
["462440"]="BraveGPT" | |
["466789"]="ChatGPT Auto-Continue" | |
["462422"]="ChatGPT Auto Refresh" | |
["465051"]="ChatGPT Infinity" | |
["461473"]="ChatGPT Widescreen Mode" | |
["459849"]="DuckDuckGPT" | |
["478597"]="GoogleGPT" | |
) | |
JSD_GH_REPOS=( # for jsDelivr request counts | |
"adamlui/autoclear-chatgpt-history" | |
"KudoAI/bravegpt" | |
"adamlui/chatgpt-auto-continue" | |
"adamlui/chatgpt-auto-refresh" | |
"adamlui/chatgpt-infinity" | |
"adamlui/chatgpt-widescreen" | |
"KudoAI/duckduckgpt" | |
"KudoAI/googlegpt" | |
) | |
expand_num() { # expand nums abbreviated w/ 'k' or 'm' suffix to integers | |
local num=$(echo "$1" | tr '[:upper:]' '[:lower:]') # convert to lowercase | |
if [[ $num =~ k$ ]] ; then | |
num="${num%k}" # remove 'k' suffix | |
num=$(awk "BEGIN { printf \"%.0f\", $num * 1000 }") # multiply by 1000 | |
elif [[ $num =~ m$ ]] ; then | |
num="${num%m}" # remove 'm' suffix | |
num=$(awk "BEGIN { printf \"%.0f\", $num * 1000000 }") # multiply by 1000000 | |
fi ; echo "$num" | |
} | |
format_total() { | |
local num=$1 ; first_digit="${num:0:1}" second_digit="${num:1:1}" | |
second_digit_rounded=$(( second_digit < 5 ? 0 : 5 )) | |
if (( num >= 1000000000 )) ; then # 1B+ w/ one decimal place | |
formatted_num="$(( num / 1000000000 ))" | |
remainder=$(( (num % 1000000000) / 100000000 )) | |
if (( remainder != 0 )) ; then formatted_num+=".$remainder" ; fi | |
formatted_num+="B+" | |
elif (( num >= 10000000 )) ; then # abbr 10,000,000+ to 999,000,000+ | |
formatted_num=$(printf "%'.f+" $((( num / 1000000 ) * 1000000 ))) | |
elif (( num >= 1000000 )) ; then # abbr 1,000,000+ to 9,500,000+ | |
formatted_num="${first_digit},${second_digit}00,000+" | |
elif (( num >= 100000 )) ; then # abbr 100,000+ to 950,000+ | |
formatted_num="${first_digit}${second_digit_rounded}0,000+" | |
elif (( num >= 10000 )) ; then # abbr 10,000+ to 90,000+ | |
formatted_num="${first_digit}0,000+" | |
elif (( num >= 1000 )) ; then # abbr 1K to 9.9K | |
formatted_num="$(( num / 1000 ))" | |
remainder=$(( (num % 1000) / 100 )) | |
if (( remainder != 0 )) ; then formatted_num+=".$remainder" ; fi | |
formatted_num+="K" | |
else formatted_num="$num" ; fi # preserve <1K as is | |
echo "$formatted_num" | |
} | |
# Sort GF_SCRIPTS alphabetically for more readable logging | |
sorted_gf_scripts=() | |
for gf_script_id in "${!GF_SCRIPTS[@]}" ; do | |
gf_script_name="${GF_SCRIPTS[$gf_script_id]}" | |
gf_sorted_scripts+=("$gf_script_name:$gf_script_id") | |
done | |
IFS=$'\n' gf_sorted_scripts=($(sort <<<"${gf_sorted_scripts[*]}")) | |
# Fetch/sum Greasy Fork script user counts | |
for tuple in "${gf_sorted_scripts[@]}" ; do | |
gf_script_id="${tuple##*:}" gf_script_name="${tuple%%:*}" | |
gf_users=$(curl -s "https://img.shields.io/greasyfork/dt/$gf_script_id" | | |
sed -n 's/.*<title>installs: \([0-9.k]\+\)*<\/title>.*/\1/Ip') | |
gf_users=$(expand_num "$gf_users") | |
echo "$gf_script_name GF users: $gf_users" | |
total_users=$((total_users + gf_users)) | |
done ; echo -e "\n-----\nTotal Greasy Fork users: $total_users\n-----\n" | |
# Fetch/sum jsDelivr request counts | |
for repo in "${JSD_GH_REPOS[@]}" ; do | |
repo_requests=$(curl -s "https://img.shields.io/jsdelivr/gh/hm/$repo.svg" | | |
sed -n -E 's|.*<title>jsdelivr: ([0-9,.km]+).*</title>.*|\1|Ip') | |
repo_requests=$(expand_num "$repo_requests") | |
echo "$repo jsDelivr hits: $repo_requests" | |
total_requests=$((total_requests + repo_requests)) | |
done ; echo -e "\n-----\nTotal monthly jsDelivr requests: $total_requests\n-----\n" | |
# Format totals | |
formatted_total_users=$(format_total "$total_users") | |
echo "Formatted total Greasy Fork users: $formatted_total_users" | |
formatted_total_requests=$(format_total "$total_requests") | |
echo "Formatted total monthly jsDelivr requests: $formatted_total_requests" | |
# Expose totals for update step next | |
echo "TOTAL_USERS=$formatted_total_users" >> $GITHUB_ENV | |
echo "TOTAL_REQUESTS=$formatted_total_requests" >> $GITHUB_ENV | |
- name: Update README shields | |
run: | | |
cd ${{ github.workspace }}/adamlui/userscripts | |
TOTAL_USERS="${{ env.TOTAL_USERS }}" | |
TOTAL_REQUESTS="${{ env.TOTAL_REQUESTS }}" | |
# Update usercount shields | |
if [ "$TOTAL_USERS" == "0" ] ; then echo "Error getting total usercount" | |
else | |
for readme in $(find chatgpt/docs/ -name "README.md") ; do | |
old_readme=$(<"$readme") | |
sed -i -E "s|(badge/[^-]+-)[0-9.,km+]+([^?]+\?logo=weightsandbiases)|\1$TOTAL_USERS\2|gI" "$readme" | |
new_readme=$(<"$readme") | |
if [ "$old_readme" != "$new_readme" ] ; then users_updated=true ; fi | |
done | |
if [ "$users_updated" = true ] ; then echo "Usercount shields updated to $TOTAL_USERS" | |
else echo "Usercount shields already up-to-date" ; fi | |
fi | |
# Update jsDelivr shields | |
if [ "$TOTAL_REQUESTS" == "0" ] ; then echo "Error getting total jsDelivr requests" | |
else | |
for readme in $(find chatgpt/docs/ -name "README.md") ; do | |
old_readme=$(<"$readme") | |
sed -i -E "s|(badge/jsDelivr_[^-]+-)[0-9.,km+]+|\1$TOTAL_REQUESTS|Ig" "$readme" | |
new_readme=$(<"$readme") | |
if [ "$old_readme" != "$new_readme" ] ; then requests_updated=true ; fi | |
done | |
if [ "$requests_updated" = true ] ; then echo "jsDelivr shields updated to $TOTAL_REQUESTS" | |
else echo "jsDelivr shields already up-to-date" ; fi | |
fi | |
# Define commit msg for push step next | |
if [ "$users_updated" = true ] && [ "$requests_updated" = true ] ; then | |
multi_shield_types_updated=true ; fi | |
commit_msg="Updated " | |
[ "$users_updated" = true ] && commit_msg+="usercount" | |
[ "$multi_shield_types_updated" = true ] && commit_msg+="/" | |
[ "$requests_updated" = true ] && commit_msg+="jsDelivr" | |
commit_msg+=" shield counters in chatgpt/ READMEs" | |
echo "COMMIT_MSG=$commit_msg" >> $GITHUB_ENV # expose as env var | |
# Set Updated flag to check in subseuqent steps | |
if [ "$users_updated" = true ] || [ "$requests_updated" = true ] ; then | |
echo "SHIELDS_UPDATED=true" >> $GITHUB_ENV ; fi | |
- name: Config committer | |
if: env.SHIELDS_UPDATED == 'true' | |
run: | | |
gpg --batch --import <(echo "${{ secrets.GPG_PRIVATE_KEY }}") | |
git config --global commit.gpgsign true | |
git config --global user.name "kudo-sync-bot" | |
git config --global user.email "[email protected]" | |
git config --global user.signingkey "${{ secrets.GPG_PRIVATE_ID }}" | |
- name: Push changes to adamlui/userscripts | |
if: env.SHIELDS_UPDATED == 'true' | |
run: | | |
cd ${{ github.workspace }}/adamlui/userscripts | |
git add . | |
git commit -n -m "${{ env.COMMIT_MSG }}" || true | |
git push |