From 742ff31bd832e796c2cf2322e4b576d71b6bd9a7 Mon Sep 17 00:00:00 2001 From: Eli Saado Date: Sun, 1 Apr 2018 21:48:33 +0200 Subject: [PATCH] (almost?) finish bash port --- download_daemon | 2 +- downloader.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100755 downloader.sh diff --git a/download_daemon b/download_daemon index af99835..a6a7cf9 100755 --- a/download_daemon +++ b/download_daemon @@ -11,7 +11,7 @@ while : ; do # If no, wait 300 secs and check again. [[ `< $HOME/.config/lock_screen.sta` -eq 0 ]] && sleep 300 && continue # Download the new image - $HOME/i3lock-delta/downloader.php + $HOME/i3lock-delta/downloader.sh if [[ $? -eq 0 ]]; then name_photographer="`< /tmp/name_photographer`" diff --git a/downloader.sh b/downloader.sh new file mode 100755 index 0000000..d2db181 --- /dev/null +++ b/downloader.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +# Get the api key +key=$(cat ~/i3lock-delta/api_key) +# Get the queries +queries=$(cat ~/.config/lock_preferences | xargs) +# Construct the base url +url="https://api.unsplash.com/photos/random?client_id=$key&orientation=landscape&featured=false" + +# Check if there are any queries +if [ -n "$queries" ]; then + url="$url&query=$(shuf -n 1 ~/.config/lock_preferences)" +fi + +# make API call and check if curl fails +if ! json=$(curl -s $url); then + exit 1 +fi + +# Get image location from json +image_url=$(echo $json | jq -r ".links.download_location") + +# Download image and check if curl fails +curl $image_url > ~/.rand_bg +if [[ $? -ne 0 ]] ; then exit 1; fi + +# Write author's name to file +user_name=$(echo $json | jq -r ".user.name") +echo "$user_name" > /tmp/name_photographer