Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function with NItrogen #1

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
20 changes: 14 additions & 6 deletions README.org → README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
#+TITLE:Reddit wallpaper fetcher
⚠️No longer works due to Reddit's API changes⚠️

* What is this?
# TITLE:Reddit wallpaper fetcher (Redwall)<br/>

# What is this?<br/>
This is a simple shell script to pull the images from a set of subreddits, intended for use as a dynamic wallpaper.
It is written in bash and uses curl, jq and imagemagick for its processing.
Currently it downloads the images, names them and places them in a folder which should set the wallpaper.
It can optionally add the title of the image to the image.
It uses a simple, bash readable configuration file.

* How to use
# How to use<br/>
Copy the default configuration file to ~/.config/reddit-wallpaper-fetcher.conf and alter the variables as need be.
The list of subreddits is a bash list, so every entry needs to be in quotes and seperated by a space or newline.

* TODOs
- Interface with the window manager to set the wallpaper
# - Setup image scaling/resizing
# Dependencies<br/>
•<a href="https://github.com/l3ib/nitrogen" >nitrogen</a>


# TODOs<br/>
•Add support for all DE/WMs<br/>
•Add support for multiple mmonitors<br/>
•Add multiple subreddit support<br/>
•Randomise image out of selection
193 changes: 129 additions & 64 deletions reddit-wallpaper-fetcher
Original file line number Diff line number Diff line change
@@ -1,81 +1,146 @@
#!/bin/bash
set -x

TESTING=0
ext=""

#Default variables
SUBREDDITS=("Wallpapers")
#to change these access ~/.config/reddit-wallpaper-fetcher.conf
SUBREDDITS=("Wallpapers" "redwall")
SORT="new"
USER=$(whoami)
SIZE=1920x1080
WRITE_TITLE=1
TITLE_COLOR="white"
TITLE_POSITION="Southeast"
TITLE_SIZE=30

#Set directories for testing
if [ $TESTING != 1 ]; then
CONFDIR="$HOME/.config"
WALLDIR="$HOME/.wallpaper"
else
CONFDIR="$(dirname $(realpath "$0"))"
WALLDIR="$(dirname $(realpath "$0"))"
fi
TITLE_SIZE=20
AMOUNT_OF_RESULTS_TO_SORT=5

[ ! -d "$WALLDIR" ] && mkdir "$WALLDIR"
source "$CONFDIR"/reddit-wallpaper-fetcher.conf &>/dev/null
if [ "$1" = "--run" ] || [ "$1" = "-r" ]; then
if [ "$2" != "" ]; then
#Set directories for testing
if [ $TESTING != 1 ]; then
CONFDIR="$HOME/.config"
WALLDIR="$HOME/.wallpaper"
else
CONFDIR="$(dirname $(realpath "$0"))"
WALLDIR="$(dirname $(realpath "$0"))"
fi

#Loud output for testing a changed config
[ -z "$1" ] && LOUD="-s"
[ ! -d "$WALLDIR" ] && mkdir "$WALLDIR"
source "$CONFDIR"/reddit-wallpaper-fetcher.conf &>/dev/null
echo set directories for testing # info output

#Initialise time from last fetch
LATEST_TIME=0
if [ -f "$WALLDIR"/json ]; then
LATEST=$(cat "$WALLDIR"/json)
LATEST_TIME=$(echo "$LATEST" | jq -r ".data.children[-1].data.created_utc")
fi
if [ "$LATEST_TIME" == "null" ]; then
LATEST_TIME=0
fi
#Load output for testing a changed config
[ -z "$1" ] && LOUD="-s"
echo loaded output for testing and changed config

#Try subreddits for new images
for i in "${SUBREDDITS[@]}"; do
temp=$(curl "$LOUD" --user-agent "$USER" "https://www.reddit.com/r/$i.json?sort=$SORT&limit=1")
time=$(echo "$temp" | jq -r ".data.children[-1].data.created_utc")
if [ "$time" == "null" ]; then
continue
fi
if [ $(awk -v n1="$time" -v n2="$LATEST_TIME" 'BEGIN {printf (n1>n2?"1":"")}') ]; then
LATEST="$temp"
LATEST_TIME="$time"
fi
done

