|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script submits Hugo site URLs to IndexNow and Bing APIs |
| 4 | + |
| 5 | +# Get environment variables (set by GitHub Actions) |
| 6 | +SITE_URL="${SITE_URL:-https://learn-software.com}" |
| 7 | +API_KEY="${API_KEY:-53f1811377874f608f161d768a9c0b78}" |
| 8 | +KEY_LOCATION="$SITE_URL/$API_KEY.txt" |
| 9 | + |
| 10 | +# IndexNow endpoints |
| 11 | +INDEXNOW_API="https://api.indexnow.org/indexnow" |
| 12 | +BING_API="https://www.bing.com/indexnow" |
| 13 | + |
| 14 | +# Function to submit URLs to IndexNow |
| 15 | +submit_to_indexnow() { |
| 16 | + local urls_json="$1" |
| 17 | + |
| 18 | + echo "=== IndexNow Submission ===" |
| 19 | + echo "Site: $SITE_URL" |
| 20 | + echo "Timestamp: $(date)" |
| 21 | + echo |
| 22 | + |
| 23 | + # Submit to IndexNow API |
| 24 | + echo "Submitting to IndexNow API..." |
| 25 | + response1=$(curl -s -w "HTTP_STATUS:%{http_code}" -X POST \ |
| 26 | + -H "Content-Type: application/json; charset=utf-8" \ |
| 27 | + -d "$urls_json" \ |
| 28 | + "$INDEXNOW_API") |
| 29 | + |
| 30 | + status1=$(echo "$response1" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2) |
| 31 | + echo "IndexNow API response: HTTP $status1" |
| 32 | + |
| 33 | + # Submit to Bing API |
| 34 | + echo "Submitting to Bing API..." |
| 35 | + response2=$(curl -s -w "HTTP_STATUS:%{http_code}" -X POST \ |
| 36 | + -H "Content-Type: application/json; charset=utf-8" \ |
| 37 | + -d "$urls_json" \ |
| 38 | + "$BING_API") |
| 39 | + |
| 40 | + status2=$(echo "$response2" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2) |
| 41 | + echo "Bing API response: HTTP $status2" |
| 42 | + |
| 43 | + # Check results |
| 44 | + if [[ "$status1" == "200" ]] || [[ "$status2" == "200" ]]; then |
| 45 | + echo "IndexNow submission successful" |
| 46 | + return 0 |
| 47 | + else |
| 48 | + echo "IndexNow submission failed" |
| 49 | + echo "IndexNow status: $status1" |
| 50 | + echo "Bing status: $status2" |
| 51 | + return 1 |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +# Main execution |
| 56 | +echo "=== Hugo IndexNow Automation ===" |
| 57 | + |
| 58 | +# Check if sitemap exists |
| 59 | +if [ ! -f "public/sitemap.xml" ]; then |
| 60 | + echo "Error: sitemap.xml not found in public folder" |
| 61 | + echo "Make sure Hugo is configured to generate a sitemap" |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +# Extract URLs from sitemap |
| 66 | +echo "Extracting URLs from sitemap..." |
| 67 | +urls=$(grep -oP '(?<=<loc>)[^<]+' public/sitemap.xml | grep -v "\.xml$" | head -10000) |
| 68 | + |
| 69 | +if [ -z "$urls" ]; then |
| 70 | + echo "No URLs found in sitemap" |
| 71 | + exit 1 |
| 72 | +fi |
| 73 | + |
| 74 | +url_count=$(echo "$urls" | wc -l) |
| 75 | +echo "Found $url_count URLs to submit" |
| 76 | + |
| 77 | +# Convert URLs to JSON array format |
| 78 | +url_array=$(echo "$urls" | sed 's/.*/"&"/' | paste -sd ',' -) |
| 79 | + |
| 80 | +# Create JSON payload |
| 81 | +urls_json=$(cat << EOF |
| 82 | +{ |
| 83 | + "host": "$SITE_URL", |
| 84 | + "key": "$API_KEY", |
| 85 | + "keyLocation": "$KEY_LOCATION", |
| 86 | + "urlList": [$url_array] |
| 87 | +} |
| 88 | +EOF |
| 89 | +) |
| 90 | + |
| 91 | +# Submit URLs |
| 92 | +submit_to_indexnow "$urls_json" |
0 commit comments