-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpkgstats.sh
More file actions
executable file
·62 lines (50 loc) · 1.52 KB
/
pkgstats.sh
File metadata and controls
executable file
·62 lines (50 loc) · 1.52 KB
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
#!/bin/sh
# path: /home/klassiker/.local/share/repos/shell/pkgstats.sh
# author: klassiker [mrdotx]
# url: https://github.com/mrdotx/shell
# date: 2026-05-18T05:28:52+0200
# config
url="https://pkgstats.archlinux.de/api/packages"
out_dir="$HOME/Public/pkgstats"
round() {
# WORKAROUND: printf "%.0f" not completely converted in the dash shell
awk "BEGIN {printf \"%.$1f\", $2}"
}
extract_data() {
printf "%s" "$1" \
| awk -F "\"$2\":" '{print $2}' \
| cut -d ',' -f1 \
| tr -d "\""
}
request() {
data=$(curl -fsS "$url/$1")
name=$(extract_data "$data" "name")
samples=$(extract_data "$data" "samples")
count=$(extract_data "$data" "count")
popularity=$(extract_data "$data" "popularity")
month=$(extract_data "$data" "startMonth")
printf "%s %d %d %s %d\n" \
"$name" \
"$month" \
"$count" \
"$(round 2 "$popularity")" \
"$samples"
}
for pkg in "$@"; do
# if pkg is a fullpath use only basename
pkg=$(basename "$pkg")
file_header="Name Month Count Popularity Samples"
printf "%s" "$pkg"
[ -e "$out_dir/$pkg.csv" ] \
&& output=$(sed "/$file_header/d" "$out_dir/$pkg.csv") \
&& output=$(printf "%s\n%s" \
"$output" \
"$(request "$pkg")" \
)
[ -z "$output" ] \
&& output="$(request "$pkg")"
printf "%s\n" "$file_header" > "$out_dir/$pkg.csv"
printf "%s" "$output" | sort -ur >> "$out_dir/$pkg.csv"
printf " => finished\n"
unset output
done