#No Update to do
if [ -f "$WALLDIR"/json ] && [ "$(cat "$WALLDIR"/json)" == "$LATEST" ]; then
exit
fi
#Initialise time from last fetch
LATEST_TIME=0
if [ -f "$WALLDIR"/json ]; then
SELECTED_IMAGE=$(cat "$WALLDIR"/json)
LATEST_TIME=$(echo "$SELECTED_IMAGE" | jq -r ".data.children[-1].data.created_utc")
fi
if [ "$LATEST_TIME" == "null" ]; then
LATEST_TIME=0
fi
echo initialized time from last fetch

#Download image
url=$(echo "$LATEST" | jq -r ".data.children[-1].data.url" | sed 's/"//g')
if [ -n "$(echo $url | grep gallery)" ]; then
media_id=$(echo "$LATEST" | jq -r ".data.children[-1].data.gallery_data.items[0].media_id" | sed 's/"//g')
url=$(echo "$LATEST" | jq -r ".data.children[-1].data.media_metadata.$media_id.p[0].u" | sed 's/"//g;s/\?.*//g;s/preview/i/g')
fi
ext="$(echo "$url" | rev | cut -d'.' -f1 | rev)"
curl "$url" "$LOUD" --output "$WALLDIR/temp.$ext"

#Save image information
echo "$LATEST" > "$WALLDIR"/json
title=$(echo "$LATEST" | jq -r ".data.children[-1].data.title")
echo "$title" > "$WALLDIR"/data.txt
echo $LATEST | jq -r ".data.children[-1].data.subreddit" >> "$WALLDIR"/data.txt
echo "$url" >> "$WALLDIR"/data.txt
echo https://reddit.com$(echo "$LATEST" | jq -r ".data.children[-1].data.permalink" | sed 's/"//g') >> "$WALLDIR"/data.txt

#Add image title
convert "$WALLDIR/temp.$ext" -resize "$SIZE^" -gravity center -crop "$SIZE+0+0" -resize "$SIZE!" "$WALLDIR/wallpaper.jpg"
rm "$WALLDIR/temp.$ext"

if [ $WRITE_TITLE == 1 ]; then
convert "$WALLDIR/wallpaper.jpg" -fill "$TITLE_COLOR" -gravity "$TITLE_POSITION" -pointsize "$TITLE_SIZE" -annotate 0 "$title" "$WALLDIR/wallpaper.jpg"
all_wallpapers=""
#Try subreddits for new images
for i in "${SUBREDDITS[@]}"; do
echo $i
temp=$(curl "$LOUD" --user-agent "$USER" "https://www.reddit.com/r/$i.json?sort=$SORT&limit=$AMOUNT_OF_RESULTS_TO_SORT" | jq -r '.data.children[].data.url')
echo
echo https://www.reddit.com/r/$i.json?sort=$SORT&limit=$AMOUNT_OF_RESULTS_TO_SORT
echo $temp
#time=$(echo "$temp" | jq -r ".data.children[-1].data.created_utc")
all_wallpapers+=$(echo " $temp")
#all_wallpapers=$(echo "$temp" | grep -Eo ' ')
echo $all_wallpapers

#if [ "$time" == "null" ]; then
# continue
#fi
#if [ $(awk -v n1="$time" -v n2="$LATEST_TIME" 'BEGIN {printf (n1>n2?"1":"")}') ]; then
# LATEST="$temp"
# LATEST_TIME="$time"
# fi
done

corrupted_image=true
repetition=0
while [ "$corrupted_image" == true ] && [ "$repetition" -lt 20 ];
do
SELECTED_IMAGE=$(echo "$all_wallpapers" | shuf -n 1)
echo tried subreddits for new images
echo ----------
echo $SELECTED_IMAGE
echo ----------

