-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgithub-stars-backup.sh
executable file
·58 lines (47 loc) · 1.29 KB
/
github-stars-backup.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
58
#!/bin/bash
# Description: Backup starred repositories for a Github user, output to JSON and plain text
# License: WTFPL
# Authors: nodiscc <[email protected]>
set -o errexit
set -o nounset
usage="$0 USERNAME OUTPUT_FILE"
username=${1:-}
outfile=${2:-}
tmpfile="github-stars.json"
##############################
# download starred repositories as JSON from the API, unauthenticated
# note that no error will be thrown if you exceed the API rate limit, but the results will be wrong
function download_starred() {
i="1"
response=$(curl "https://api.github.com/users/$username/starred?per_page=100\&page=$i")
echo "$response" >| "$tmpfile"
until [[ "$response" = "[
]" ]]; do
i=$(( "$i" +1 ))
response=$(curl "https://api.github.com/users/$username/starred?per_page=100\&page=$i")
echo "$response" >> "$tmpfile"
done
}
function json_to_txt() {
repos=$(grep full_name "$tmpfile" | cut -d"\"" -f4)
for i in $repos; do
echo "https://github.com/$i"
done >| "$outfile"
}
function _main() {
echo $tmpfile
if [[ -z "$username" ]]; then
echo -e "ERROR: no username provided"
echo -e "USAGE: $usage"
exit 1
fi
if [[ -z "$outfile" ]]; then
echo -e "ERROR: no output file provided"
echo -e "USAGE: $usage"
exit 1
fi
download_starred
json_to_txt
}
############################
_main