Skip to content

Instantly share code, notes, and snippets.

@kvalv
Created May 18, 2018 22:02
Show Gist options
  • Save kvalv/b6836cb8f499bf1f1fe8d285cd70db20 to your computer and use it in GitHub Desktop.
Save kvalv/b6836cb8f499bf1f1fe8d285cd70db20 to your computer and use it in GitHub Desktop.
if [ x$1 == "x--help" ]; then
cat <<EOF
Usage: $0 [subreddit] [past]
subreddit: string, any subreddit with images
past: day|week|month|year
EOF
fi
subreddit=${1:-itookapicture}
past=${2:-month}
link="https://www.reddit.com/r/$subreddit/top.json?sort=top&t=$past.json"
# link="https://www.reddit.com/r/itookapicture/top.json?sort=top&t=month.json"
image_link=$(python - << END
import requests, random
# choose some common user agent to not get blocked
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
data = requests.get("$link", headers={'user-agent': user_agent}).json()
data = data['data']['children'] # 25 results
data = data[:min(len(data), 10)]
idx = random.randint(0, len(data))
image_link = data[idx]['data']['preview']['images'][0]['source']['url']
print(image_link)
END
)
echo $image_link
filename=$(mktemp --suffix=.png)
wget $image_link -O $filename
feh --bg-fill $filename
rm $filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment