#!/bin/bash # Default URL of the Twtxt feed DEFAULT_FEED_URL="https://twtxt.net/user/prologic/twtxt.txt" # If a URL is passed as the first argument, use it; otherwise, use the default FEED_URL="${1:-$DEFAULT_FEED_URL}" OUTPUT_FILE="output.txt" MODIFIED_FILE="modified_output.txt" # Fetch the last 100 lines of the feed and extract Twt Subjects curl -s "$FEED_URL" | tail -n 100 > "$OUTPUT_FILE" # Loop through each line, find the Twt Subject, and replace with twter.url + created timestamp while IFS= read -r line; do # Extract the Twt Subject using grep and regex SUBJECT=$(echo "$line" | grep -oP '\(#\K\w+') if [[ -n "$SUBJECT" ]]; then # Curl the JSON data for the corresponding Twt Subject JSON_DATA=$(curl -s -H "Accept: application/json" "https://twtxt.net/twt/$SUBJECT") # Check if the JSON data is valid if echo "$JSON_DATA" | jq empty 2>/dev/null; then # Extract the twter URL and created timestamp from the JSON response TWTER_URL=$(echo "$JSON_DATA" | jq -r '.twter.uri // empty') CREATED_TIMESTAMP=$(echo "$JSON_DATA" | jq -r '.created // empty') # Only process the line if both twter.url and created are valid if [[ -n "$TWTER_URL" && -n "$CREATED_TIMESTAMP" ]]; then # Build the replacement string REPLACEMENT="($TWTER_URL $CREATED_TIMESTAMP)" # Replace the Twt Subject in the line manually using Bash string manipulation MODIFIED_LINE="${line//(#$SUBJECT)/$REPLACEMENT}" # Append the modified line to the modified output file echo "$MODIFIED_LINE" >> "$MODIFIED_FILE" else # If no valid data, copy the line as is echo "$line" >> "$MODIFIED_FILE" fi else # If invalid JSON, copy the line as is echo "$line" >> "$MODIFIED_FILE" fi else # If no subject found, copy the line as is echo "$line" >> "$MODIFIED_FILE" fi done < "$OUTPUT_FILE" # Compare the file sizes ORIGINAL_SIZE=$(stat --format="%s" "$OUTPUT_FILE") MODIFIED_SIZE=$(stat --format="%s" "$MODIFIED_FILE") # Calculate percentage increase SIZE_DIFF=$((MODIFIED_SIZE - ORIGINAL_SIZE)) PERCENT_INCREASE=$(awk "BEGIN {printf \"%.2f\", ($SIZE_DIFF / $ORIGINAL_SIZE) * 100}") # Display file sizes and percentage increase echo "Original file size: $ORIGINAL_SIZE bytes" echo "Modified file size: $MODIFIED_SIZE bytes" echo "Percentage increase in file size: $PERCENT_INCREASE%" # Extract and display the last lines of both files echo -e "\nLast line of the original file:" ORIGINAL_LAST_LINE=$(tail -n 1 "$OUTPUT_FILE") echo "$ORIGINAL_LAST_LINE" echo -e "\nLast line of the modified file:" MODIFIED_LAST_LINE=$(tail -n 1 "$MODIFIED_FILE") echo "$MODIFIED_LAST_LINE" # Use diff to show a colorized diff of the last lines echo -e "\nColorized diff of the last line:" diff --color=always <(echo "$ORIGINAL_LAST_LINE") <(echo "$MODIFIED_LAST_LINE") || true