Skip to content

dsdrive_api

TWTom edited this page Jan 6, 2024 · 5 revisions

dsdrive_api

HookTool Objects

class HookTool()

A tool to send data to multiple webhooks

Attributes:

  • hooks list - A list of webhook URLs
  • index int - The index of the current webhook URL

Methods:

  • get_hook - Get the current webhook URL
  • send - Send data to the current webhook URL
  • get - Send a GET request to a URL, handling rate limits and nothing

get_hook

def get_hook()

Get the current webhook URL

send

def send(*args, **kwargs)

Send data to the current webhook URL, handling rate limits

Arguments:

  • *args - Arguments to be passed to requests.post
  • **kwargs - Keyword arguments to be passed to requests.post

Returns:

  • requests.Response - The response of the request

get

def get(*args, **kwargs)

Send a GET request to a URL, handling rate limits and nothing

Arguments:

  • *args - Arguments to be passed to requests.get
  • **kwargs - Keyword arguments to be passed to requests.get

Returns:

  • requests.Response - The response of the request

DSFile Objects

class DSFile(BytesIO)

A file-like object that can be used to read and write files on DSdrive

Attributes:

  • path str - The path of the file
  • dsdrive DSdriveApi - The DSdriveApi object
  • mode str - The mode of the file
  • _read bool - Whether the file is readable
  • _write bool - Whether the file is writable

Methods:

  • readable - Whether the file is readable
  • writable - Whether the file is writable
  • read - Read the file
  • write - Write to the file
  • close - Close the file

__init__

def __init__(path, dsdrive, mode="r", zero_size=False)

Create a DSFile object

Arguments:

  • path str - The path of the file
  • dsdrive DSdriveApi - The DSdriveApi object
  • mode str - The mode of the file
  • zero_size bool - Whether the file is empty

read

def read(size=-1) -> bytes

Read the file

Arguments:

  • size int - The number of bytes to read. If -1, read the whole file

Returns:

  • bytes - The bytes read

Raises:

  • OSError - If the file is not readable

write

def write(b: bytes) -> int

Read the file

Arguments:

  • b bytes - The bytes to write

Returns:

  • int - The number of bytes written

Raises:

  • OSError - If the file is not writable

close

def close()

Close the file, and send the file to DSdrive if the file is writable

DSdriveApi Objects

class DSdriveApi()

A wrapper class for DSdriveApiWebhook and DSdriveApiBot

__init__

def __init__(*args, **kwargs)

Create a DSdriveApi object, where the backend api is either DSdriveApiWebhook or DSdriveApiBot, depending on the value of use_bot

DSdriveApiBase Objects

class DSdriveApiBase()

A base class for DSdrive API implementations

encrypt

def encrypt(data: bytes)

Encrypt data

Arguments:

  • data bytes - The data to encrypt
  • encryption_func function - The encryption function to use

Returns:

  • encrypted bytes - The encrypted data

decrypt

def decrypt(data: bytes)

Decrypt data

Arguments:

  • data bytes - The data to decrypt
  • encryption_func function - The decryption function to use

Returns:

  • decrypted bytes - The decrypted data

path_splitter

def path_splitter(path: str)

Split a path into a list

Arguments:

  • path str - The path to split

Returns:

  • filelist list - The list of directories

open_binary

def open_binary(path: str, mode: str = "r")

Open a file

Arguments:

  • path str - The path of the file
  • mode str - The mode of the file

Returns:

  • IO DSFile - The file-like object

DSdriveApiWebhook Objects

class DSdriveApiWebhook(DSdriveApiBase)

A class to interact with Discord's file storage system

Attributes:

  • db pymongo.database.Database - The database object
  • hook HookTool - The HookTool object
  • root_id bson.objectid.ObjectId - The ID of the root directory

