Member-only story
Get a random github project with bash
While developing an AI-driven refactoring agent, we recently needed to automate the retrieval of random Python projects from GitHub. A key requirement was compatibility with both macOS and native Linux environments, using only built-in system commands. This constraint meant avoiding any additional tools, such as jq
for JSON processing or even Python itself, to maintain simplicity and portability.
Below is the code we quickly devised to meet these requirements. It achieves a level of randomness that was sufficient for our use case.
#!/bin/bash
# Generate a random number between 0 and 99
PAGE=$(( RANDOM % 100 ))
# GitHub API URL with the random page number
URL="https://api.github.com/search/repositories?q=language:Python&order=desc&sort=updated&per_page=10&page=${PAGE}"
# Fetch the JSON data
JSON=$(curl -s "$URL")
# Check if the curl command succeeded
if [ $? -ne 0 ] || [ -z "$JSON" ]; then
echo "Error: Unable to fetch data from GitHub API"
exit 1
fi
# Extract 'html_url' values excluding those inside "owner" objects
URLS=$(echo "$JSON" |
tr -d '\n' |
sed 's/},/},\n/g' |
grep '"html_url"' |
grep -v '"owner":' |
sed -n 's/.*"html_url": "\([^"]*\)".*/\1/p')
# Check if any URLs were extracted
if [ -z "$URLS" ]; then
echo "Error: No valid 'html_url' found in the JSON response"
exit 1
fi
# Convert the URLs into an array
IFS=$'\n' read -rd '' -a URLS_ARRAY <<< "$URLS"
# Select a random URL from the array
RANDOM_INDEX=$(( RANDOM % ${#URLS_ARRAY[@]} ))
HTML_URL=${URLS_ARRAY[$RANDOM_INDEX]}
# Output the selected URL
echo "Randomly selected repository URL: $HTML_URL"
About the author(s)
Johan Louwers is currently Chief Enterprise Architect within Oracle as well as the lead architect for NATO and a number of militaries. Johan has a strong and long background in the field of Enterprise Architecture and complex system engineering. Having worked with enterprises in a diverse set of industries as (enterprise) architect, CTO and technical and strategic business advisor Johan brings both deep technical knowledge to the table as well as strong business oriented expertise. In addition to this Johan is a tech addict who tends to enjoy supporting open source initiatives and actively coding as a hobby. Views expressed in this post are personnel and do not necessarily reflect the views of my current employer.