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'…