Skip to content

Commit

Permalink
Merge pull request #90 from nstrydom2/dev
Browse files Browse the repository at this point in the history
Adds missing changes to changelog and handles duplicate downloads
  • Loading branch information
nstrydom2 authored Jul 22, 2023
2 parents 43ff66b + d5d0435 commit 82412d5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

## Version 1.0.0 (2023-7-18)

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

## Version 0.2.7 (2021-9-12)

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ from anonfile import AnonFile

anon = AnonFile()

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

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

And voilà, pain-free anonymous file sharing. If you want more information about
Expand Down
44 changes: 25 additions & 19 deletions src/anonfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,33 @@

from __future__ import annotations

import argparse
import json
import sys
from argparse import ArgumentParser
from collections import namedtuple
from distutils.util import strtobool
from pathlib import Path
from typing import List

from .anonfile import *
from .anonfile import __version__
from .anonfile import __version__, package_name


def __print_dict(dictionary: dict, indent: int = 4) -> None:
print("{\n%s\n}" % '\n'.join([f"\033[36m{indent * ' '}{key}\033[0m: \033[32m{value}\033[0m" for key, value in dictionary.items()]))

def str2bool(val: str) -> bool:
return val.lower() in ('yes', 'y', 'true', 't', '1', 'on', '')

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


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


def main():
parser = argparse.ArgumentParser(prog=package_name)
def build_parser(package_name: str, version: str) -> ArgumentParser:
parser = ArgumentParser(prog=package_name)
parser._positionals.title = 'Commands'
parser._optionals.title = 'Arguments'

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

return parser

def main():
parser = build_parser(package_name, __version__)

try:
args = parser.parse_args()
anon = AnonFile(args.token, user_agent=args.user_agent, proxies=format_proxies(args.proxies) if args.proxies else None)
Expand All @@ -77,21 +79,25 @@ def main():
if args.command == 'preview':
for url in args.url:
preview = anon.preview(url)
values = ['online' if preview.status else 'offline', preview.file_path.name, preview.url.geturl(), preview.ddl.geturl(), preview.id, f"{preview.size_readable}"]
response = {
'Status': 'online' if preview.status else 'offline',
'File Path': preview.file_path.name,
'URL': preview.url.geturl(),
'DDL': preview.ddl.geturl(),
'ID': preview.id,
'Size': preview.size_readable,
}

if args.verbose:
__print_dict(dict(zip(['Status', 'File Path', 'URL', 'DDL', 'ID', 'Size'], values)))
else:
print(','.join(values))
print(json.dumps(response, indent=4) if args.verbose else ','.join(response.values))

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

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

0 comments on commit 82412d5

Please sign in to comment.