22
33from __future__ import annotations
44
5- import argparse
5+ import json
66import sys
7+ from argparse import ArgumentParser
78from collections import namedtuple
8- from distutils .util import strtobool
99from pathlib import Path
1010from typing import List
1111
1212from .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
2019def __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-
2523def 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