Skip to content

Instantly share code, notes, and snippets.

@royrscb
Created April 16, 2019 23:51
Show Gist options
  • Save royrscb/c023e1512e63bd31d2aa08ca9c638f2a to your computer and use it in GitHub Desktop.
Save royrscb/c023e1512e63bd31d2aa08ca9c638f2a to your computer and use it in GitHub Desktop.
Get daily pictures from nasa and national geographic
#!/bin/bash
###########################
#Author: Roy Ros Cobo ###
###########################
carpeta="/home/$USER/Pictures/dailies"
###########################################
ping -c 1 -w 1 8.8.8.8 > /dev/null; if [ $? -ne 0 ]; then echo 'No internet connection!'; sleep 1; exit; fi
mkdir -p $carpeta
mkdir -p "/home/$USER/tmp"
cd /home/$USER/tmp
function get_n_ask-img(){
local title=$1
local information=$2
local url=$3
local ext=$4
if [ -z "$ext" ]; then ext=$(echo $url | rev | cut -f1 -d. | rev); fi
######## GET #################################################################
wget --output-document="$title.$ext" $url
# ######## ASK #############################################################
clear; echo -e "\e[1m\e[34m$title\e[0m\n\n$information\n\n"
eog $(pwd)/"$title.$ext" & eog_pid=$!
read -n 1 -r -p "Vols guardar \"$title\" (s/n,w)? " resp; echo
kill $eog_pid 2> /dev/null; wait $eog_pid 2> /dev/null
if [ "$resp" == "n" ]; then rm "$title.$ext"
else
mv "$title.$ext" "$carpeta"
if [ "$resp" == "w" ]; then gsettings set org.gnome.desktop.background picture-uri file://"$carpeta/$title.$ext"; fi
fi
clear
}
function natGeo(){
######## HTML ########################################################################
local urlweb='https://www.nationalgeographic.com/photography/photo-of-the-day'
local html=nat_geo.html
wget --no-check-certificate --output-document=$html $urlweb
######## NOM && URL ##################################################################
local title=$(grep -m 1 'property="og:title"' $html | cut -d'"' -f4 | cut -d'"' -f4)
local explanation=$(sed -n "$(($(grep -n lead-component nat_geo.html | cut -f1 -d:) + 2))p" $html \
| jq ".dek.dek.text" | cut -d'>' -f2 | cut -d'<' -f1 | head -c -57).
if [ $(echo "$explanation" | tail -c 3) == ',.' ]; then explanation=$(echo "$explanation" | head -c -3).; fi
local url=$(grep -m 1 'yourshot' $html | cut -d= -f3 | cut -c 2- | head -c -5)
rm $html
####### GET N ASK #################################################################
get_n_ask-img "$title" "$explanation" "$url" "jpeg"
}
function nasaAPOD(){
local nasaKey='HERE_GOES_YOUR_NASA_API_KEY'
######## JSON #################################################################
local json=$(curl 'https://api.nasa.gov/planetary/apod?api_key='$nasaKey)
if [ $(echo $json | jq -r '.media_type') == "video" ]; then rm $html; clear; echo 'Es un video...'; sleep 3; exit; fi
######## URL && TITLE && EXPLICACIO ############################################
local title=$(echo $json | jq -r '.title')
local explanation=$(echo $json | jq -r '.explanation')
local url=$(echo $json | jq -r '.hdurl')
######## GET N ASK #################################################################
get_n_ask-img "$title" "$explanation" "$url"
}
######## EXECUTION ##########
natGeo
nasaAPOD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment