Example #1
0
def reddit(vcr, request):
    cassette_name = '%s.yaml' % request.node.name
    # Clear the cassette before running the test
    if request.config.option.record_mode == 'once':
        filename = os.path.join(vcr.cassette_library_dir, cassette_name)
        if os.path.exists(filename):
            os.remove(filename)

    with vcr.use_cassette(cassette_name):
        with patch('rtv.packages.praw.Reddit.get_access_information'):
            reddit = praw.Reddit(user_agent='rtv test suite',
                                 decode_html_entities=False,
                                 disable_update_check=True)
            # praw uses a global cache for requests, so we need to clear it
            # before each unit test. Otherwise we may fail to generate new
            # cassettes.
            reddit.handler.clear_cache()
            if request.config.option.record_mode == 'none':
                # Turn off praw rate limiting when using cassettes
                reddit.config.api_request_delay = 0
            yield reddit
Example #2
0
def main():

    locale.setlocale(locale.LC_ALL, '')

    if len(sys.argv) > 1:
        theme = Theme.from_name(sys.argv[1])
    else:
        theme = Theme()

    vcr = initialize_vcr()
    with vcr.use_cassette('demo_theme.yaml') as cassette, \
            curses_session() as stdscr:

        config = Config()
        if vcr.record_mode == 'once':
            config.load_refresh_token()
        else:
            config.refresh_token = 'mock_refresh_token'

        reddit = praw.Reddit(user_agent='RTV Theme Demo',
                             decode_html_entities=False,
                             disable_update_check=True)
        reddit.config.api_request_delay = 0

        config.history.add('https://api.reddit.com/comments/6llvsl/_/djutc3s')
        config.history.add('http://i.imgur.com/Z9iGKWv.gifv')
        config.history.add('https://www.reddit.com/r/Python/comments/6302cj/rpython_official_job_board/')

        term = Terminal(stdscr, config)
        term.set_theme()
        oauth = OAuthHelper(reddit, term, config)
        oauth.authorize()

        theme_list = ThemeList()

        while True:
            term = Terminal(stdscr, config)
            term.set_theme(theme)
            threads = draw_screen(stdscr, reddit, config, theme, oauth)

            try:
                ch = term.show_notification(theme.display_string)
            except KeyboardInterrupt:
                ch = Terminal.ESCAPE

            for thread, term in threads:
                term.pause_getch = False
                thread.join()

            if vcr.record_mode == 'once':
                break
            else:
                cassette.play_counts = Counter()

            theme_list.reload()

            if ch == curses.KEY_RIGHT:
                theme = theme_list.next(theme)
            elif ch == curses.KEY_LEFT:
                theme = theme_list.previous(theme)
            elif ch == Terminal.ESCAPE:
                break
            else:
                # Force the theme to reload
                theme = theme_list.next(theme)
                theme = theme_list.previous(theme)
"""
Initialize an authenticated instance of PRAW to interact with.

$ python -i initialize_session.py
"""
from rtv.docs import AGENT
from rtv.packages import praw
from rtv.content import RequestHeaderRateLimiter
from rtv.config import Config

config = Config()
config.load_refresh_token()

reddit = praw.Reddit(
    user_agent=AGENT.format(version='test_session'),
    decode_html_entities=False,
    disable_update_check=True,
    timeout=10,  # 10 second request timeout
    handler=RequestHeaderRateLimiter())

reddit.set_oauth_app_info(config['oauth_client_id'],
                          config['oauth_client_secret'],
                          config['oauth_redirect_uri'])
reddit.refresh_access_information(config.refresh_token)

inbox = reddit.get_inbox()
items = [next(inbox) for _ in range(20)]
pass