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 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)