gofile

Module and tool to upload files to gofile.io
git clone https://code.alwayswait.ing/gofile.git
Log | Files | Refs

commit c16efd480dc3aaceaa73c23ff3d27d680f78d524
parent 900f403fca939187b71272955b2d65847b0921be
Author: archiveanon <>
Date:   Wed,  8 Jan 2025 01:10:20 +0000

Decouple uploading from tqdm

tqdm will be implemented as an extra as part of interactive use
later.

Diffstat:
Msrc/gofile/api.py | 67++++++++++++++++++++++---------------------------------------------
1 file changed, 22 insertions(+), 45 deletions(-)

diff --git a/src/gofile/api.py b/src/gofile/api.py @@ -4,22 +4,16 @@ import argparse import asyncio import collections import enum +import io import os import pathlib import random -import textwrap -from typing import AsyncIterator, Generic, Iterator, Optional, TypeVar +from typing import Any, AsyncIterator, Generic, Iterator, Optional, TypeVar # normally I'd prefer using standard modules but streaming POST data is really important import httpx import msgspec -try: - import tqdm - import tqdm.utils -except ImportError: - tqdm = None - T = TypeVar("T") GofileUpload = collections.namedtuple("GofileUpload", ["file", "result"]) @@ -120,55 +114,36 @@ async def get_upload_server() -> GofileServerResult: async def upload_single( - file: pathlib.Path, + file: io.FileIO, token: Optional[str] = None, folder_id: Optional[str] = None, server: Optional[str] = None, ) -> GofileUpload: # we return a GofileUpload instead of a GofileUploadResult so there's consistency between upload_single / upload_multiple - if not file.exists() or not file.is_file(): - raise ValueError(f"Attempting to upload missing or non-file '{file}'") if not server: upload_server_result = await get_upload_server() server = upload_server_result.server # automatically shorten long file names in small terminals (e.g. split panes) - termsize = os.get_terminal_size() - short_file_name = textwrap.shorten( - file.name, width=int(termsize.columns * 0.6), placeholder=f"…{file.suffix}" - ) - - file_size = file.stat().st_size while True: try: - with tqdm.tqdm( - desc=short_file_name, - total=file_size, - unit="B", - unit_scale=True, - unit_divisor=1024, - dynamic_ncols=True, - ) as progress: - post_data = { - "file": ( - file.name, - tqdm.utils.CallbackIOWrapper(progress.update, file.open("rb"), "read"), - "application/octet-stream", - ), - } - - if token: - post_data["token"] = token - if folder_id: - post_data["folderId"] = folder_id - - upload_result = await _gofile_api_post( - f"https://{server}.gofile.io/contents/uploadfile", - files=post_data, - type=GofileUploadResult, - ) - break + filepath = pathlib.Path(str(file.name)) + post_data: dict[str, Any] = { + "file": (filepath.name, file, "application/octet-stream"), + } + + if token: + post_data["token"] = token + if folder_id: + post_data["folderId"] = folder_id + + upload_result = await _gofile_api_post( + f"https://{server}.gofile.io/contents/uploadfile", + files=post_data, + type=GofileUploadResult, + ) + break except httpx.HTTPError as e: print(e) pass @@ -257,7 +232,9 @@ async def main(): elif args.path.is_file(): files = [args.path] - uploads = upload_multiple(files, token=args.token, folder_id=args.folder_id) + uploads = upload_multiple( + (f.open("rb") for f in files), token=args.token, folder_id=args.folder_id + ) # force generator to evaluate and yield the first result first_upload = await anext(uploads)