예제 #1
0
    def begin(self):

        if self.refresh_token:
            self.arguments["refresh_token"] = self.refresh_token
            self.redditInstance = praw.Reddit(**self.arguments)
            try:
                self.redditInstance.auth.scopes()
                return self.redditInstance
            except ResponseException:
                self.arguments["redirect_uri"] = "http://localhost:" + str(
                    self.PORT)
                self.redditInstance = praw.Reddit(**self.arguments)
                reddit, refresh_token = self.getRefreshToken(*self.SCOPES)
        else:
            self.arguments["redirect_uri"] = "http://localhost:" + str(
                self.PORT)
            self.redditInstance = praw.Reddit(**self.arguments)
            reddit, refresh_token = self.getRefreshToken(*self.SCOPES)

        JsonFile(GLOBAL.configDirectory).add(
            {
                "reddit_username": str(reddit.user.me()),
                "reddit": refresh_token
            }, "credentials")

        return self.redditInstance
예제 #2
0
def createLogFile(TITLE):
    """Create a log file with given name
    inside a folder time stampt in its name and
    put given arguments inside \"HEADER\" key
    """

    folderDirectory = GLOBAL.directory / "LOG_FILES" / GLOBAL.RUN_TIME

    logFilename = TITLE.upper() + '.json'

    if not path.exists(folderDirectory):
        makedirs(folderDirectory)

    FILE = JsonFile(folderDirectory / Path(logFilename))
    HEADER = " ".join(sys.argv)
    FILE.add({"HEADER": HEADER})

    return FILE
예제 #3
0
def postFromLog(fileName):
    """Analyze a log file and return a list of dictionaries containing
    submissions
    """
    if Path.is_file(Path(fileName)):
        content = JsonFile(fileName).read()
    else:
        print("File not found")
        sys.exit()

    try:
        del content["HEADER"]
    except KeyError:
        pass

    posts = []

    for post in content:
        if not content[post][-1]['TYPE'] == None:
            posts.append(content[post][-1])

    return posts
예제 #4
0
def main():
    
    if Path("config.json").exists():
        GLOBAL.configDirectory = Path("config.json")
    else:
        if not Path(GLOBAL.defaultConfigDirectory).is_dir():
            os.makedirs(GLOBAL.defaultConfigDirectory)
        GLOBAL.configDirectory = GLOBAL.defaultConfigDirectory  / "config.json"
    try:
        GLOBAL.config = Config(GLOBAL.configDirectory).generate()
    except InvalidJSONFile as exception:
        VanillaPrint(str(exception.__class__.__name__),">>",str(exception))
        VanillaPrint("Resolve it or remove it to proceed")
        input("\nPress enter to quit")
        sys.exit()

    sys.argv = sys.argv + GLOBAL.config["options"].split()

    arguments = Arguments.parse()
    GLOBAL.arguments = arguments

    if arguments.set_filename:
        Config(GLOBAL.configDirectory).setCustomFileName()
        sys.exit()

    if arguments.set_folderpath:
        Config(GLOBAL.configDirectory).setCustomFolderPath()
        sys.exit()

    if arguments.set_default_directory:
        Config(GLOBAL.configDirectory).setDefaultDirectory()
        sys.exit()

    if arguments.set_default_options:
        Config(GLOBAL.configDirectory).setDefaultOptions()
        sys.exit()

    if arguments.use_local_config:
        JsonFile("config.json").add(GLOBAL.config)
        sys.exit()
        
    if arguments.directory:
        GLOBAL.directory = Path(arguments.directory.strip())
    elif "default_directory" in GLOBAL.config and GLOBAL.config["default_directory"] != "":
        GLOBAL.directory = Path(GLOBAL.config["default_directory"].format(time=GLOBAL.RUN_TIME))
    else:
        GLOBAL.directory = Path(input("\ndownload directory: ").strip())

    if arguments.downloaded_posts:
        GLOBAL.downloadedPosts = Store(arguments.downloaded_posts)
    else:
        GLOBAL.downloadedPosts = Store()

    printLogo()
    print("\n"," ".join(sys.argv),"\n",noPrint=True)

    if arguments.log is not None:
        logDir = Path(arguments.log)
        download(postFromLog(logDir))
        sys.exit()


    programMode = ProgramMode(arguments).generate()

    try:
        posts = getPosts(programMode)
    except Exception as exc:
        logging.error(sys.exc_info()[0].__name__,
                      exc_info=full_exc_info(sys.exc_info()))
        print(GLOBAL.log_stream.getvalue(),noPrint=True)
        print(exc)
        sys.exit()

    if posts is None:
        print("I could not find any posts in that URL")
        sys.exit()

    if GLOBAL.arguments.no_download: pass
    else: download(posts)
 def __init__(self, filename):
     self.filename = filename
     self.file = JsonFile(self.filename)
class Config():
    def __init__(self, filename):
        self.filename = filename
        self.file = JsonFile(self.filename)

    def generate(self):
        self._validateCredentials()
        self._readCustomFileName()
        self._readCustomFolderPath()
        self._readDefaultOptions()
        return self.file.read()

    def setCustomFileName(self):
        print(
            """
IMPORTANT: Do not change the filename structure frequently.
           If you did, the program could not find duplicates and
           would download the already downloaded files again.
           This would not create any duplicates in the directory but
           the program would not be as snappy as it should be.

Type a template file name for each post.

You can use SUBREDDIT, REDDITOR, POSTID, TITLE, UPVOTES, FLAIR, DATE in curly braces
The text in curly braces will be replaced with the corresponding property of an each post

For example: {FLAIR}_{SUBREDDIT}_{REDDITOR}

Existing filename template:""", None if "filename" not in self.file.read() else
            self.file.read()["filename"])

        filename = nameCorrector(input(">> ").upper())
        self.file.add({"filename": filename})

    def _readCustomFileName(self):
        content = self.file.read()

        if not "filename" in content:
            self.file.add({"filename": "{REDDITOR}_{TITLE}_{POSTID}"})
        content = self.file.read()

        if not "{POSTID}" in content["filename"]:
            self.file.add({"filename": content["filename"] + "_{POSTID}"})

    def setCustomFolderPath(self):
        print(
            """
Type a folder structure (generic folder path)

Use slash or DOUBLE backslash to separate folders

You can use SUBREDDIT, REDDITOR, POSTID, TITLE, UPVOTES, FLAIR, DATE in curly braces
The text in curly braces will be replaced with the corresponding property of an each post

For example: {REDDITOR}\{SUBREDDIT}\{FLAIR}

Existing folder structure""", None if "folderpath" not in self.file.read() else
            self.file.read()["folderpath"])

        folderpath = nameCorrector(input(">> ").strip("\\").strip("/").upper())

        self.file.add({"folderpath": folderpath})

    def _readCustomFolderPath(self, path=None):
        content = self.file.read()
        if not "folderpath" in content:
            self.file.add({"folderpath": "{SUBREDDIT}"})

    def setDefaultOptions(self):
        print(
            """
Type options to be used everytime script runs

For example: --no-dupes --quit --limit 100 --skip youtube.com

Existing default options:""", None if "options" not in self.file.read() else
            self.file.read()["options"])

        options = input(">> ").strip("")

        self.file.add({"options": options})

    def _readDefaultOptions(self, path=None):
        content = self.file.read()
        if not "options" in content:
            self.file.add({"options": ""})

    def _validateCredentials(self):
        """Read credentials from config.json file"""

        try:
            content = self.file.read()["credentials"]
        except:
            self.file.add({"credentials": {}})
            content = self.file.read()["credentials"]

        if "reddit" in content and len(content["reddit"]) != 0:
            pass
        else:
            Reddit().begin()

        print()

    def setDefaultDirectory(self):
        print("""Set a default directory to use in case no directory is given
Leave blank to reset it. You can use {time} in folder names to use to timestamp it
For example: D:/archive/BDFR_{time}
""")
        print(
            "Current default directory:",
            self.file.read()["default_directory"]
            if "default_directory" in self.file.read() else "")
        self.file.add({"default_directory": input(">> ")})
