Skip to content

Instantly share code, notes, and snippets.

@mbohun
Last active March 13, 2023 20:06
Show Gist options
  • Save mbohun/b161521b2440b9f08b59 to your computer and use it in GitHub Desktop.
Save mbohun/b161521b2440b9f08b59 to your computer and use it in GitHub Desktop.
simple BASH/curl scipt for github REST API (HTTP GET queries)
$ ./githubapi-get.sh $GITHUBTOKEN /users/mbohun/repos
HTTP/1.1 200 OK
Server: GitHub.com
Date: Wed, 04 Mar 2015 04:30:29 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 155683
Status: 200 OK
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4942
X-RateLimit-Reset: 1425443499
Cache-Control: private, max-age=60, s-maxage=60
ETag: "0a577732c2798b325a04f7130536d23f"
X-OAuth-Scopes: gist, repo, user
X-Accepted-OAuth-Scopes: 
Vary: Accept, Authorization, Cookie, X-GitHub-OTP
X-GitHub-Media-Type: github.v3
Link: <https://api.github.com/user/1772897/repos?page=2>; rel="next", <https://api.github.com/user/1772897/repos?page=3>; rel="last"
X-XSS-Protection: 1; mode=block
X-Frame-Options: deny
Content-Security-Policy: default-src 'none'
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
Access-Control-Allow-Origin: *
X-GitHub-Request-Id: 9853C7D8:4CA0:7128FF:54F68A65
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Content-Type-Options: nosniff
Vary: Accept-Encoding
X-Served-By: a241e1a8264a6ace03db946c85b92db3

