Sometimes, you’re testing a chat bot dingus, and you need a couple images so it can respond to
@bot boop . I wanted more than 10, quickly, preferably without having to click through Google Image search and manually download them.
Turns out Reddit exposes any endpoint in JSON as well as for web browsers, and that’s publicly accessible. And Reddit has a whole community called /r/BoopableSnoots .
So I threw this command together:
curl "https://www.reddit.com/r/BoopableSnoots.json" | \
jq -c '.data.children | ..data.url' | \
xargs -n 1 curl -O
What is this doing?
curl - sends a GET request to the specified URL and forwards the response body to stdout .
| - take the output of the previous command on stdout and feed it into the next command as stdin
jq -c '...' - jq is a command-line JSON parser and editor. This command drills one level down the object structure returned by reddit and returns the
data.url field. The
-c flag removes quoting from the output, and the
. iterates across an array, outputting one element per line
xargs - is a meta-command; it says “run the following command for each line of input on stdin.” It can run in parallel, or in a pool of workers, etc.
curl -O - sends a GET request to the specified URL and saves the response to the filesystem using the filename contained in the URL
Putting it all together:
- Get the ~25 most recent posts off Reddit’s BoopableSnoots community
- Filter down to just the images that people posted
- download those images to the current directory
This quickly got me some snoots for my bot to boop, and I could move on with my work.