Esempio n. 1
0
def interact(settings):
    """ Used only after base setup to simplify managing Sources and their Filters. Can be run at any time. """
    print(
        "Source wizard launched.\n"
        'ABOUT: "Sources" are the places this downloader pulls Submissions or Comments from.\n'
        "\tThere are many different places on Reddit that you may want to pull posts from.\n"
        "\tThis wizard is built to help you easily manage them.\n")
    while True:
        su.print_color(Fore.GREEN, '=== Config Wizard Home ===')
        opt = console.prompt_list(
            'What would you like to do?',
            [('Edit my saved account information', 'account'),
             ('Add a new Source', 'add'),
             ('Edit my current Sources (%s)' % len(settings.get_sources()),
              'edit_source'), ("Save & Exit", "exit")])
        print('\n\n')

        if opt == 'exit':
            settings.save()
            print('Wizard completed.')
            sys.exit(0)

        if opt == 'add':
            _add_source(settings)

        if opt == 'edit_source':
            _edit_sources(settings)

        if opt == 'account':
            _edit_account(settings)

        print('\n\n')
Esempio n. 2
0
def login():
    global _credentials, _user, _reddit
    if not _credentials:
        raise ConnectionError('Credentials not set!')

    stringutil.print_color(Fore.LIGHTYELLOW_EX, "Authenticating via OAuth...")
    _reddit = praw.Reddit(**_credentials)
    _user = _reddit.user.me()
    stringutil.print_color(Fore.LIGHTYELLOW_EX,
                           "Authenticated as [%s]\n" % _user.name)
	def run(self):
		""" Threaded loading of elements. """
		for source in self.sources:
			stringutil.print_color(Fore.GREEN, 'Downloading from Source: %s' % source.get_alias())
			for r in source.get_elements():
				r.set_source(source)
				self._queue.put(r)

				# Extra tracking stuff below:
				with self._c_lock:
					self._total_count+= 1
				if self._testing_cache is not None:
					self._testing_cache.append(r)
		#print("Element loading complete.\n")
		self._running = False
Esempio n. 4
0
def prompt_list(prompt, options, allow_none=False):
	""" Prompts for the user to select an option from the list, and returns the index.
	 	If *options* is an array of tuples, they are prompted with [0], and [1] is returned.
	"""
	if len(options) == 0:
		return None
	stringutil.print_color(Fore.CYAN, prompt)
	is_tuple = isinstance(options[0], tuple)
	if not is_tuple:
		options = [(o, o) for o in options]
	if allow_none:
		options.append(('Cancel', None))
	for idx, opt in enumerate(options):
		print("\t%s: %s" % (idx+1, opt[0]) )
	select = number("Choose an option", 1, len(options), round_val=True) - 1
	return options[select][1]
    def run(self):
        """ The editor screen for a specific Source. """
        while True:
            print('\n\n\n')
            su.print_color(
                su.Fore.GREEN, 'Editing Source: "%s" -> %s' %
                (self.source.get_alias(), self.source.get_config_summary()))
            filters = self.source.get_filters()
            if len(filters) > 0:
                for f in filters:
                    print('\t-%s' % f)

            choice = console.prompt_list(
                'What would you like to do with this Source?',
                [('Edit this Source', 'edit'), ('Rename', 'rename'),
                 ('Delete this Source', 'delete'),
                 ('Add a Filter', 'add_filter'),
                 ('Remove a Filter', 'remove_filter'), ('Nothing', 'exit')])
            print('\n')

            if choice == 'edit':
                self._edit_source()

            if choice == 'rename':
                self._rename()

            if choice == 'delete':
                if self._delete():
                    break

            if choice == 'add_filter':
                self._add_filter()

            if choice == 'remove_filter':
                self._remove_filter()

            if choice == 'exit':
                break
Esempio n. 6
0
import colorama
from colorama import Fore
from classes.util.settings import Settings
from classes.processing.elementprocessor import ElementProcessor
from classes.reddit.redditloader import RedditLoader
import classes.util.manifest as manifest
import classes.reddit.reddit as reddit
import classes.wizards.wizard as wizard
from classes.util import stringutil

colorama.init(convert=True)

stringutil.print_color(
    Fore.GREEN, """
====================================
    Reddit Media Downloader %s
====================================
    (By ShadowMoose @ Github)
""" % __version__)


class Scraper(object):
    def __init__(self, settings_file, c_settings=None):
        if not args.test:
            self.settings = Settings(settings_file,
                                     can_save=(c_settings is None),
                                     can_load=(not args.test))
        else:
            self.settings = Settings('./tests/test-settings.json',
                                     can_save=False,
                                     can_load=True)
Esempio n. 7
0
def pause():
	""" Prompts the User to press any button to continue. """
	stringutil.print_color(Fore.GREEN, '[Press Enter to continue]')
	input()
