#! /usr/bin/bash -efu

# 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

function send-request {
    # Send request to GNU Social server
    curl -sSfu "$bot_username:$bot_password" "$@"
}

# Check if the bot username/password are correct
send-request -o /dev/null $social_api_url/statuses/home_timeline.json

# 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/ *$//')
if [[ -z "$(echo "$apod_html" | pup 'iframe attr{src}')" ]]; then
    media_link=$apod_base_url/$(echo "$apod_html" | pup -p 'img attr{src}')
else
    media_link=$(echo "$apod_html" | pup 'iframe attr{src}' | sed 's/?.*//' \
		     | sed 's|player.vimeo.com/video|vimeo.com|' \
		     | sed 's|embed/|watch?v=|' \
		     | sed 's|^//|https://|')
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
send-request $social_api_url/statuses/home_timeline.json \
    | jq -e --arg notice "$notice" '(.[0] | .text) != $notice' > /dev/null \
    || (echo "Notice \"$notice\" already published. Aborting..." >&2 && exit 1)

# Check image and page links exist
wget --spider $media_link
wget --spider $page_link

# Publish notice
send-request --data-urlencode "status=$notice" \
	     $social_api_url/statuses/update.json \
    | jq -r '"Published notice \"\(.text)\" on \(.created_at)"'
