aboutsummaryrefslogtreecommitdiff
path: root/apod-bot.sh
blob: b79f261e6f466be35014652f74f45a5ac8ae2fb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /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 $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 -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

# Check image and page links exist
wget --spider $MEDIA_LINK
wget --spider $PAGE_LINK

# Publish notice
curl -u "$BOT_USERNAME:$BOT_PASSWORD" --data-urlencode "status=$NOTICE" \
     $SOCIAL_API_URL/statuses/update.json \
    | jq -r '"Published notice \"\(.text)\" on \(.created_at)"'