Skip to content
James Taylor edited this page Jul 7, 2020 · 7 revisions

Welcome to the eastleighmp wiki!

Get today's date

UNTIL=$(date +%F)

Get yesterday's date

SINCE=$(date --date='yesterday' +%F)

Search for day of tweets

twurl -j "/1.1/search/tweets.json?q=(from:pauljholmes)+since:${SINCE}+until:${UNTIL}&result_type=recent&tweet_mode=extended"

Process tweets

jq --raw-output '.statuses[].full_text'

Raw output causes a problem with tweets which contain new lines

Process tweets into separate files (in the right order for retweeting)

jq -c '.statuses[].full_text' | tac | split -l 1 - tweet

Find tweet files

find . -type f -name 'tweet*' -print0 | sort -z | xargs -0 -L 1 sh -c 'file="$1"; echo "$file"' _

Strip out mentions

sed 's/\B@\([a-zA-Z0-9_]\+\)/\1/g'

Strip out mentions with jq

jq --raw-output '.statuses[].full_text | sub("\\B@(?<user>[a-zA-Z0-9_]+)"; .user)'

Post a tweet

twurl -d 'status=Test tweet using the POST statuses/update endpoint' /1.1/statuses/update.json

Post an oversized tweet (fails!)

twurl -d 'status=Test tweet using the POST statuses/update endpoint. What happens it the tweet is too long????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????' /1.1/statuses/update.json

Keep tweets to max size (cut to max size - 1 and remove any potential part word/link)

cut -z -b 1-279 | sed "s/\S*$//"

Removing the last word on most tweets isn't great!

Keep tweets to max size with jq

jq --raw-output '.statuses[].full_text[:280]'

Test whether first overflow character is a space with jq

jq --raw-output '.statuses[].full_text[280:281] | test("\\s")'

Trim any trailing part word with jq

jq '.statuses[0].full_text | if (.[12:13] | test ("\\S")) then .[:12] | sub("\\S*$";"") else .[:12] end'

Post tweets from a file - does not handle new lines!

xargs -0 -d '\n' -L 1 -I{} twurl -d 'status={}' /1.1/statuses/update.json < tweets.txt

Post tweets from separate files (dry run!)

find . -type f -name 'tweet*' -print0 | sort -z | xargs -0 -L 1 sh -c 'file="$1"; tweet=$(cat "$file" | jq -r . | sed "s/\B@\([a-zA-Z0-9_]\+\)/\1/g" | cut -c 1-280); echo "Tweet: $tweet"' _

Post tweets from separate files (risk of shell expansion in tweet? will newlines work?)

find . -type f -name 'tweet*' -print0 | sort -z | xargs -0 -L 1 sh -c 'file="$1"; tweet=$(cat "$file" | jq -r . | sed "s/\B@\([a-zA-Z0-9_]\+\)/\1/g" | cut -c 1-280); twurl -d "status=$tweet" /1.1/statuses/update.json' _
Clone this wiki locally