Last active
September 11, 2023 23:33
-
-
Save clragon/2673f401f8978d89e3fc56d78ef3cdfb to your computer and use it in GitHub Desktop.
auto release download bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Exit if no arguments are provided | |
| if [ "$#" -lt 1 ]; then | |
| echo "Usage: $0 <owner/repo> [command]" | |
| exit 1 | |
| fi | |
| # GitHub repository to download release from | |
| REPO=$1 | |
| # Optional command to run | |
| POST_UPDATE_COMMAND=$2 | |
| # Create a unique folder for each repository | |
| REPO_FOLDER=$(echo $REPO | tr '/' '_') | |
| mkdir -p "./${REPO_FOLDER}" | |
| # Location of the file to store the last update time | |
| LAST_UPDATE_FILE="./${REPO_FOLDER}/last_update" | |
| # Time between updates in seconds (24 hours) | |
| UPDATE_INTERVAL=86400 | |
| # Function to download the latest release | |
| update_repo() { | |
| echo "Downloading the latest release of $REPO..." | |
| # Change directory to repository folder | |
| cd "./${REPO_FOLDER}" > /dev/null | |
| # Wipe the folder clean | |
| rm -rf ./* | |
| # Use GitHub API to get the download URLs of all assets in the latest release | |
| LATEST_RELEASE_URLS=$(curl -s https://api.github.com/repos/$REPO/releases/latest | grep 'browser_download_url' | cut -d '"' -f 4) | |
| # Download all assets | |
| for url in $LATEST_RELEASE_URLS; do | |
| curl -LO $url | |
| done | |
| # Restore previous directory | |
| cd - > /dev/null | |
| echo "Latest release assets downloaded." | |
| } | |
| while true; do | |
| # Get the current time | |
| CURRENT_TIME=$(date +%s) | |
| # Check if the last update file exists | |
| if [ ! -f $LAST_UPDATE_FILE ]; then | |
| # File doesn't exist, download the latest release and create the file | |
| update_repo | |
| echo $CURRENT_TIME > $LAST_UPDATE_FILE | |
| else | |
| # Read the last update time from the file | |
| LAST_UPDATE_TIME=$(cat $LAST_UPDATE_FILE) | |
| # Calculate the elapsed time since the last update | |
| ELAPSED_TIME=$(($CURRENT_TIME - $LAST_UPDATE_TIME)) | |
| # Check if enough time has passed for a new update | |
| if [ $ELAPSED_TIME -ge $UPDATE_INTERVAL ]; then | |
| update_repo | |
| echo $CURRENT_TIME > $LAST_UPDATE_FILE | |
| else | |
| # Calculate the time remaining until the next update | |
| REMAINING_TIME=$(($UPDATE_INTERVAL - $ELAPSED_TIME)) | |
| echo "Waiting for $REMAINING_TIME seconds before the next update." | |
| fi | |
| fi | |
| # Run the post-update command | |
| if [ ! -z "$POST_UPDATE_COMMAND" ]; then | |
| echo "Running post-update command: $POST_UPDATE_COMMAND" | |
| eval "$POST_UPDATE_COMMAND" | |
| fi | |
| # Wait for the remaining time or the full interval if an update was performed | |
| sleep $UPDATE_INTERVAL | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment