-
Notifications
You must be signed in to change notification settings - Fork 0
/
showerthought_cache
executable file
·46 lines (33 loc) · 1.51 KB
/
showerthought_cache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# Showerthought cache script, by Blake Oliver <oliver22213@me.com>
# directly retrieving a thought from /r/showerthoughts manually on every login
# is too slow
# This script is intended to be run from cron or something similar;
# it will create / update a cache of "thoughts" in /usr/share/showerthought
# that the 'showerthought' script, whether called for the MOTD, or
# manually, will randomly pick one from
import os
import praw
import json
# Variables that control aspects of the cache
# Location of the cache directory (must be the same in the 'showerthought' script
showerthought_dir = "/usr/share/showerthought"
# Filename to store caches in
# This must be the same in the 'showerthought' script
showerthought_cfn = "thoughtcache.json"
# Limit of how many "thoughts" to cache
# Note: the API limit for one request to Reddit is 100;
# with 2 seconds between requests, the higher the limit the longer this script will take
# Depending on how long between cache refreshes there is, you might want
# igher for longer times between refreshes and lower for shorter ones
showerthought_limit = 100
if os.path.exists(showerthought_dir) == False:
os.makedirs(showerthought_dir)
thoughtlist = []
r = praw.Reddit("""Showerthought MOTD caching script by /u/bladehunter2213""")
s = r.get_subreddit("showerthoughts")
for p in s.get_hot(limit=showerthought_limit):
thoughtlist.append(p.title)
j = json.dumps(thoughtlist)
with open(os.path.join(showerthought_dir, showerthought_cfn), 'w') as f:
f.write(j)