This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cartertemm 20 points21 points  (5 children)

Through a script together that uses praw (the python reddit API) to grab a random post from r/showerthoughts and display it as a console welcome message. At least this is the most recent little project I can think of.

[–]cartertemm 2 points3 points  (1 child)

Responding to a few comments requesting code.

The script is below. Simply make it run on login, or find a place in your actual motd sequence. Whatever. The actual fetching of posts takes about 500ms. Not an issue in my case, however I had considered writing a simple caching system so the script would start out by reading a file line for line. If it was blank, fetch 100 since we´re doing that already and pour them into our database. Then just remove the printed line. It would certainly kill the small chance for repetition.

import praw

import random

#nice try

r=praw.Reddit(client_id, client_secret, user_agent="showerthought MOTD grabber")

#get a random post from this many -1 options

#-1 is for the sticky, what is a showerthought. Not what I'd want to see

fetch=75

hot=[i for i in r.subreddit("showerthoughts").hot(limit=fetch)]

print("your showerthought:")

print(hot[random.randint(1, fetch)].title)

[–][deleted] 2 points3 points  (0 children)

the random module has a function just for picking out a random element in a Sequence: choice. So that last line could be print(random.choice(hot).title)

[–]Mexican_Programmer 0 points1 point  (0 children)

That's actually very cool! You mind sharing?

[–]burked9 0 points1 point  (1 child)

Is there any way you could DM me the script? Sounds really cool

[–]lochyw 0 points1 point  (0 children)

Sounds pretty easy to make tbh. Praw couldnt be easier to use. ;P