Esempio n. 8
0
def col_input(prompt, color=Fore.LIGHTYELLOW_EX):
	""" Prints a colorized input prompt. """
	stringutil.print_color(color, prompt, end='')
	return input()
Esempio n. 9
0
def run(settings_file='settings.json'):
    client_setup = {'secret': None, 'id': None}
    user_setup = {'username': None, 'password': None}

    su.print_color(Fore.GREEN,
                   "Welcome to the Reddit Media Downloader automatic setup!")
    su.print_color(
        Fore.YELLOW,
        '\tThis program will help you automatically set everything up in a few steps.'
    )

    su.print_color(Fore.CYAN,
                   "To start, we need to register this script with reddit.")
    su.print_color(
        Fore.CYAN,
        "I'll walk you through this, & the visual guide can be found here: https://goo.gl/ox6PmA"
    )
    su.print_color(
        Fore.YELLOW,
        "\tReddit requires that any program accessing it is registered.")
    su.print_color(
        Fore.YELLOW,
        "\tFor your account's security, you need to do this yourself.")

    su.print_color(Fore.CYAN,
                   "Open https://www.reddit.com/prefs/apps, and sign in.")
    if console.confirm(
            '\tIs it okay to open the URL in your browser for you?'):
        webbrowser.open('https://www.reddit.com/prefs/apps')

    su.print_color(
        Fore.CYAN,
        "Once you're signed in, scroll down and click on 'Create Application'."
    )
    print()
    su.print_color(
        Fore.CYAN,
        "Give it a name to remind you what it's for in the future, and select the 'Script' option.\n"
    )
    su.print_color(Fore.YELLOW,
                   "The description can be anything to remind yourself.")
    su.print_color(
        Fore.YELLOW,
        "The URL and URI fields don't matter, so they can be set to 'http://localhost', or a made-up URL."
    )
    print('\n')
    su.print_color(
        Fore.YELLOW,
        "Go ahead and click 'Create App' after you've filled this all in, then press Enter here."
    )
    console.pause()

    su.print_color(
        Fore.CYAN,
        "Now that you've registered your script, scroll to it on the page and get its ID (the random text under 'personal use script' on top of its box)"
    )
    su.print_color(Fore.YELLOW, "\t(This is your client_id)")
    client_setup['id'] = console.string("Paste or type your client ID here")

    su.print_color(Fore.CYAN, "\nAlso grab the generated 'Secret'.")
    su.print_color(Fore.YELLOW, "\t(This is your client_secret)")
    client_setup['secret'] = console.string(
        "Paste or type your client Secret here")

    su.print_color(
        Fore.GREEN,
        "Excellent!\nNow, finally, this script will need the username and password of the account that registered this script."
    )
    su.print_color(
        Fore.GREEN,
        "\tYour credentials will be stored locally, and never sent anywhere - don't worry;"
    )
    su.print_color(
        Fore.GREEN,
        "\tThe whole point of the last few steps was to keep your information safe!"
    )

    user_setup['username'] = console.string('Enter your username')
    user_setup['password'] = console.string('Enter your password')
    print('\n\n')
    su.print_color(
        Fore.CYAN,
        "Great job!\nThe script is now going to attempt to authenticate using the provided information."
    )
    su.print_color(
        Fore.CYAN,
        "\tThis may take a few seconds to process. Just hold on a minute...\n\n"
    )

    try:
        reddit = praw.Reddit(client_id=client_setup['id'],
                             client_secret=client_setup['secret'],
                             password=user_setup['password'],
                             user_agent='RSM-Authenticator',
                             username=user_setup['username'])
        user = reddit.user.me()

        su.print_color(Fore.GREEN, "Authenticated as [%s]\n" % user.name)
        import random
        settings = Settings(
            settings_file,
            can_load=False)  # Skip loading and just build from defaults.
        settings.set(
            'auth', {
                "client_id": client_setup['id'],
                "client_secret": client_setup['secret'],
                "password": user_setup['password'],
                "user_agent": "RMD-%s" % random.random(),
                "username": user_setup['username']
            })
        settings.save()

        su.print_color(
            Fore.GREEN, "Well done, it all works!\n"
            "Your setup is complete!\n"
            "By default, the program has been configured to scan submissions and comments you've upvoted/saved."
        )
        if console.confirm('Would you like to set up some other sources?'):
            interact(settings)
        else:
            su.print_color(
                Fore.GREEN,
                "Everything's good to go, then! Just relaunch the script to start downloading!"
            )
            sys.exit(0)
    except prawcore.exceptions.ResponseException:
        su.print_color(Fore.RED, "There was an error authenticating you.")
        su.print_color(Fore.RED,
                       "Edit or delete the 'Settings.ini' file just created,")
        su.print_color(
            Fore.RED,
            "then retry these steps and assure you've gotten everything correct."
        )
        sys.exit(1)