commit 589f780911731cad0d42af679693376138a7af61
parent 2cdbc167a45fdf63c4a38108f5b6b48857f9d350
Author: archiveanon <>
Date: Fri, 10 Jan 2025 21:06:03 +0000
Move interactive tool to separate file
Diffstat:
M | src/gofile/api.py | | | 89 | ------------------------------------------------------------------------------- |
A | src/gofile/cli.py | | | 95 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 95 insertions(+), 89 deletions(-)
diff --git a/src/gofile/api.py b/src/gofile/api.py
@@ -1,11 +1,9 @@
#!/usr/bin/python3
-import argparse
import asyncio
import collections
import enum
import io
-import os
import pathlib
import random
from typing import Any, AsyncIterator, Generic, Iterator, Optional, TypeVar
@@ -177,90 +175,3 @@ async def upload_multiple(
uploads = [upload_single(file, token, folder_id, server) for file in other_files]
for upload in asyncio.as_completed(uploads):
yield await upload
-
-
-async def main():
- # you may save and reuse a guest token
- # there's no clear indicator on whether or not it expires
- # there may be duplicate filenames in a folder
-
- parser = argparse.ArgumentParser()
- parser.add_argument("path", type=pathlib.Path, help="Path to single file or folder")
-
- parser.add_argument(
- "--token", type=str, help="Token for user upload, or empty for initial anonymous upload"
- )
- parser.add_argument("--folder-id", type=str, help="Folder to upload to")
- parser.add_argument(
- "--fast-link",
- action="store_true",
- help="Uploads are queued in ascending size to get a download link as fast as possible",
- )
- parser.add_argument(
- "--upload-count-threshold",
- type=int,
- help="Bail if there are more than this number of files present (ensuring you don't accidentally something important)",
- default=10,
- )
- parser.add_argument(
- "-r",
- "--recursive",
- action="store_true",
- help="Get files from subdirectories",
- default=False,
- )
-
- args = parser.parse_args()
-
- if not args.path.exists():
- print(f"Path '{args.path}' does not exist")
- return
-
- files = []
-
- if args.path.is_dir():
- files = list(args.path.glob("*"))
- if args.fast_link:
- # sort by ascending size so we can produce a link as fast as possible
- files = sorted(files, key=lambda x: os.stat(x).st_size)
- files = [f for f in files if f.name not in ("desktop.ini",)]
- if not files:
- print(f"Directory '{args.path}' is empty")
- return
- if args.upload_count_threshold and args.upload_count_threshold < len(files):
- print(
- f"Directory '{args.path}' has {len(files)} files; only expecting "
- f"{args.upload_count_threshold}. Pass --upload-count-threshold to confirm "
- "that you really did intend to upload this directory."
- )
- return
- elif args.path.is_file():
- files = [args.path]
-
- 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)
-
- print(f"- Download URL: {first_upload.result.download_page}")
- print(f"- Folder ID: {first_upload.result.parent_folder}")
-
- if first_upload.result.guest_token:
- print(f"- Guest token: {first_upload.result.guest_token}")
-
- # let the rest of the uploads process
- try:
- async for upload in uploads:
- pass
- except KeyboardInterrupt:
- # reprint the information in case we bail out
- print()
- print("Cancelling upload. Resume with the following options:")
- print(f"- Folder ID: {first_upload.result.parent_folder}")
- print(f"- Token: {first_upload.result.guest_token or args.token}")
-
-
-if __name__ == "__main__":
- asyncio.run(main())
diff --git a/src/gofile/cli.py b/src/gofile/cli.py
@@ -0,0 +1,95 @@
+#!/usr/bin/python3
+
+import argparse
+import asyncio
+import os
+import pathlib
+
+from . import api
+
+
+async def main():
+ # you may save and reuse a guest token
+ # there's no clear indicator on whether or not it expires
+ # there may be duplicate filenames in a folder
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument("path", type=pathlib.Path, help="Path to single file or folder")
+
+ parser.add_argument(
+ "--token", type=str, help="Token for user upload, or empty for initial anonymous upload"
+ )
+ parser.add_argument("--folder-id", type=str, help="Folder to upload to")
+ parser.add_argument(
+ "--fast-link",
+ action="store_true",
+ help="Uploads are queued in ascending size to get a download link as fast as possible",
+ )
+ parser.add_argument(
+ "--upload-count-threshold",
+ type=int,
+ help="Bail if there are more than this number of files present (ensuring you don't accidentally something important)",
+ default=10,
+ )
+ parser.add_argument(
+ "-r",
+ "--recursive",
+ action="store_true",
+ help="Get files from subdirectories",
+ default=False,
+ )
+
+ args = parser.parse_args()
+
+ if not args.path.exists():
+ print(f"Path '{args.path}' does not exist")
+ return
+
+ files = []
+
+ if args.path.is_dir():
+ files = list(args.path.glob("*"))
+ if args.fast_link:
+ # sort by ascending size so we can produce a link as fast as possible
+ files = sorted(files, key=lambda x: os.stat(x).st_size)
+ files = [f for f in files if f.name not in ("desktop.ini",)]
+ if not files:
+ print(f"Directory '{args.path}' is empty")
+ return
+ if args.upload_count_threshold and args.upload_count_threshold < len(files):
+ print(
+ f"Directory '{args.path}' has {len(files)} files; only expecting "
+ f"{args.upload_count_threshold}. Pass --upload-count-threshold to confirm "
+ "that you really did intend to upload this directory."
+ )
+ return
+ elif args.path.is_file():
+ files = [args.path]
+
+ uploads = api.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)
+
+ print(f"- Download URL: {first_upload.result.download_page}")
+ print(f"- Folder ID: {first_upload.result.parent_folder}")
+
+ if first_upload.result.guest_token:
+ print(f"- Guest token: {first_upload.result.guest_token}")
+
+ # let the rest of the uploads process
+ try:
+ async for upload in uploads:
+ pass
+ except KeyboardInterrupt:
+ # reprint the information in case we bail out
+ print()
+ print("Cancelling upload. Resume with the following options:")
+ print(f"- Folder ID: {first_upload.result.parent_folder}")
+ print(f"- Token: {first_upload.result.guest_token or args.token}")
+
+
+if __name__ == "__main__":
+ asyncio.run(main())