#! /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 this project at # https://git.systemreboot.net/nasa-apod-gnu-social-bot/about/ # # To run this script, you will need jq, the command line JSON # processor and pup, the command line HTML processor. # Settings SOCIAL_API_URL=https://social.systemreboot.net/api BOT_USERNAME=apod BOT_PASSWORD=secret-password-here APOD_BASE_URL=https://apod.nasa.gov/apod # Download HTML page, and scrape required information APOD_HTML=$(curl -sS $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 # Construct notice NOTICE="$TITLE $PAGE_LINK $MEDIA_LINK" # Ensure we are not publishing a duplicate notice PREVIOUS_NOTICE=$(curl -sSu "$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 # Check image and page links exist wget --spider $MEDIA_LINK wget --spider $PAGE_LINK # Publish notice curl -sSu "$BOT_USERNAME:$BOT_PASSWORD" --data-urlencode "status=$NOTICE" \ $SOCIAL_API_URL/statuses/update.json \ | jq -r '"Published notice \"\(.text)\" on \(.created_at)"'