-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaapi.sh
64 lines (53 loc) · 1.99 KB
/
shaapi.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
59
60
61
62
63
64
#!/bin/sh
sha_token=`cat sha.token`
SHA_JSONDIR="json-sha"
SHA_RATE_LEFT=-1
SHA_RATE_EPOCH=0
function sha_rateleft {
#curl --silent -i -H "Authorization: Bearer $sha_token" -X GET https://archive.softwareheritage.org/api/1/stat/counters/ | grep 'X-RateLimit-Remaining:' | cut -d' ' -f2
curl --silent -i -X GET https://archive.softwareheritage.org/api/1/stat/counters/ | grep 'X-RateLimit-Remaining:' | cut -d' ' -f2
}
function sha_ratelimit {
current_epoch=`date +%s`
sleep_seconds=$((SHA_RATE_EPOCH - current_epoch + 5))
echo "[`date`] sleeping $sleep_seconds seconds until `date -r $SHA_RATE_EPOCH`..." >>/dev/stderr
sleep $sleep_seconds
SHA_RATE_LEFT=-1
SHA_RATE_EPOCH=0
}
function sha_unrated {
#rm -f headers
#curl --silent -D headers -H "Authorization: Bearer $sha_token" -X GET https://archive.softwareheritage.org/api/1/$1
#SHA_RATE_LEFT=`grep 'X-RateLimit-Remaining:' headers | cut -d' ' -f2 | tr -d '[:space:]'`
#SHA_RATE_EPOCH=`grep 'X-RateLimit-Reset:' headers | cut -d' ' -f2 | tr -d '[:space:]'`
#rm -f headers
rm -f headers2
curl --silent -D headers2 -X GET https://archive.softwareheritage.org/api/1/$1
SHA_RATE_LEFT=`grep 'X-RateLimit-Remaining:' headers2 | cut -d' ' -f2 | tr -d '[:space:]'`
SHA_RATE_EPOCH=`grep 'X-RateLimit-Reset:' headers2 | cut -d' ' -f2 | tr -d '[:space:]'`
rm -f headers2
}
function sha_api {
if [[ $SHA_RATE_LEFT -lt 0 ]]; then
sha_unrated "stat/counters/" > /dev/null
echo "API ratelimit left: $SHA_RATE_LEFT" >>/dev/stderr
echo "API ratelimit epoch: $SHA_RATE_EPOCH" >>/dev/stderr
fi
if [[ $SHA_RATE_LEFT -lt 1 ]]; then
echo "SHA API rate limit reached" >>/dev/stderr
sha_ratelimit
fi
}
function sha_api_commit {
sha_api
sha_unrated "revision/$1/"
}
function sha_api_repo {
sha_api
sha_unrated "origin/$1/get/"
}
function sha_json_path {
first=`echo $1 | cut -c1-1`
second=`echo $1 | cut -c2-2`
echo "$first/$second/$1.json"
}