Skip to content

Update usercount/jsDelivr shields in chatgpt/ READMEs #1

Update usercount/jsDelivr shields in chatgpt/ READMEs

Update usercount/jsDelivr shields in chatgpt/ READMEs #1

name: Update usercount + jsDelivr shields in chatgpt/ READMEs
on:
schedule:
- cron: "45 1 * * 1,3,5" # every Mon/Wed/Fri @ 1:45 AM
jobs:
update-chatgpt-usercount-jsd-shields:
runs-on: ubuntu-latest
steps:
- name: Checkout adamlui/userscripts
uses: actions/checkout@v3
with:
token: ${{ secrets.REPO_SYNC_PAT }}
repository: adamlui/userscripts
path: adamlui/userscripts
- name: Fetch/sum GF script user + JSD request counts
id: get-stats
run: |
LC_NUMERIC=en_US # to insert commas
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" "adamlui/chatgpt-auto-continue"
"adamlui/chatgpt-auto-refresh" "adamlui/chatgpt-infinity" "adamlui/chatgpt-widescreen"
"KudoAI/bravegpt" "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 as outputs for update step next
echo "total_users=$formatted_total_users" >> $GITHUB_OUTPUT
echo "total_requests=$formatted_total_requests" >> $GITHUB_OUTPUT
- name: Update README shields
id: update-shields
run: |
cd ${{ github.workspace }}/adamlui/userscripts
total_users="${{ steps.get-stats.outputs.total_users }}"
total_requests="${{ steps.get-stats.outputs.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 "s/\(badge\/[^-]*-\)[0-9.,km+]\+-/\1$total_users-/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_OUTPUT # expose as output
- name: Push to adamlui/userscripts
run: |
cd ${{ github.workspace }}/adamlui/userscripts
git config --global user.name "kudo-sync-bot"
git config --global user.email "auto-sync@kudoai.com"
git add .
git commit -n -m "${{ steps.update-shields.outputs.commit_msg }}" || true
git push