Last active 1727067438

Compare Twtxt Location based vs. Content based addressing

compare.sh Raw
1#!/bin/bash
2
3# Default URL of the Twtxt feed
4DEFAULT_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
7FEED_URL="${1:-$DEFAULT_FEED_URL}"
8
9OUTPUT_FILE="output.txt"
10MODIFIED_FILE="modified_output.txt"
11
12# Fetch the last 100 lines of the feed and extract Twt Subjects
13curl -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
16while 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
52done < "$OUTPUT_FILE"
53
54# Compare the file sizes
55ORIGINAL_SIZE=$(stat --format="%s" "$OUTPUT_FILE")
56MODIFIED_SIZE=$(stat --format="%s" "$MODIFIED_FILE")
57
58# Calculate percentage increase
59SIZE_DIFF=$((MODIFIED_SIZE - ORIGINAL_SIZE))
60PERCENT_INCREASE=$(awk "BEGIN {printf \"%.2f\", ($SIZE_DIFF / $ORIGINAL_SIZE) * 100}")
61
62# Display file sizes and percentage increase
63echo "Original file size: $ORIGINAL_SIZE bytes"
64echo "Modified file size: $MODIFIED_SIZE bytes"
65echo "Percentage increase in file size: $PERCENT_INCREASE%"
66
67# Extract and display the last lines of both files
68echo -e "\nLast line of the original file:"
69ORIGINAL_LAST_LINE=$(tail -n 1 "$OUTPUT_FILE")
70echo "$ORIGINAL_LAST_LINE"
71
72echo -e "\nLast line of the modified file:"
73MODIFIED_LAST_LINE=$(tail -n 1 "$MODIFIED_FILE")
74echo "$MODIFIED_LAST_LINE"
75
76# Use diff to show a colorized diff of the last lines
77echo -e "\nColorized diff of the last line:"
78diff --color=always <(echo "$ORIGINAL_LAST_LINE") <(echo "$MODIFIED_LAST_LINE") || true
79

Powered by Opengist Load: 8ms