url=$SELECTED_IMAGE
echo url: $url
curl "$url" "$LOUD" --output "$WALLDIR/temp.$ext"
if [ ! -d "$WALLDIR" ]; then
mkdir -p "$WALLDIR"
fi
filename=temp.${url: -3}
echo saved temp to $filename
#rm $WALLDIR/$filename
filepath="$WALLDIR/$filename"
curl "$url" -o "$filepath"

#if [ file "$WALLDIR/temp.$ext" | grep -q "corrupted" ] && [ [ echo $(url: -3} !== "png" ] && [ echo $(url: -3) == "jpg" ] && [ echo ${url: -3} == "peg" ] ]; then
#if [ file "$WALLDIR/temp.$ext" | grep -q "corrupted" ] || [ echo "${url: -3}" != "png" && echo "${url: -3}" != "jpg" && echo "${url: -3}" == "peg" ]; then
if [ $(file "$WALLDIR/temp.$ext" | grep -q "corrupted") ] || [ "${url: -3}" != "png" ] && [ "${url: -3}" != "jpg" ]; then
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!image is corrupted!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
else
corrupted_image=false
echo image is good, breaking
ext=${url: -3}
break
fi
((repetition++))
echo repeition+=1 = $repetition
done


#Save image information into data.txt
#echo "$SELECTED_IMAGE" > "$WALLDIR"/json
#title=$(echo "$SELECTED_IMAGE" | jq -r ".data.children[-1].data.title")
#echo $title
#echo "$title" > "$WALLDIR"/data.txt
#echo $SELECTED_IMAGE | jq -r ".data.children[-1].data.subreddit" >> "$WALLDIR"/data.txt
#echo "$url" >> "$WALLDIR"/data.txt
#echo https://reddit.com$(echo "$SELECTED_IMAGE" | jq -r ".data.children[-1].data.permalink" | sed 's/"//g') >> "$WALLDIR"/data.txt
#echo saved image info to wallpaper dir

echo created image file


#Add image title to the temp
convert "$WALLDIR/temp.$ext" -resize "$SIZE^" -gravity center -crop "$SIZE+0+0" -resize "$SIZE!" "$WALLDIR/wallpaper.jpg"
rm "$WALLDIR/temp.jpg"
rm "$WALLDIR/temp."
rm "$WALLDIR/temp.png"


if [ $WRITE_TITLE == 1 ]; then
touch $WALLDIR/wallpaper.jpg
echo created image file
#convert "$WALLDIR/wallpaper.jpg" -fill "$TITLE_COLOR" -gravity "$TITLE_POSITION" -pointsize "$TITLE_SIZE" -annotate 0 "$title" "$WALLDIR/wallpaper.jpg"
mv $WALLDIR/temp.$ext $WALLDIR/wallpaper.jpg
echo added image title and contents to file wallpaper.jpg

fi

nitrogen --set-scaled --head=$(($2 - 1)) $HOME/.wallpaper/wallpaper.jpg
echo Success >> "$WALLDIR"/data.txt
elif [ "$2" = "" ]; then
echo specify monitor
fi


elif [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
echo help
fi
11 changes: 8 additions & 3 deletions reddit-wallpaper-fetcher.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#List of subreddits to check
SUBREDDITS=("Wallpapers")
#List of subreddits to check sperated as an array
#The more you add the more variety
SUBREDDITS=("Wallpapers" "earthporn")
#What sort fetch list by, will choose the latest
SORT="new"
#User-agent, shouldn't have to change
Expand All @@ -13,4 +14,8 @@ TITLE_COLOR="white"
#Title position for ImageMagick (North, South, East, West, Centre)
TITLE_POSITION="Southeast"
#Title font size
TITLE_SIZE=30
TITLE_SIZE=20
#When sorting, top amount of results to show up
#i.e if set to new and 5 it would find the 5 newest posts
AMOUNT_OF_RESULTS_TO_SORT=5