#! /usr/bin/bash -e # This work is in the public domain. # # It was completed by Arun I in 2016. # # Though you are not legally obliged to do so, I would appreciate it # if you credit me for this work by not removing this notice, and # hopefully linking to my blog post at # https://systemreboot.net/post/nasa-astronomy-picture-of-the-day-bot-for-gnu-social # # To run this script, you will need jq, the command line JSON # processor and pup, the command line HTML processor. SOCIAL_API_URL=https://social.systemreboot.net/api BOT_USERNAME=apod BOT_PASSWORD=secret-password-here APOD_BASE_URL=https://apod.nasa.gov/apod APOD_HTML=$(curl $APOD_BASE_URL/astropix.html) TITLE=$(echo "$APOD_HTML" | pup 'center:nth-child(2) > b:nth-child(1) text{}' \ | sed -e 's/^ *//' -e 's/ *$//') IMAGE_LINK=$APOD_BASE_URL/$(echo "$APOD_HTML" | pup -p 'img attr{src}') YOUTUBE_VIDEO_ID=$(echo "$APOD_HTML" | pup 'iframe attr{src}' | awk -F/ '{print $5}' | awk -F? '{print $1}') YOUTUBE_LINK="https://www.youtube.com/watch?v=$YOUTUBE_VIDEO_ID" if [[ -z "$YOUTUBE_VIDEO_ID" ]] then MEDIA_LINK=$IMAGE_LINK else MEDIA_LINK=$YOUTUBE_LINK fi DATE=$(echo "$APOD_HTML" | pup ':contains("Discuss") attr{href}' | awk -F= '{print $2}') PAGE_LINK=$APOD_BASE_URL/ap$DATE.html NOTICE="$TITLE $PAGE_LINK $MEDIA_LINK" PREVIOUS_NOTICE=$(curl -u "$BOT_USERNAME:$BOT_PASSWORD" $SOCIAL_API_URL/statuses/home_timeline.json \ | jq -r '.[0] | .text') if [[ "$NOTICE" = "$PREVIOUS_NOTICE" ]] then echo "Notice \"$NOTICE\" already published. Aborting..." >&2 exit 1 fi wget --spider $MEDIA_LINK wget --spider $PAGE_LINK curl -u "$BOT_USERNAME:$BOT_PASSWORD" --data-urlencode "status=$NOTICE" \ $SOCIAL_API_URL/statuses/update.json \ | jq -r '"Published notice \"\(.text)\" on \(.created_at)"'