-
-
Notifications
You must be signed in to change notification settings - Fork 20
165 lines (146 loc) · 7.68 KB
/
update-chatgpt-usercount-jsd-shields-triweekly.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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