prologic revised this gist . Go to revision
1 file changed, 78 insertions
compare.sh(file created)
@@ -0,0 +1,78 @@ | |||
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 |
Newer
Older