def main():
    from TweetPoster.reddit import Redditor
    from TweetPoster.twitter import Twitter

    db = Database()
    db.init()
    twitter = Twitter()
    reddit = Redditor().login(
        config['reddit']['username'],
        config['reddit']['password']
    )

    while True:
        try:
            posts = reddit.get_new_posts(db)
            for post in posts:
                handle_submission(post, twitter, reddit)
        except KeyboardInterrupt:
            import sys
            sys.exit(0)
        except requests.exceptions.Timeout:
            # These are exceptions we don't
            # want to tell sentry about
            pass
        except:
            sentry.captureException()
        finally:
            print 'sleeping'
            time.sleep(90)
Exemple #2
0
def main():
    from TweetPoster.reddit import Redditor
    from TweetPoster.twitter import Twitter

    db = Database()
    db.init()
    twitter = Twitter()
    reddit = Redditor().login(config['reddit']['username'],
                              config['reddit']['password'])

    while True:
        try:
            posts = reddit.get_new_posts(db)
            for post in posts:
                handle_submission(post, twitter, reddit)
        except KeyboardInterrupt:
            import sys
            sys.exit(0)
        except requests.exceptions.Timeout:
            # These are exceptions we don't
            # want to tell sentry about
            pass
        except:
            sentry.captureException()
        finally:
            print 'sleeping'
            time.sleep(90)
def test_ratelimit():
    mock_index()
    url = 'http://www.reddit.com/'
    r = Redditor()
    start = time.time()
    r.get(url)
    r.get(url)
    end = time.time()
    assert end - start > 2
def test_ratelimit():
    mock_index()
    url = 'http://www.reddit.com/'
    r = Redditor()
    start = time.time()
    r.get(url)
    r.get(url)
    end = time.time()
    assert end - start > 2
def test_login():
    mock_login()
    r = Redditor(bypass_ratelimit=True)
    r.login('TweetPoster', 'hunter2')
    req = httpretty.last_request()

    assert r.authenticated
    assert 'api_type=json' in req.body
    assert 'passwd=hunter2' in req.body
    assert 'user=TweetPoster' in req.body
    assert 'nom' == r.session.cookies['reddit_session']
def test_login():
    mock_login()
    r = Redditor(bypass_ratelimit=True)
    r.login('TweetPoster', 'hunter2')
    req = httpretty.last_request()

    assert r.authenticated
    assert 'api_type=json' in req.body
    assert 'passwd=hunter2' in req.body
    assert 'user=TweetPoster' in req.body
    assert 'nom' == r.session.cookies['reddit_session']
def test_comment():
    mock_login()
    mock_comment()

    thing_id = 't3_1hb15l'
    comment = 'test comment'

    r = Redditor(bypass_ratelimit=True).login('tp', 'hunter2')
    c = r.comment(thing_id, comment)

    thing = c.json()['json']['data']['things'][0]
    assert comment == thing['data']['contentText']
    assert thing_id == thing['data']['parent']
def test_comment():
    mock_login()
    mock_comment()

    thing_id = 't3_1hb15l'
    comment = 'test comment'

    r = Redditor(bypass_ratelimit=True).login('tp', 'hunter2')
    c = r.comment(thing_id, comment)

    thing = c.json()['json']['data']['things'][0]
    assert comment == thing['data']['contentText']
    assert thing_id == thing['data']['parent']
def test_get_new():
    mock_posts()
    db = Database()
    db.init(clean=True)
    r = Redditor(bypass_ratelimit=True)
    all_posts = r.get_new_posts(db)
    assert len(all_posts) == 15

    # Mark some as processed
    for i, p in enumerate(all_posts):
        if i % 5 == 0:
            p.mark_as_processed(db)

    # Now check if only returns new ones
    posts = r.get_new_posts(db)
    assert len(posts) == 12
def test_get_new():
    mock_posts()
    db = Database()
    db.init(clean=True)
    r = Redditor(bypass_ratelimit=True)
    all_posts = r.get_new_posts(db)
    assert len(all_posts) == 15

    # Mark some as processed
    for i, p in enumerate(all_posts):
        if i % 5 == 0:
            p.mark_as_processed(db)

    # Now check if only returns new ones
    posts = r.get_new_posts(db)
    assert len(posts) == 12
def test_useragent():
    mock_index()
    r = Redditor(bypass_ratelimit=True)
    r.get('http://www.reddit.com')
    h = httpretty.last_request().headers
    assert r.headers['User-Agent'] == h['User-Agent']
def test_useragent():
    mock_index()
    r = Redditor(bypass_ratelimit=True)
    r.get('http://www.reddit.com')
    h = httpretty.last_request().headers
    assert r.headers['User-Agent'] == h['User-Agent']