Skip to content

Instantly share code, notes, and snippets.

@dbeley
Last active January 31, 2021 09:15
Show Gist options
  • Save dbeley/3708994e0f3a4308bf06d050c22c377f to your computer and use it in GitHub Desktop.
Save dbeley/3708994e0f3a4308bf06d050c22c377f to your computer and use it in GitHub Desktop.
Archive all github starred repos of an user.
#!/usr/bin/env bash
set -eEu -o pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd -P)"
usage() { printf "%s" "\
Usage: ./archive_github_user_starred.sh [-h] USER
"; exit 0;
}
if [ "$1" == "-h" ]; then
usage
fi
USER=$1
USER_DIR="$DIR/starred/$USER"
mkdir -p "$USER_DIR"
[ -f "$USER_DIR/$USER.txt" ] && printf "##### $USER_DIR/$USER.txt file detected.\nDelete it if you want to update the starred list.\n"
if [ ! -f "$USER_DIR/$USER.txt" ]; then
printf "##### Extracting starred repos for $USER.\n"
PAGE_NUMBER=`curl -sI "https://api.github.com/users/"$USER"/starred" | awk "/^link/" | cut -d? -f3`
for PAGE in `seq 1 ${PAGE_NUMBER//[!0-9]/}`; do
printf "##### Extracting starred repos from page $PAGE\n"
URL="https://api.github.com/users/$USER/starred?page=$PAGE"
curl -s "$URL" | jq -r '.[]|.html_url' >> "$USER_DIR/$USER.txt"
done
fi
for URL in `cat "$USER_DIR/$USER.txt"`; do
REPO=`basename -s ".git" "$URL"`
REPO_USER=`basename -$(dirname $URL)`
FOLDER="$USER_DIR/$REPO_USER/$REPO"
if [ ! -d "$FOLDER" ] ; then
printf "##### Cloning $URL in $FOLDER.\n"
mkdir -p "$FOLDER"
git clone $URL "$FOLDER"
else
printf "##### Updating $URL in $FOLDER.\n"
cd "$FOLDER"
git pull $URL
cd "$DIR"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment