Skip to content

Commit 82412d5

Browse files
authored
Merge pull request #90 from nstrydom2/dev
Adds missing changes to changelog and handles duplicate downloads
2 parents 43ff66b + d5d0435 commit 82412d5

3 files changed

Lines changed: 34 additions & 26 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
## Version 1.0.0 (2023-7-18)
44

5-
- drops support for Python 3.7 and makes 3.8 the new baseline for further development
6-
- updates all dependencies
7-
- adds `size_readable` property to `ParseResponse`
8-
- improves the testing methodology of the library
5+
- drops support for Python 3.7 and makes 3.8 the new baseline for further development (PR #81)
6+
- updates all dependencies (PR #81, #84)
7+
- adds `size_readable` property to `ParseResponse` (PR #80)
8+
- improves the testing methodology of the library (PR #77)
9+
- fixes a bug in which the config directory was set incorrectly on FreeBSD (PR #85)
10+
- refactors and future proofs CLI code (PR #89)
911

1012
## Version 0.2.7 (2021-9-12)
1113

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ from anonfile import AnonFile
7272

7373
anon = AnonFile()
7474

75-
# upload a file and enable progressbar terminal feedback
75+
# upload a file and enable progressbar terminal feedback (off by default)
7676
upload = anon.upload('/home/guest/jims_paperwork.doc', progressbar=True)
7777
print(upload.url.geturl())
7878

7979
# download a file and set the download directory
8080
from pathlib import Path
8181
target_dir = Path.home().joinpath('Downloads')
82-
filename = anon.download("https://anonfiles.com/9ee1jcu6u9/test_txt", path=target_dir)
83-
print(filename)
82+
download = anon.download("https://anonfiles.com/9ee1jcu6u9/test_txt", path=target_dir)
83+
print(download.file_path)
8484
```
8585

8686
And voilà, pain-free anonymous file sharing. If you want more information about

src/anonfile/__init__.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,33 @@
22

33
from __future__ import annotations
44

5-
import argparse
5+
import json
66
import sys
7+
from argparse import ArgumentParser
78
from collections import namedtuple
8-
from distutils.util import strtobool
99
from pathlib import Path
1010
from typing import List
1111

1212
from .anonfile import *
13-
from .anonfile import __version__
13+
from .anonfile import __version__, package_name
1414

1515

16-
def __print_dict(dictionary: dict, indent: int = 4) -> None:
17-
print("{\n%s\n}" % '\n'.join([f"\033[36m{indent * ' '}{key}\033[0m: \033[32m{value}\033[0m" for key, value in dictionary.items()]))
18-
16+
def str2bool(val: str) -> bool:
17+
return val.lower() in ('yes', 'y', 'true', 't', '1', 'on', '')
1918

2019
def __from_file(path: Path) -> List[str]:
2120
with open(path, mode='r', encoding='utf-8') as file_handler:
2221
return [line.rstrip() for line in file_handler.readlines() if line[0] != '#']
2322

24-
2523
def format_proxies(proxies: str) -> dict:
2624
return {prot: f"{prot}://{ip}" for (prot, ip) in [proxy.split('://') for proxy in proxies.split()]}
2725

28-
29-
def main():
30-
parser = argparse.ArgumentParser(prog=package_name)
26+
def build_parser(package_name: str, version: str) -> ArgumentParser:
27+
parser = ArgumentParser(prog=package_name)
3128
parser._positionals.title = 'Commands'
3229
parser._optionals.title = 'Arguments'
3330

34-
parser.add_argument('-v', '--version', action='version', version=f"%(prog)s {__version__}")
31+
parser.add_argument('-v', '--version', action='version', version=f"%(prog)s {version}")
3532
parser.add_argument('-V', '--verbose', default=True, action='store_true', help="increase output verbosity (default)")
3633
parser.add_argument('--no-verbose', dest='verbose', action='store_false', help="run commands silently")
3734
parser.add_argument('-l', '--logging', default=True, action='store_true', help="enable URL logging (default)")
@@ -59,6 +56,11 @@ def main():
5956
log_parser.add_argument('--path', action='store_true', help="return the log file path")
6057
log_parser.add_argument('--read', action='store_true', help='read the log file')
6158

59+
return parser
60+
61+
def main():
62+
parser = build_parser(package_name, __version__)
63+
6264
try:
6365
args = parser.parse_args()
6466
anon = AnonFile(args.token, user_agent=args.user_agent, proxies=format_proxies(args.proxies) if args.proxies else None)
@@ -77,21 +79,25 @@ def main():
7779
if args.command == 'preview':
7880
for url in args.url:
7981
preview = anon.preview(url)
80-
values = ['online' if preview.status else 'offline', preview.file_path.name, preview.url.geturl(), preview.ddl.geturl(), preview.id, f"{preview.size_readable}"]
82+
response = {
83+
'Status': 'online' if preview.status else 'offline',
84+
'File Path': preview.file_path.name,
85+
'URL': preview.url.geturl(),
86+
'DDL': preview.ddl.geturl(),
87+
'ID': preview.id,
88+
'Size': preview.size_readable,
89+
}
8190

82-
if args.verbose:
83-
__print_dict(dict(zip(['Status', 'File Path', 'URL', 'DDL', 'ID', 'Size'], values)))
84-
else:
85-
print(','.join(values))
91+
print(json.dumps(response, indent=4) if args.verbose else ','.join(response.values))
8692

8793
if args.command == 'download':
8894
for url in (args.url or __from_file(args.batch_file)):
8995
download = lambda url: anon.download(url, args.path, progressbar=args.verbose, enable_logging=args.logging)
9096

9197
if args.check and anon.preview(url, args.path).file_path.exists():
92-
print(f"\033[33mWarning:\033[0m A file with the same name already exists in {str(args.path)!r}.")
93-
choice = input("Proceed with download? [Y/n] ")
94-
if choice == '' or strtobool(choice):
98+
print(f"Warning: A file with the same name already exists in {str(args.path)!r}.")
99+
prompt = input("Proceed with download? [Y/n] ")
100+
if str2bool(prompt):
95101
print(f"File: {download(url).file_path}")
96102
else:
97103
print(f"File: {download(url).file_path}")

0 commit comments

Comments
 (0)