예제 #7
0
class Config():
    def __init__(self, filename):
        self.filename = filename
        self.file = JsonFile(self.filename)

    def generate(self):
        self._validateCredentials()
        self._readCustomFileName()
        self._readCustomFolderPath()
        self._readDefaultOptions()
        return self.file.read()

    def setCustomFileName(self):
        print(
            """
IMPORTANT: Do not change the filename structure frequently.
           If you did, the program could not find duplicates and
           would download the already downloaded files again.
           This would not create any duplicates in the directory but
           the program would not be as snappy as it should be.

Type a template file name for each post.

You can use SUBREDDIT, REDDITOR, POSTID, TITLE, UPVOTES, FLAIR, DATE in curly braces
The text in curly braces will be replaced with the corresponding property of an each post

For example: {FLAIR}_{SUBREDDIT}_{REDDITOR}

Existing filename template:""", None if "filename" not in self.file.read() else
            self.file.read()["filename"])

        filename = input(">> ").upper()
        self.file.add({"filename": filename})

    def _readCustomFileName(self):
        content = self.file.read()

        if not "filename" in content:
            self.file.add({"filename": "{REDDITOR}_{TITLE}_{POSTID}"})
        content = self.file.read()

        if not "{POSTID}" in content["filename"]:
            self.file.add({"filename": content["filename"] + "_{POSTID}"})

    def setCustomFolderPath(self):
        print(
            """
Type a folder structure (generic folder path)

Use slash or DOUBLE backslash to separate folders

You can use SUBREDDIT, REDDITOR, POSTID, TITLE, UPVOTES, FLAIR, DATE in curly braces
The text in curly braces will be replaced with the corresponding property of an each post

For example: {REDDITOR}/{SUBREDDIT}/{FLAIR}

Existing folder structure""", None if "folderpath" not in self.file.read() else
            self.file.read()["folderpath"])

        folderpath = input(">> ").strip("\\").strip("/").upper()

        self.file.add({"folderpath": folderpath})

    def _readCustomFolderPath(self, path=None):
        content = self.file.read()
        if not "folderpath" in content:
            self.file.add({"folderpath": "{SUBREDDIT}"})

    def setDefaultOptions(self):
        print(
            """
Type options to be used everytime script runs

For example: --no-dupes --quit --limit 100 --skip youtube.com

Existing default options:""", None if "options" not in self.file.read() else
            self.file.read()["options"])

        options = input(">> ").strip("")

        self.file.add({"options": options})

    def _readDefaultOptions(self, path=None):
        content = self.file.read()
        if not "options" in content:
            self.file.add({"options": ""})

    def _validateCredentials(self):
        """Read credentials from config.json file"""

        keys = ['imgur_client_id', 'imgur_client_secret']
        try:
            content = self.file.read()["credentials"]
        except:
            self.file.add({"credentials": {}})
            content = self.file.read()["credentials"]

        if "reddit" in content and len(content["reddit"]) != 0:
            pass
        else:
            Reddit().begin()

        if not all(content.get(key, False) for key in keys):
            print(
                "---Setting up the Imgur API---\n\n" \
                "Go to this URL and fill the form:\n" \
                "https://api.imgur.com/oauth2/addclient\n" \
                "Then, enter the client id and client secret here\n" \
                "Press Enter to open the link in the browser"
            )
            input()
            webbrowser.open("https://api.imgur.com/oauth2/addclient", new=2)

        for key in keys:
            try:
                if content[key] == "":
                    raise KeyError
            except KeyError:
                self.file.add({key: input("\t" + key + ": ")}, "credentials")
        print()

    def setDefaultDirectory(self):
        print("""Set a default directory to use in case no directory is given
Leave blank to reset it. You can use {time} in foler names to use to timestamp it
For example: D:/archive/BDFR_{time}
""")
        print(
            "Current default directory:",
            self.file.read()["default_directory"]
            if "default_directory" in self.file.read() else "")
        self.file.add({"default_directory": input(">> ")})