!/bin/sh -e

convert twtxt.txt to atom file

09/05 : make sha256 usable on linux. Thanks Frederic Galusik.

some help can't hurt

help()

{

printf "usage:\n"

printf "\t%s -n <nick> -u <feed url> -t <feed title> -x <twtxt.txt url> twtxt.txt\n" "$0"

exit 1

}

make sure variables to check later if user set them

nick=""

url=""

title=""

twtxturl=""

use getopt to get options

while getopts 'n:u:t:x:h' c

do

case $c in

	n) nick="${OPTARG}" ;;

	u) url="${OPTARG}" ;;

	t) title="${OPTARG}" ;;

	x) twtxturl="${OPTARG}" ;;

	h) help ;;

esac

done

skip options in argumnent index so $1 is twtxt.txt

shift $((OPTIND - 1))

abort if user forgot a parameter

test -z "$nick" && help

test -z "$url" && help

test -z "$title" && help

header template of the atom

HEADER="

${url}

${nick}

"

get the last entry to set last time atom was updated

LAST="$(awk 'END {print $1}' $1)"

Let's output the beginning

printf '%s' "$HEADER"

printf '%s\n' "$LAST"

grep remove comments and empty lines and loop over twtxt.txt lines

grep -Ev '^#|^$' "${1}" | while read -r TS MSG; do

# ID must be unique, use sha256 on it

if [[ "$OSTYPE" == "linux-gnu" ]]; then

	ID="$(printf '%s' "$1 $TS $MSG" | sha256sum | cut -f 1 -d " " )"

else

	ID="$(printf '%s' "$1 $TS $MSG" | sha256)"

fi

# Can't have ]]> in CDATA

ESCAPED="$(printf "%s" "${MSG}" | awk '{gsub("]]>", "]]]]><![CDATA[>")}1')"

# output the entry

printf '<entry>\n'

printf '<id>urn:hash::sha256:%s</id>\n' "${ID}"

printf '<title type="text"><![CDATA[%s]]></title>\n' "${ESCAPED}"

printf '<updated>%s</updated>\n' "${TS}"

printf '<content type="text"><![CDATA[%s]]></content>\n' "${ESCAPED}"

printf '<link rel="alternate" type="text/plain" href="%s" />\n' "${twtxturl}"

printf '</entry>\n'

done

printf '%s' ''

exit 0

Proxy Information
Original URL
gemini://si3t.ch/code/twtxt2atom/twtxt2atom.sh
Status Code
Success (20)
Meta
application/octet-stream
Capsule Response Time
556.696471 milliseconds
Gemini-to-HTML Time
1.349071 milliseconds

This content has been proxied by September (ba2dc).