예제 #1
0
def get_previous() -> (str, str, str):
    """
    Gets all the data from the previous run
    Returns
    -------

    """
    try:
        comments = dh.fetch("Comments", "id")
        posts = dh.fetch("Posts", "id")
        blacklist = dh.fetch("Blacklist", "user")
        blacklist.append("useful_bot")
        retrieved_mentions = dh.fetch("replied_mentions", "id")
        message_responses = dh.fetch("message_responses", "*")
        comments_triggers = dh.fetch("comment_responses", "*")
        post_triggers = dh.fetch("post_responses", "*")
    except Exception as err:
        logger.error("Error previous info from database: %s", err)
        stop_bot()
    return (
        comments,
        posts,
        blacklist,
        retrieved_mentions,
        message_responses,
        comments_triggers,
        post_triggers,
    )
예제 #2
0
 def fetch_config(self, find) -> str:  # Fetches the values needed to run the bot
     """
     Fetches the value `find` and returns the string for the needed value
     Parameters
     ----------
     find: str
         configuration value needed
     Returns
     -------
         The value that is mapped to find
     """
     try:
         values = dh.fetch("configurations", "value")
         ids = dh.fetch("configurations", "id")
         return values[ids.index(find)]
     # The exception is called if there are no values in configurations table
     except ValueError:
         value = input("Enter " + find + ": ")
         dh.insert("configurations", [[find, value]])
         return value
     except Exception as err:
         self.logger.error("There was an error retrieving credentials: %s ", err)
         main.stop_bot()
예제 #3
0
def search() -> None:
    """
    Find all data in the tables
    """
    tables = dh.table_fetch()
    table = input("Available tables are {}. \nEnter the table: ".format(tables))
    if table in tables:
        print("Retrieving {}".format(table))
        retrieved = dh.fetch(table, "*")
        if len(retrieved) == 0:
            print("The table was empty")
        else:
            for _, row in enumerate(retrieved):
                print(retrieved[row], "\n")
    else:
        print("Enter a valid table")
예제 #4
0
def start() -> praw.Reddit():
    """
    Gets the Reddit() class based on botinfo or stored into in the database
    Returns
    -------
        The Reddit class that is used for operations
    """
    try:
        reddit_client = praw.Reddit(
            client_id=botinfo.client_id,
            client_secret=botinfo.client_secret,
            password=botinfo.password,
            username=botinfo.username,
            user_agent=botinfo.user_agent,
        )
        reddit_client.user.me(
        )  # Verify log in, will raise exception if log in failed.
        logger.info("Successfully logged in")
        return reddit_client
    except AttributeError:  # AttributeError will occur if the values of botinfo do not exist
        try:  # Attempts to import credentials from the configurations table
            if dh.fetch("configurations", "remember") == "yes":
                try:
                    reddit_client = praw.Reddit(
                        client_id=dh.fetch("configurations", "client_id"),
                        client_secret=dh.fetch("configurations",
                                               "client_secret"),
                        password=dh.fetch("configurations", "password"),
                        username=dh.fetch("configurations", "username"),
                        user_agent=dh.fetch("configurations", "user_agent"),
                    )
                    reddit_client.user.me()
                    logger.info("Successfully logged in using the database")
                    return reddit_client
                except Exception as err:
                    logger.error("Error when trying to import credentials: %s",
                                 err)
                    stop_bot()
        except OperationalError:  # OperationalError will occur when nothing exists in the table
            logger.error(
                "There was an error trying to import the credentials."
                "This either means that it was never setup"
                " or there was actually an error.\n"
                "If you want to to be able to import credentials then add them from the cli"
            )
            stop_bot()
        except Exception as err:
            logger.error(
                "There was an error when dealing with credentials: %s", err)
            stop_bot()
예제 #5
0
    def response_delete(self) -> None:
        """
        Deletes unwanted responses from the database
        Returns
        -------

        """
        choice = input(
            "Enter the response type you want to remove: Comment, Post, or Message\n"
        ).lower()
        if choice.startswith("c"):
            table = "comment_responses"
        elif choice.startswith("p"):
            table = "post_responses"
        elif choice.startswith("m"):
            table = "message_responses"
        else:
            print("No valid table selected")
            self.run()
        retrieved = dh.fetch(table, "*")
        for item, _ in enumerate(retrieved):
            print(item + 1, retrieved[item], "\n")
        try:
            choice = int(input("Enter the number of the response you wish to delete: "))
            print(
                "You have selected ",
                retrieved[choice - 1],
                "\n" + "Enter (y)es to delete: ",
            )
        except (ValueError, IndexError):
            print("Enter a valid number")
            self.run()
        confirm = input().lower()
        if confirm.startswith("y"):
            dh.delete(
                "message_responses",
                "keyword",
                "'{keyword}'".format(keyword=retrieved[choice - 1][0]),
            )
            print("Deleted")