compare.sh
Raw
#!/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
1 | #!/bin/bash |
2 | |
3 | # Default URL of the Twtxt feed |
4 | DEFAULT_FEED_URL="https://twtxt.net/user/prologic/twtxt.txt" |
5 | |
6 | # If a URL is passed as the first argument, use it; otherwise, use the default |
7 | FEED_URL="${1:-$DEFAULT_FEED_URL}" |
8 | |
9 | OUTPUT_FILE="output.txt" |
10 | MODIFIED_FILE="modified_output.txt" |
11 | |
12 | # Fetch the last 100 lines of the feed and extract Twt Subjects |
13 | curl -s "$FEED_URL" | tail -n 100 > "$OUTPUT_FILE" |
14 | |
15 | # Loop through each line, find the Twt Subject, and replace with twter.url + created timestamp |
16 | while IFS= read -r line; do |
17 | # Extract the Twt Subject using grep and regex |
18 | SUBJECT=$(echo "$line" | grep -oP '\(#\K\w+') |
19 | |
20 | if [[ -n "$SUBJECT" ]]; then |
21 | # Curl the JSON data for the corresponding Twt Subject |
22 | JSON_DATA=$(curl -s -H "Accept: application/json" "https://twtxt.net/twt/$SUBJECT") |
23 | |
24 | # Check if the JSON data is valid |
25 | if echo "$JSON_DATA" | jq empty 2>/dev/null; then |
26 | # Extract the twter URL and created timestamp from the JSON response |
27 | TWTER_URL=$(echo "$JSON_DATA" | jq -r '.twter.uri // empty') |
28 | CREATED_TIMESTAMP=$(echo "$JSON_DATA" | jq -r '.created // empty') |
29 | |
30 | # Only process the line if both twter.url and created are valid |
31 | if [[ -n "$TWTER_URL" && -n "$CREATED_TIMESTAMP" ]]; then |
32 | # Build the replacement string |
33 | REPLACEMENT="($TWTER_URL $CREATED_TIMESTAMP)" |
34 | |
35 | # Replace the Twt Subject in the line manually using Bash string manipulation |
36 | MODIFIED_LINE="${line//(#$SUBJECT)/$REPLACEMENT}" |
37 | |
38 | # Append the modified line to the modified output file |
39 | echo "$MODIFIED_LINE" >> "$MODIFIED_FILE" |
40 | else |
41 | # If no valid data, copy the line as is |
42 | echo "$line" >> "$MODIFIED_FILE" |
43 | fi |
44 | else |
45 | # If invalid JSON, copy the line as is |
46 | echo "$line" >> "$MODIFIED_FILE" |
47 | fi |
48 | else |
49 | # If no subject found, copy the line as is |
50 | echo "$line" >> "$MODIFIED_FILE" |
51 | fi |
52 | done < "$OUTPUT_FILE" |
53 | |
54 | # Compare the file sizes |
55 | ORIGINAL_SIZE=$(stat --format="%s" "$OUTPUT_FILE") |
56 | MODIFIED_SIZE=$(stat --format="%s" "$MODIFIED_FILE") |
57 | |
58 | # Calculate percentage increase |
59 | SIZE_DIFF=$((MODIFIED_SIZE - ORIGINAL_SIZE)) |
60 | PERCENT_INCREASE=$(awk "BEGIN {printf \"%.2f\", ($SIZE_DIFF / $ORIGINAL_SIZE) * 100}") |
61 | |
62 | # Display file sizes and percentage increase |
63 | echo "Original file size: $ORIGINAL_SIZE bytes" |
64 | echo "Modified file size: $MODIFIED_SIZE bytes" |
65 | echo "Percentage increase in file size: $PERCENT_INCREASE%" |
66 | |
67 | # Extract and display the last lines of both files |
68 | echo -e "\nLast line of the original file:" |
69 | ORIGINAL_LAST_LINE=$(tail -n 1 "$OUTPUT_FILE") |
70 | echo "$ORIGINAL_LAST_LINE" |
71 | |
72 | echo -e "\nLast line of the modified file:" |
73 | MODIFIED_LAST_LINE=$(tail -n 1 "$MODIFIED_FILE") |
74 | echo "$MODIFIED_LAST_LINE" |
75 | |
76 | # Use diff to show a colorized diff of the last lines |
77 | echo -e "\nColorized diff of the last line:" |
78 | diff --color=always <(echo "$ORIGINAL_LAST_LINE") <(echo "$MODIFIED_LAST_LINE") || true |
79 |