#!/bin/bash # This script converts Base64url-encoded Twitter filenames into human readable dates and appends them as prefix # Written by yours truly # # Hot singles in your area at https://endchan.net/agatha2/ # # Inconsistent reference materials: # https://en.wikipedia.org/wiki/Snowflake_ID # https://github.com/twitter-archive/snowflake/blob/snowflake-2010/src/main/scala/com/twitter/service/snowflake/IdWorker.scala # # Requirements: GNU/bash I guess # # Usage # Run script by sourcing, asterisk picks up every file in current working directory: # $ source /home/user/path/twitter-filename-to-timestamp.sh * # # Or select files, drag-and-drop into terminal: # $ source /home/user/path/twitter-filename-to-timestamp.sh '/home/user/path1/E2lkq7dXwAARMsY.jpg' '/home/user/path2/E6B2fedXMAIm80Z.jpg' # # Test file: # https://twitter.com/Wikipedia/status/1707751173812269539 # F7MmpC0XcAEggXE.jpg # Post timestamp: [2023-09-29 13:36 UTC] # # Create separate subdir to put final files in mkdir "timestamped" # # Consume multiple file paths as array for FILE_PATH in "${@}" ;do # # Get raw filename FILENAME="$(basename "$FILE_PATH")"; # echo "Filename: $FILENAME" # > F7MmpC0XcAEggXE.jpg # # Strip file extension and dot SNOWFLAKE="${FILENAME%.*}"; # echo "Snowflake ID: $SNOWFLAKE" # > F7MmpC0XcAEggXE # # Decode from Base64 to Base2/binary number string, '=' padding to avoid errors BIN_STRING="$(echo "$SNOWFLAKE=" | basenc -d --base64url | basenc -w0 --base2msbf)"; # echo "Binary string: $BIN_STRING" # > 0001011110110011001001101010010000101101000101110111000000000001001000001000000101110001 # # Extract first 42 characters/bits (7x6) BIN_TSTAMP="${BIN_STRING:0:42}"; # echo "Binary timestamp: $BIN_TSTAMP" # > 000101111011001100100110101001000010110100 # # Convert binary (Base2 or Radix 2) number to decimal (Base10 or Radix 10) using bash arithmetic expansion DEC_TSTAMP="$(echo "obase=10; ibase=2; $BIN_TSTAMP" | bc)"; # echo "Decimal timestamp: $DEC_TSTAMP" # > 407159607476 # # Get post date by adding Twitter epoch [1288834974657] and removing milliseconds POST_DATE="$(date --utc -d @$((("$DEC_TSTAMP"+1288834974657)/1000)) +'[%Y-%m-%d %H:%M:%S UTC]')"; # echo "$POST_DATE"; # > [2023-09-29 13:36:22 UTC] # NEW_FILENAME="$POST_DATE $FILENAME"; echo "$NEW_FILENAME"; # > [2023-09-29 13:36:22 UTC] F7MmpC0XcAEggXE.jpg # # Copy new files into subdir cp "$FILE_PATH" "timestamped/$NEW_FILENAME"; done;