-
-
Notifications
You must be signed in to change notification settings - Fork 12
180 lines (158 loc) · 8.2 KB
/
update-root-usercount-clones-shields-3x-weekly.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
name: Update usercount/clones shields in root READMEs, sync to adamlui/chatgpt-apps
on:
schedule:
- cron: "28 3 * * 2,4,6" # every Tue/Thu/Sat @ 3:28 AM
jobs:
update-root-usercount-clones-jsd-shields:
runs-on: ubuntu-latest
steps:
- name: Checkout adamlui/chatgpt-widescreen
uses: actions/checkout@v4
with:
token: ${{ secrets.REPO_SYNC_PAT }}
repository: adamlui/chatgpt-widescreen
path: adamlui/chatgpt-widescreen
- name: Checkout adamlui/chatgpt-apps
uses: actions/checkout@v4
with:
token: ${{ secrets.REPO_SYNC_PAT }}
repository: adamlui/chatgpt-apps
path: adamlui/chatgpt-apps
- name: Fetch/sum user + clone counts
id: get-stats
run: |
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"
}
# Fetch Chrome active user count
base_url="https://img.shields.io/chrome-web-store/users/"
app_id="jgnjpnmofkalfliddjelaciggjgnphgm"
chrome_users=$(curl -s "$base_url$app_id" |
sed -n 's/.*<title>users: \([0-9.k]\+\)*<\/title>.*/\1/Ip')
chrome_users=$(expand_num "$chrome_users")
echo -e "\nChrome users: $chrome_users"
# Fetch Edge active user count
base_url="https://microsoftedge.microsoft.com/addons/getproductdetailsbycrxid/"
app_id="obnaaalnokmchdoagnhmllakaclaaooa"
edge_users=$(curl -s "$base_url$app_id" |
sed -n 's/.*"activeInstallCount":\([0-9]*\).*/\1/p')
echo "Edge users: $edge_users"
# Fetch Greasy Fork total user count
base_url="https://img.shields.io/greasyfork/dt/"
app_id="461473"
gf_users=$(curl -s "$base_url$app_id" |
sed -n 's/.*<title>installs: \([0-9.k]\+\)*<\/title>.*/\1/Ip')
gf_users=$(expand_num "$gf_users")
echo "Greasy Fork users: $gf_users"
# Sum user counts
total_users=$((chrome_users + edge_users + gf_users))
echo -e "\n-----\nTotal users: $total_users\n-----\n"
# Fetch/calculate git clone count
biweekly_clones=$(curl -sSL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.REPO_SYNC_PAT }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/adamlui/chatgpt-widescreen/traffic/clones" |
sed -n -E '0,/.*"count": ([0-9]+).*/ s//\1/p')
total_clones=$((biweekly_clones * 2))
echo "Biweekly git clones: $biweekly_clones"
echo -e "\n-----\nTotal monthly git clones: $total_clones\n-----\n"
# Format totals
formatted_total_users=$(format_total "$total_users")
echo "Formatted total users: $formatted_total_users"
formatted_total_clones=$(format_total "$total_clones")
echo "Formatted total monthly git clones: $formatted_total_clones"
# Expose as outputs for update step next
echo "total_users=$formatted_total_users" >> $GITHUB_OUTPUT
echo "total_clones=$formatted_total_clones" >> $GITHUB_OUTPUT
- name: Update README shields
id: update-shields
run: |
cd ${{ github.workspace }}/adamlui/chatgpt-widescreen
total_users="${{ steps.get-stats.outputs.total_users }}"
total_clones="${{ steps.get-stats.outputs.total_clones }}"
# Update userccount shields
if [ "$total_users" == "0" ] ; then echo "Error getting total usercount"
else # perform update
for readme in $(find 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 git clones shield
if [ "$total_clones" == "0" ] ; then echo "Error getting total git clones"
else # perform update
for readme in $(find docs/ -name "README.md") ; do
old_readme=$(<"$readme")
sed -i -E "s|(badge/[^-]+-)[0-9.,km+]+(/[^?]+\?logo=github)|\1$total_clones\2|gI" "$readme"
new_readme=$(<"$readme")
if [ "$old_readme" != "$new_readme" ] ; then clones_updated=true ; fi
done
if [ "$clones_updated" = true ] ; then echo "Git clones shields updated to $total_clones"
else echo "Git clones shields already up-to-date" ; fi
fi
# Define commit msg intro for push steps
if [ "$users_updated" = true ] && [ "$clones_updated" = true ] ; then
multi_shield_types_updated=true ; fi
commit_msg_intro="Updated "
[ "$users_updated" = true ] && commit_msg_intro+="usercount"
[ "$multi_shield_types_updated" = true ] && commit_msg_intro+="/"
[ "$clones_updated" = true ] && commit_msg_intro+="clones"
commit_msg_intro+=" shield counters"
echo "commit_msg_intro=$commit_msg_intro" >> $GITHUB_OUTPUT # expose as output
- name: Sync ./* to adamlui/chatgpt-apps/chatgpt-widescreen/*
run: |
rsync -avhr --delete --exclude={'.*','eslint*','package*json'} \
${{ github.workspace }}/adamlui/chatgpt-widescreen/ \
${{ github.workspace }}/adamlui/chatgpt-apps/chatgpt-widescreen/
- name: Init committer name/email
run: |
git config --global user.name "kudo-sync-bot"
git config --global user.email "auto-sync@kudoai.com"
- name: Push to adamlui/chatgpt-widescreen
run: |
cd ${{ github.workspace }}/adamlui/chatgpt-widescreen
git add .
git commit -n -m "${{ steps.update-shields.outputs.commit_msg_intro }} in root READMEs" || true
git push
- name: Push changes to adamlui/chatgpt-apps
run: |
cd ${{ github.workspace }}/adamlui/chatgpt-apps
git add .
git commit -n -m "${{ steps.update-shields.outputs.commit_msg_intro }} in ChatGPT Widescreen root READMEs" || true
git push