-
Notifications
You must be signed in to change notification settings - Fork 1
/
pkgstats.sh
executable file
·57 lines (46 loc) · 1.38 KB
/
pkgstats.sh
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
#!/bin/sh
# path: /home/klassiker/.local/share/repos/shell/pkgstats.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/shell
# date: 2024-08-30T06:22:16+0200
# config
url="https://pkgstats.archlinux.de/api/packages"
out_dir="$HOME/Public/pkgstats"
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 %s %s %s %s\n" \
"$name" \
"$month" \
"$count" \
"$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