Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Created September 4, 2024 03:19
Show Gist options
  • Select an option

  • Save wilmoore/c46e4ea94db8d0dff876fe2088686028 to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/c46e4ea94db8d0dff876fe2088686028 to your computer and use it in GitHub Desktop.
Software Engineering :: Source Control :: VCS :: Git :: Managed Hosting Platform :: GitHub :: Gist :: API :: Scripting :: gist-list.sh

Software Engineering :: Source Control :: VCS :: Git :: Managed Hosting Platform :: GitHub :: Gist :: API :: Scripting :: gist-list.sh

⪼ Made with 💜 by Polyglot.

gist-list.sh

#!/bin/bash

# Load environment variables from .env file
export $(grep -v '^#' .env | xargs)

# Initial URL
url="https://api.github.com/gists?per_page=100"

# Loop through pages
while [ "$url" != "" ]; do
  # Make the curl request and capture headers and body
  response=$(curl -s -D - --request GET \
    --url "$url" \
    --header 'Accept: application/vnd.github+json' \
    --header "Authorization: Bearer ${GIST_API_KEY}" \
    --header 'X-GitHub-Api-Version: 2022-11-28')
  
  # Extract the body of the response
  body=$(echo "$response" | sed -n '/^\r$/,$p' | sed '1d')
  
  # Process the body (e.g., save it to a file, print it, etc.)
  echo "$body"

  # Extract the Link header for pagination
  link_header=$(echo "$response" | grep -i "^Link:")

  # Parse the next page URL from the Link header
  url=$(echo "$link_header" | sed -n 's/.*<\(.*\)>; rel="next".*/\1/p')

  # If no next URL is found, exit the loop
  if [ -z "$url" ]; then
    url=""
  fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment