Shower Thoughts Using a Raspberry Pi

Introduction: Shower Thoughts Using a Raspberry Pi

About: I like taking stuff apart and making random things

This project is how to write a python program to grab a random post from the Reddit ShowerThoughts subreddit at

Step 1: Things You Need

This is a simple requirement. You just need any version Raspberry Pi that is already setup and connected to the internet.

Step 2: The Code

First we need to create the program. In terminal type in sudo nano, then paste in this code and save by pressing ctrl-x

import json, random, textwrap, requests

randompost = random.randint(1,20)

url = requests.get('', headers = {'User-agent': 'Showerthoughtbot 0.1'})

reddit = json.loads(url.text)

print textwrap.fill((reddit['data']['children'][randompost]['data']['title']),32)

To run the code in terminal type in sudo python

Step 3: How the Code Works

First we need to import some modules to the program.

import json, random, textwrap, requests

Next we will need a way to get a random number between 1 and 20, we will call this value randompost.This will be used later to get a random post.We skip post 0 since that is just an announcement for the subreddit

randompost = random.randint(1,20)

Now we will grab the json data. We will be getting the hot posts, which is the best recent posts. According to the Reddit api we need to identify ourselves by putting in a User Agent. Im calling this bot Showerthoughtbot 0.1, but if this stops working we will need to rename it.

url = requests.get('', headers = {'User-agent': 'Showerthoughtbot 0.1'})

Then we call that data reddit

reddit = json.loads(url.text)

Then we display the post. We want the title of the post. We use textwrap so that after 32 characters it starts a new line.I picked 32 so the text doesnt scroll too far in the screen. Depending on how you display this you may need to adjust it.

print textwrap.fill((reddit['data']['children'][randompost]['data']['title']),32)

Step 4: Ways to Improve

There are multiple things that could be added on to this project. One thing I would like to add in the future is to have it print out the results on a small thermal printer. Could also interface with a coin acceptor to need money before giving a result. You could also have it display on a lcd display.



    • Water Contest

      Water Contest
    • Metalworking Contest

      Metalworking Contest
    • Tiny Home Contest

      Tiny Home Contest

    3 Discussions

    Why the sudo command to run the script? I would expect it to run without root permissions, which would be simpler, easier, and arguably more secure.

    Thanks for an interesting idea!

    2 replies

    Im not that good with linux yet. So this was the way I knew to run it.

    Nice to see you taking the steps to learn!

    The script should run with just "python". Also, unless you are saving the script outside of your home directory (which is your console's default location, you can launch nano without using sudo as well.

    Also, if you make the script executable (chmod +x you can run the script without typing "python" at the beginning. An extra bonus is adding "#! /usr/bin/python" or "#! usr/bin/env python" as the first line of your script. Then you can drop the file extension completely and just run the script by typing "thought".

    The frustrating thing about learning the Linux command line for me was that not everyone makes it clear when something like sudo is necessary. Installing programs and libraries usually requires it, editing system files requires it, but tweaking things in your home directory never does unless you have written a script or program that does one of those. Because of this, using sudo is an understandable default.

    Hope my rambling was educational, and keep up the good work.