Reddit

I previously wrote about installing my own instance of Reddit but I haven't done a lot with. This post is really just to share a couple things I have been trying to work on to make the Reddit instance usable.

I've mostly just been working to try to get my instance to suck in text and link posts from the real reddit.com, however I know very little about Python so I currently am only able to fetch some posts, but I am not sure how I get them into my instance as a specific user. Here's my script so far:

#!/usr/bin/python

import praw
import logging

logging.captureWarnings(True)

r = praw.Reddit(user_agent="Ubuntu:reddit.local:v0.1 (by /u/jimmybreddit)")
subreddit=r.get_subreddit('Equestrian')

for submission in subreddit.get_new(limit=10):
    print "---------------------------------"
    print submission.title
    if submission.is_self == True
        print submission.selftext
    else
        print submission.url
    print "---------------------------------"

I am going to keep working on it, and hopefully one day I can get my Reddit instance to pull in posts as a bot. 😄

Update - November 18, 2015: I've been working on my Reddit API script and this is what I've come up with:

#!/usr/bin/python

import praw
import logging
import time
import calendar

logging.captureWarnings(True)

fh = open("/home/username/Scripts/announcements.date", "r")
previous = fh.read()

r = praw.Reddit(user_agent='Ubuntu:reddit.local:v0.1 (by /u/username)', site_name='reddit')
subreddit=r.get_subreddit('announcements')

for submission in subreddit.get_new(limit=50):
    if int(submission.created) > int(previous):
        if submission.is_self == True:
            rl = praw.Reddit(user_agent='Ubuntu:reddit.local:v0.1 (by /u/username)', site_name='local_dev')
            rl.login(username="$USERNAME", password="$PASSWORD")
            rl.submit('announcements', submission.title, text=submission.selftext)
            print "Submitting Text: " + submission.title
        if submission.is_self == False:
            rl = praw.Reddit(user_agent='Ubuntu:reddit.local:v0.1 (by /u/username)', site_name='local_dev')
            rl.login(username="$USERNAME", password="$PASSWORD")
            check = r.request_json("http://reddit.local/api/info/.json", params={"url": submission.url})
            if check == '':
                rl.submit('announcements', submission.title, url=submission.url)
                print "Submitting Link: " + submission.title

timenow = calendar.timegm(time.gmtime())
timenow = int(timenow) + 25200

fh = open("/home/username/Scripts/announcements.date","w")
fh.write(str(timenow))
fh.close()

So what this script does is connects with reddit.com and pulls the last 50 new submissions from a specific subreddit. It relies on a seprate file which stores the date of the last check and should only submit new submissions since that date. The date is stored in a Unix timestamp value. It also checks if the submission is a text post or link. If a link it checks the local installation to ensure the link hasn't been submitted to the subreddit before.

Now, I still had some issues with this, where submissions coming in from reddit.com where posted in the future. I have no idea how that happened. Due to this, it resulted in duplicate posts to my local installation, but beyond this the script worked well.

Also of note, this script uses no authentication to reddit.com and username and password authentication to the local installation. Obviously not ideal. It should be using OAuth2, but I didn't have time or patience to wrap my head around that.