From a03b71fd0ca338372854e8978235e681bf0d8da7 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Fri, 27 Aug 2021 07:01:00 +0000
Subject: [PATCH] output subreddit to data.txt

---
 reddit-wallpaper-fetcher |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/reddit-wallpaper-fetcher b/reddit-wallpaper-fetcher
index 9e2bba5..82108de 100755
--- a/reddit-wallpaper-fetcher
+++ b/reddit-wallpaper-fetcher
@@ -4,7 +4,7 @@
 
 #Default variables
 SUBREDDITS=("Wallpapers")
-SORT=new
+SORT="new"
 USER=$(whoami)
 SIZE=1920x1080
 WRITE_TITLE=1
@@ -31,7 +31,7 @@
 LATEST_TIME=0
 if [ -f "$WALLDIR"/json ]; then
     LATEST=$(cat "$WALLDIR"/json)
-    LATEST_TIME=$(echo "$LATEST" | jq ".data.children[-1].data.created_utc")
+    LATEST_TIME=$(echo "$LATEST" | jq -r ".data.children[-1].data.created_utc")
 fi
 if [ "$LATEST_TIME" == "null" ]; then
     LATEST_TIME=0
@@ -40,7 +40,10 @@
 #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 ".data.children[-1].data.created_utc")
+    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"
@@ -53,20 +56,21 @@
 fi
 
 #Download image
-url=$(echo "$LATEST" | jq ".data.children[-1].data.url" | sed 's/"//g')
+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 ".data.children[-1].data.gallery_data.items[0].media_id" | sed 's/"//g')
-	url=$(echo "$LATEST" | jq ".data.children[-1].data.media_metadata.$media_id.p[0].u" | sed 's/"//g;s/\?.*//g;s/preview/i/g')
+	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 ".data.children[-1].data.title")
+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 ".data.children[-1].data.permalink" | sed 's/"//g') >> "$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"

--
Gitblit v1.10.0