Methods:

  • clear - Clear the database, very dangerous
  • path_splitter - Split a path into a list of directories
  • makedirs - Create directories
  • find - Find a path
  • send_file - Send a file to DSdrive
  • open_binary - Open a file
  • get_file_urls - Get the URLs of a file
  • download_file - Download a file
  • list_dir - List a directory
  • remove_file - Remove a file
  • remove_dir - Remove a directory
  • remove_tree - Remove a tree
  • get_info - Get the info of a file or directory
  • set_info - Set the info of a file or directory

__init__

def __init__(url: str,
             hook: HookTool,
             url_expire_policy: BaseExpirePolicy = ApiExpirePolicy,
             token: Optional[str] = None,
             key: Union[str, bytes] = "despacito") -> None

Create a DSdriveApi object, creates the root directory if it doesn't exist

Arguments:

  • url str - The URL of the MongoDB database
  • hook HookTool - The HookTool object

clear

def clear()

Clear the database safely

makedirs

def makedirs(paths: list, allow_many: bool = False, exist_ok: bool = False)

Create directories

Arguments:

  • paths list - The list of directories to create
  • allow_many bool - Whether to allow creating multiple directories
  • exist_ok bool - Whether to allow creating directories that already exist

Returns:

  • code int - An error code, or 0 if successful
  • parent_id Union[ObjectID, None] - The ID of the last directory created

find

def find(paths: str, return_obj: bool = False)

Find a path

Arguments:

  • paths list - The list of directories to find
  • return_obj bool - Whether to return the object found or the ID of the object found

Returns:

  • code int - An error code, or 0 if successful
  • found_id Union[ObjectID, None] - The ID of the last position found

send_file

def send_file(path: str, file_obj: Union[str, BytesIO, DSFile, None] = None)

Send a file to Didcord

Arguments:

  • path str - The path to send the file to
  • file_obj Union[str, BytesIO, DSFile] - The file to send

get_file_urls

def get_file_urls(path: str)

Get the URLs of a file

Arguments:

  • path str - The path of the file

Returns:

  • filelist list - The list of URLs of the file

download_file

def download_file(path_src: str, path_dst: Optional[str] = None)

Download a file from Discord to the local filesystem

Arguments:

  • path_src str - The path of the file on Discord
  • path_dst str - The path of the file on the local filesystem

list_dir

def list_dir(path: str)

List a directory

Arguments:

  • path str - The path of the directory

Returns:

  • code int - an error code, or 0 if successful
  • filelist Union[Iterable, None] - A list of files and directories, or None if an error occured

remove_file

def remove_file(path: str)

Remove a file

Arguments:

  • path str - The path of the file

Returns:

  • code int - An error code, or 0 if successful

remove_dir

def remove_dir(path: str)

Remove a directory

Arguments:

  • path str - The path of the directory

Returns:

  • code int - An error code, or 0 if successful

remove_tree

def remove_tree(path: str)

Remove a tree, not implemented yet

Arguments:

  • path str - The path of the tree

Returns:

  • code int - An error code, or 0 if successful

rename

def rename(path_src: str,
           path_dst: str,
           overwrite: bool = False,
           create_dirs: bool = False,
           preserve_timestamps: bool = False)

Rename a file or directory

Arguments:

  • path_src str - The path of the file or directory
  • path_dst str - The new path of the file or directory

Returns:

  • code int - An error code, or 0 if successful

copy

def copy(path_src: str,
         path_dst: str,
         overwrite: bool = False,
         create_dirs: bool = False,
         preserve_timestamps: bool = False)

Copy a file or directory

Arguments:

  • path_src str - The path of the file or directory
  • path_dst str - The new path of the file or directory

Returns:

  • code int - An error code, or 0 if successful

get_info

def get_info(path: str)

Get the info of a file or directory

Arguments:

  • path str - The path of the file or directory

Returns:

  • code int - An error code, or 0 if successful
  • info dict - The info of the file or directory

set_info

def set_info(path: str, info: dict)

Set the info of a file or directory

Arguments:

  • path str - The path of the file or directory
  • info dict - The info you want to set

Returns:

  • code int - An error code, or 0 if successful