aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Isaac2016-12-22 22:00:49 +0530
committerArun Isaac2016-12-22 22:00:49 +0530
commit3f7312b75f576a7160cec40208348f3e03d1b611 (patch)
tree729dcb1500a998754bf459cbb3847d14a53025fe
downloadnasa-apod-gnu-social-bot-3f7312b75f576a7160cec40208348f3e03d1b611.tar.gz
nasa-apod-gnu-social-bot-3f7312b75f576a7160cec40208348f3e03d1b611.tar.lz
nasa-apod-gnu-social-bot-3f7312b75f576a7160cec40208348f3e03d1b611.zip
Initial commit
-rw-r--r--apod-bot.sh52
1 files changed, 52 insertions, 0 deletions
diff --git a/apod-bot.sh b/apod-bot.sh
new file mode 100644
index 0000000..85acc1b
--- /dev/null
+++ b/apod-bot.sh
@@ -0,0 +1,52 @@
+#! /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=http://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)"'