mirror of https://github.com/Chizi123/Reddit-wallpaper-fetcher.git

Joel Grunbaum
2020-12-03 f77646f4f5479464282480b9ce27e28d5859de0a
Changed querying to jq instead of grep, names images
1 files modified
62 ■■■■ changed files
main.sh 62 ●●●● patch | view | raw | blame | history
main.sh
@@ -1,30 +1,66 @@
#!/bin/bash
IMAGE_REGEX='"url": "https:\/\/i\.redd\.it\/[a-z0-9]*\.\w{3,4}\", "subreddit_subscribers": '
TITLE_REGEX='"clicked": (true|false), "title": "[a-zA-Z0-9.\[\]\(\) ]*", "link_flair_richtext": \['
TIME_REGEX='"subreddit_subscribers": \d*, "created_utc": [\d.]*, "num_crossposts": '
PERMALINK_REGEX=', "permalink": "\/r\/[A-Za-z0-9\/_]*", "parent_whitelist_status": '
TESTING=1
#Default variables
SUBREDDITS=("EarthPorn")
SORT=new
USER=$(whoami)
SIZE=1920x1080
WRITE_TITLE=1
TITLE_COLOR="white"
TITLE_POSITION="Southeast"
TITLE_SIZE=30
source $HOME/.config/reddit-wallpaper-fetcher.conf &>/dev/null
LATEST_TIME=0
if [ -f $HOME/.wallpaper/json ]; then
    LATEST=$(cat ~/.wallpaper/json)
    LATEST_TIME=$(echo $LATEST | grep -oP "$TIME_REGEX" | tail -n1 | grep -oP '"created_utc": [\d.]*' | grep -oP '[\d.]*')
#Set directories for testing
if [ $TESTING != 1 ]; then
    CONFDIR="$HOME/.config"
    WALLDIR="$HOME/.wallpaper"
else
    CONFDIR="$(dirname $(realpath $0))"
    WALLDIR="$(dirname $(realpath $0))"
fi
source $CONFDIR/reddit-wallpaper-fetcher.conf &>/dev/null
#Initialise time from last fetch
LATEST_TIME=0
if [ -f $WALLDIR/json ]; then
    LATEST=$(cat $WALLDIR/json)
    LATEST_TIME=$(echo $LATEST | jq ".data.children[-1].data.created_utc")
fi
#Try subreddits for new images
for i in "${SUBREDDITS[@]}"; do
    temp=$(curl -s --user-agent $USER "https://www.reddit.com/r/$i.json?sort=$SORT&limit=1")
    time=$(echo $temp | grep -oP "$TIME_REGEX" | tail -n1 | grep -oP '"created_utc": [\d.]*' | grep -oP '[\d.]*')
    time=$(echo $temp | jq ".data.children[-1].data.created_utc")
    if [ $(awk -v n1="$time" -v n2="$LATEST_TIME" 'BEGIN {printf (n1>n2?"1":"")}') ]; then
    LATEST=$temp
    LATEST_TIME=$time
    fi
done
url=$(echo $LATEST | grep -oP "$IMAGE_REGEX" | tail -n1 | grep -oP '"https:\/\/.*",' | grep -oP '".*"' | sed 's/"//g')
curl -s -o "$HOME/.wallpaper/temp.$(echo $url | rev | cut -d'.' -f1 | rev)" "$url"
#No Update to do
#if [ -f $WALLDIR/json ] && [ "$(cat $WALLDIR/json)" == "$LATEST" ]; then
#    exit
#fi
#Download image
url=$(echo $LATEST | jq ".data.children[-1].data.url" | sed 's/"//g')
ext="$(echo $url | rev | cut -d'.' -f1 | rev)"
curl -s -o "$WALLDIR/temp.$ext" "$url"
#Save image information
echo $LATEST > $WALLDIR/json
title=$(echo $LATEST | jq ".data.children[-1].data.title")
echo "$title" > $WALLDIR/data.txt
echo $url >> $WALLDIR/data.txt
echo https://reddit.com$(echo $LATEST | jq ".data.children[-1].data.permalink" | sed 's/"//g') >> $WALLDIR/data.txt
#Add image title
if [ $WRITE_TITLE == 1 ]; then
    convert "temp.$ext" -fill "$TITLE_COLOR" -gravity "$TITLE_POSITION" -pointsize "$TITLE_SIZE" -annotate 0 "$title" -resize $SIZE "wallpaper.jpg"
else
    convert "temp.$ext" -resize $SIZE "wallpaper.jpg"
fi
rm temp.$ext