[
  {
    "id": 23527440,
    "name": "ala-cas",
    "full_name": "mbohun/ala-cas",
    "owner": {
      "login": "mbohun",
      "id": 1772897,
      "avatar_url": "https://avatars.githubusercontent.com/u/1772897?v=3",
      "gravatar_id": "",
      "url": "https://api.github.com/users/mbohun",
      "html_url": "https://github.com/mbohun",
      "followers_url": "https://api.github.com/users/mbohun/followers",
      "following_url": "https://api.github.com/users/mbohun/following{/other_user}",
      "gists_url": "https://api.github.com/users/mbohun/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/mbohun/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/mbohun/subscriptions",
      "organizations_url": "https://api.github.com/users/mbohun/orgs",
      "repos_url": "https://api.github.com/users/mbohun/repos",
      "events_url": "https://api.github.com/users/mbohun/events{/privacy}",
      "received_events_url": "https://api.github.com/users/mbohun/received_events",
      "type": "User",
      "site_admin": false
    },
    "private": false,
    
    ...
./githubapi-get.sh $GITHUBTOKEN /users/mbohun/repos | grep '"name":' |sort
    "name": "ALA4R",
    "name": "Free-Social-Icons",
    "name": "JSON.sh",
    "name": "SMILES.js",
    "name": "ala-cas",
    "name": "ala-cas-client",
    "name": "ala-downloads",
    "name": "ala-expert",
    "name": "ala-hub",
    "name": "ala-install",
    "name": "ala-logger-service",
    "name": "ala-name-matching",
    "name": "ala-names-generator",
    "name": "ala-web-theme",
    "name": "ala_biocache_test",
    "name": "alerts",
    "name": "analysis-service",
    "name": "apikey",
    "name": "appd-hub",
    "name": "bhl-ftindex-manage",
    "name": "bie-webapp2",
    "name": "biocache-hubs",
    "name": "biocache-store",
    "name": "blog-00",
    "name": "dashboard",
    "name": "doctoc",
    "name": "ecodata",
    "name": "emacs",
    "name": "fieldcapture",
    "name": "generic-hub",
    "name": "gfx",
    "name": "gtk_shader_ide",
    "name": "jq",
    "name": "layers-service",
    "name": "linux",
    "name": "markdown-here",
    "name": "mbohun.github.io",
    "name": "mbohun_graph-experiments",
    "name": "misc",
    "name": "molecules",
    "name": "mpc",
    "name": "ozcam-hub",
    "name": "regions",
    "name": "rlm_ftress",
    "name": "spatial-logger",
    "name": "spatial-portal",
    "name": "specieslist-webapp",
    "name": "specimenbrowser",
    "name": "stream-handbook",
    "name": "taxon-overflow",
    "name": "test-ALA4R",
    "name": "test-repo-one",
    "name": "test-repo-two",
    "name": "test-travis-repo",
    "name": "travis-build-configuration",
    "name": "trinity",
    "name": "tviewer",
    "name": "userdetails",
    "name": "virt-test",
    "name": "volunteer-portal",
    "name": "whatever",
    "name": "wikigraph",
bash-3.2$ ./githubapi-get.sh $GITHUBTOKEN /orgs/AtlasOfLivingAustralia/members | grep '"login":' | sed -e 's/^.*": "//g' -e 's/",.*$//g'
acvaughan
adam-collins
ala-bugherd
aruizca
brynk
burkeker
charvolant
chrisala
davidbairdala
djtfmartin
hudson-ala
jamirali
jenkins-ala
jjvanderwal
linaizhong
m-hope
M-Nicholls
m-r-c
Markew
matthewandrews
mbohun
melecoq
nickdos
nielsklazenga
pbrenton
PeggyNewman
raymondben
RhiannonStephens
sadeghim
santiagomtzdelariva
sat01a
sathishala
sbearcsiro
snomelf
Tasilee
temi
turoart
#!/bin/bash
if [ ${#@} -lt 2 ]; then
echo "usage: $0 [your github token] [REST expression]"
exit 1;
fi
GITHUB_TOKEN=$1
GITHUB_API_REST=$2
GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.v3+json"
temp=`basename $0`
TMPFILE=`mktemp /tmp/${temp}.XXXXXX` || exit 1
function rest_call {
curl -s $1 -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" >> $TMPFILE
}
# single page result-s (no pagination), have no Link: section, the grep result is empty
last_page=`curl -s -I "https://api.github.com${GITHUB_API_REST}" -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" | grep '^Link:' | sed -e 's/^Link:.*page=//g' -e 's/>.*$//g'`
# does this result use pagination?
if [ -z "$last_page" ]; then
# no - this result has only one page
rest_call "https://api.github.com${GITHUB_API_REST}"
else
# yes - this result is on multiple pages
for p in `seq 1 $last_page`; do
rest_call "https://api.github.com${GITHUB_API_REST}?page=$p"
done
fi
cat $TMPFILE
#!/bin/bash
set -e
if [ ${#@} -lt 2 ]; then
echo "usage: $0 [your github token] [REST expression]"
exit 1;
fi
GITHUB_TOKEN=$1
GITHUB_API_REST=$2
GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.v3+json"
temp=`basename $0`
TMPFILE=`mktemp /tmp/${temp}.XXXXXX` || exit 1
function rest_call {
curl -s $1 -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" | sed -e 's/^\[$//g' -e 's/^\]$/,/g' >> $TMPFILE
}
# single page result-s (no pagination), have no Link: section, the grep result is empty
last_page=`curl -s -I "https://api.github.com${GITHUB_API_REST}" -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" | grep '^Link:' | sed -e 's/^Link:.*page=//g' -e 's/>.*$//g'`
# does this result use pagination?
if [ -z "$last_page" ]; then
# no - this result has only one page
rest_call "https://api.github.com${GITHUB_API_REST}"
else
# yes - this result is on multiple pages
for p in `seq 1 $last_page`; do
rest_call "https://api.github.com${GITHUB_API_REST}?page=$p"
done
fi
line_counter=`wc -l $TMPFILE | sed -e 's/[/a-zA-Z].*$//g'`
#echo "TMPFILE:$TMPFILE"
#echo "line_counter:$line_counter"
echo "["
head -n $(($line_counter - 1)) $TMPFILE
echo "]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment