Archive
Posts Tagged ‘reddit api’
submitting a link to reddit and adding a comment
March 30, 2012
Leave a comment
Problem
You want to send a large number of links to reddit and you want to get it done with a script. How to send links and how to add comments to them?
Solution
First, install the reddit api:
sudo pip install reddit -U
Then you can use my RedBot to perform the task. It is part of my jabbapylib library.
#!/usr/bin/env python
"""
Simple API for:
* posting links to reddit
* commenting on a post
This requires that you have a reddit account. Put your username and
password in the following files:
* ~/reddit_username.txt
* ~/reddit_password.txt
To use RedBot, just make a subclass of it and give it a cool name:
class HyperBot(RedBot):
def __init__(self):
super(HyperBot, self).__init__()
self.name = 'HyperBot'
Now you are ready to flood reddit :)
# from jabbapylib.reddit import red
"""
import reddit
from jabbapylib.filesystem import fs
from jabbapylib.platform import platform
USERNAME_TXT = '{home}/reddit_username.txt'.format(home=platform.get_home_dir())
PASSWORD_TXT = '{home}/reddit_password.txt'.format(home=platform.get_home_dir())
#
USERNAME = fs.read_first_line(USERNAME_TXT)
PASSWORD = fs.read_first_line(PASSWORD_TXT)
class RedBot(object):
def __init__(self):
self.name = 'RedBot'
self.username = USERNAME
self.password = PASSWORD
#
self.r = reddit.Reddit(user_agent=self.name)
self.r.login(username=self.username, password=self.password)
self.last_post = None # Submission object
self.permalink = None # URL of the last post
def submit_link(self, url, subreddit, title):
"""
The return value (res) is a Submission object or None.
URL of the newly created post: res.permalink
"""
try:
self.last_post = self.r.submit(subreddit, title, url=url)
self.permalink = self.last_post.permalink
print '# url to send: {url}'.format(url=url)
print '# submitted to: {pl}'.format(pl=self.permalink)
return self.last_post
except:
print >>sys.stderr, "Warning: couldn't submit {url}".format(url=url)
return None
def add_comment(self, comment):
if self.last_post:
self.last_post.add_comment(comment)
print '# comment added'
#############################################################################
if __name__ == "__main__":
# here is how to use it:
# url = '...'
# subreddit = '...'
# title = "..."
# comment = '...'
# r = RedBot()
# r.submit_link(url, subreddit, title)
# r.add_comment(comment)
pass
You can find the current version here.
Thanks to Bryce Boe, the maintainer of the reddit api, who kindly answered my questions.
Categories: python
post to reddit, reddit, reddit api

Send a post to reddit from Python
Problem
How to send a post to reddit.com from a Python script? Motivation: when you send a post, you have to wait 8 minutes before you could send the next one. Imagine you have 10 posts to submit. It’d be nice to launch a script at night which would send everything till next morning.
Submit a post
Now I only show how to send one post. Batch processing is left as a future project.
The official Reddit API is here. There is a wrapper for it called reddit_api, which greatly simplifies its usage.
Install reddit_api:
Submit a post:
Submit a comment (update, 20111107)
Let’s see how to add a comment to a post. First, we need the URL of a post.
Example: http://www.reddit.com/r/thewalkingdead/comments/lkycy/that_look_on_the_kids_face/. Here, the last part of the URL is just garbage, the following URL is equivalent with it: http://www.reddit.com/r/thewalkingdead/comments/lkycy. The unique ID of the post is the last part: “lkycy”. Thus, this image can be accessed via this URL too: http://www.reddit.com/lkycy.
Now, let’s log in to reddit, fetch the post by its ID and add a comment.
def get_reddit_id(url): result = re.search('/comments/(.*?)/', url) return result.group(1) def add_comment(r, reddit_url): reddit_id = get_reddit_id(reddit_url) post = r.get_submission_by_id(reddit_id) comment = "first" # just to make reddit happy ;) post.add_comment(comment) print '# comment added:', comment def main(): r = reddit.Reddit(user_agent="my_cool_application") r.login(user="...", password="...") reddit_url = ... add_comment(r, reddit_url)