Still frustrating to have to deal with that though

Oh and just for fun. Here's the full script. With random lines removed so you can't run it yourself. Not that I'm worried. I know you're too stupid to understand any of this.

opener = urllib2.buildopener(urllib2.HTTPCookieProcessor) def urlopen(url, data=None, headers = {}): if hasattr(data, "iter_"): data = urllib.urlencode(data) return opener.open(urllib2.Request(url, data, headers))

if name == "main": HASBACON = False format = None #datetime = time.strftime("%Y%m%d%H%M%S", time.gmtimeimport, cookielib, urllib, urllib2

Turns out simplejson is included in Python 2.6 and up as json.

try: import json as simplejson except ImportError: try: import simplejson except ImportError: raise ImportError('No module named json or simplejson')

REDDIT_USER_NAME = 'midsummernightstoker' TARGET = "SiberianShibe" MESSAGE = "Dance, monkey, dance!"

class RedditInvalidUsernamePasswordException(Exception): pass

class RedditNotLoggedInException(Exception): pass

class RedditBadJSONException(Exception): pass

class Reddit(object):

user = None

def __init__(self):
    cookie_jar = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
    urllib2.install_opener(opener)

def login(self, user, passwd):
    self.user = user

    params = urllib.urlencode({
        'id' : '#login_login-main',
        'op' : 'login-main',
        'passwd' : passwd,
        'user' : user
    })

    req = urllib2.Request(REDDIT_LOGIN_URL, params, REDDIT_USER_AGENT)
    retval = urllib2.urlopen(req).read()

    if retval.find('invalid password') != -1:
        self.logged_in = False
        raise RedditInvalidUsernamePasswordException('Log in failed. Please ensure that your username and password are correct.')
    else:
        self.logged_in = True

def get_new_mail(self):
    #if not self.logged_in:
        #raise RedditNotLoggedInException('You must be logged in to check for new messages.')

    url = '%s?mark=false' % REDDIT_INBOX_PAGE

    req = urllib2.Request(url, None, REDDIT_USER_AGENT)
    json_data = ''
    try:
        json_data = urllib2.urlopen(req).read()
    except ValueError:
        print ("Connection timeout")

    try:
        inbox = simplejson.loads(json_data)
        msgs = inbox['data']['children']
        return [msg['data'] for msg in msgs if msg['data']['new'] == True]

    except (KeyError, ValueError):
        raise RedditBadJSONException('The JSON returned from reddit is incomplete. Perhpas the connection was interupted or reddit is down.')

def mark_messages_as_read(self):
    if not self.logged_in:
        raise RedditNotLoggedInException('You must be logged in to check for new messages.')

    req = urllib2.Request(REDDIT_INBOX_PAGE, None, REDDIT_USER_AGENT)

def get_mail(): url = '%s?mark=false' % REDDIT_INBOX_PAGE

req = urllib2.Request(url, None, REDDIT_USER_AGENT)

try:
    msgs = inbox['data']['children']
    return [msg['data'] for msg in msgs if msg['data']['new'] == True]

except (KeyError, ValueError):
    raise RedditBadJSONException('The JSON returned from reddit is incomplete. Perhaps the connection was interupted or reddit is down.')

def mark_messages_as_read(): req = urllib2.Request(REDDIT_INBOX_PAGE, None, REDDIT_USER_AGENT) urllib2.urlopen(req).read()

reddit = Reddit()

params = urllib.urlencode({ 'api_type' : 'json', 'user' : REDDIT_USER_NAME, })

req = urllib2.Request("http://www.reddit.com/api/login/username", params, REDDIT_USER_AGENT) got_data = False json_data = None while not got_data: try: temp = urllib2.urlopen(req) json_data = temp.read() got_data = True except: print "Couldn't get credentials" time.sleep(60)

print modhash print cookie

lastMsgTS = 0

mail = reddit.get_new_mail()
print "got mail"
if mail != []:
    ts = int(mail[0]['created'])
    if lastMsgTS != ts and mail[0]['author'] == TARGET:
        wait = random.randint(30, 120)
        print("Reply to be sent in " + str(wait) + " seconds.")
        time.sleep(wait)

        params = urllib.urlencode({
            'id' : "#commentreply_"+mail[0]['name'],
            'r' : mail[0]['subreddit'],           
            'text' : MESSAGE,
            'thing_id' : mail[0]['name'],
            'uh' : modhash
        })

        req = urllib2.Request("http://www.reddit.com/api/comment", params, REDDIT_USER_AGENT)
        try:
            temp = urllib2.urlopen(req)
            json_data = temp.read()
            lastMsgTS = ts
            print("Reply sent")
        except:
            print "Connection failed"

        #mark_messages_as_read()
    else:
        print "No new messages"
else:
    print "No new messages"

time.sleep(random.randint(120, 480))

return [msg['data'] for msg in msgs if msg['data']['new'] == True and msg['data']['author'] == "SaintBarnaby" and msg['data']['context'].find("jewish_settlers_torch") != -1]

/r/AdviceAnimals Thread Parent Link - i.imgur.com