I love reddit, and let's be honest I'm probably a bit of an addict. When you come to use the site a lot, you will end up noticing that it is missing a few key features.
Thankfully with a little bit of Python and the amazing PRAW (Python Reddit API Wrapper) library, you can solve these problems really quickly.
Search saved links
Have you ever wanted to search for that funny cat picture you saved a year ago which has suddenly become relevant to your current situation?
Well reddit doesn't let you. You can go to your saved links, and then look through 25 posts at a time, having to click next each time. Or if you are the lucky owner of reddit gold you can filter by subreddit, which still isn't much use if you know what you are looking for, but not which subreddit.
This was frustrating me, so I wrote a small script to allow me to search through my saved links. It is a little slow as it gets all 1000 posts each time it runs, however as I run it so infrequently I don't mind waiting!
The script uses docopt and PRAW so you will need to install those via pip. This can be done with pip install --user praw docopt
. The repo has a requirements.txt if you find that easier.
"""
Usage:
savedLinks [options]
Options:
-t, --title TITLE Search for links based on link title
-d, --domain DOMAIN Search for links from a certain domain
-r, --reddit REDDIT Search for links based on subreddit
"""
from docopt import docopt
import praw
import sys
if __name__ == "__main__":
args = docopt(__doc__)
criteria = sum(1 for v in args.values() if v is not None)
if criteria == 0:
sys.exit(__doc__)
r = praw.Reddit(user_agent='savedSearch',
client_id='OkDyg4-hOs-TbQ',
client_secret='******************',
username='Midasx',
password='**********',)
for post in r.redditor('Midasx').saved(limit=None):
count = 0
if not hasattr(post, 'domain'):
continue # Filter out saved comments
if args['--domain']:
if args['--domain'].lower() == post.domain:
count += 1
if args['--reddit']:
if args['--reddit'].lower() == post.subreddit.display_name.lower():
count += 1
if args['--title']:
if args['--title'].lower() in post.title.lower():
count += 1
if count == criteria:
print(post.shortlink, " ", post.title)
To get this running you will need to make an app on your reddit account. Just go to your apps, and hit create another app...
then give it a name and a redirect URL (Can be anything, we aren't using it), then make sure to select that it is a Script for personal use.
Notifications for small subreddits
If you are really interested in a niche subreddit, it would be nice to know when someone posts there. A lot of my smaller subscriptions have posts go unanswered just because no one checks the sub frequently due to its inactivity.
Currently reddit doesn't offer a way to get alerted when a new post is made in a subreddit so that is why I came up with this nice little bot to do it for you. It only took an hour to write, and appears to work quite nicely. Once setup it will watch subreddits of your choice and PM you with a link to the new post when one is made.
import sys
import praw
import time
user = 'Midasx'
reddits = ['vim']
seen = []
r = praw.Reddit(user_agent='NewPost',
client_id='RGtz43e2ZuuuoA',
client_secret='*******************',
username='NewPostAlert',
password='***********',)
print("Logged in")
first = True
while True:
try:
for sub in reddits:
for post in r.subreddit(sub).new(limit=10):
if first is True:
seen.append(post.id)
if post.id not in seen:
subject = 'New post in ' + str(post.subreddit)
content = '[' + post.title + '](' + post.shortlink + ')'
r.redditor(user).message(subject, content)
print('New post! Sending PM.')
seen.append(post.id)
time.sleep(5)
first = False
except KeyboardInterrupt:
print('\n')
sys.exit(0)
except Exception as e:
print(e)
Again, as with the saved links script you will need to create an app for a user, this time I decided to create a separate user for the bot as well.
Just edit in the bots details, the user you want to message and the reddits you want it to watch. If I have time in the future I would love to make this an online service where a user can sign up for notifications on subreddits via a third party service.
Conclusion
I wish reddit made these features standard and available to everyone, however I am really thankful that the PRAW library is so easy to use. It is a really nice feeling to find a problem in your day to day life, and solve it quickly and simply with a few lines of code!
:wq