-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
167 lines (135 loc) · 5.99 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import argparse
import logging
import os
import sys
from configparser import ConfigParser
from logging.handlers import RotatingFileHandler
from auto_seedr import AutoSeedrClient, setup_config
DEFAULT_CONFIG_FILE = 'config.ini'
DEFAULT_LOG_LEVEL = 'ERROR'
DEFAULT_LOG_FILE = 'auto_seedr_cli.log'
def configure_logging(log_level: str, log_file: str) -> None:
"""
Configure logging for the application.
:param log_level: The desired logging level.
:param log_file: The file path for the log file.
:return: None
"""
logging.basicConfig(level=log_level.upper())
file_handler = RotatingFileHandler(log_file, maxBytes=1024 * 1024, backupCount=3)
file_formatter = logging.Formatter('%(asctime)s [%(levelname)s] - %(message)s')
file_handler.setFormatter(file_formatter)
console_handler = logging.StreamHandler()
console_formatter = logging.Formatter('[%(levelname)s] - %(message)s')
console_handler.setFormatter(console_formatter)
logging.getLogger().addHandler(file_handler)
logging.getLogger().addHandler(console_handler)
def get_user_input(prompt, default=None):
"""
Get user input with an optional default value.
:param prompt: The prompt to display to the user.
:param default: The default value if the user input is empty.
:return: User input or the default value.
"""
user_input = input(prompt)
return user_input.strip() if user_input.strip() != '' else default
def parse_args():
"""
Parse command-line arguments using argparse.
:return: Parsed arguments.
"""
parser = argparse.ArgumentParser(description='AutoSeedr CLI')
parser.add_argument('-C', '--create-config', action='store_true', help='Create a new config file')
parser.add_argument('-L', '--load-config', action='store_true', help='Load an existing config file')
config_group = parser.add_argument_group('Configuration Options')
config_group.add_argument('-f', '--config-file', default=DEFAULT_CONFIG_FILE,
help='INI file path for configuration (default: config.ini)')
config_group.add_argument('-e', '--email', help='Seedr account email')
config_group.add_argument('-p', '--password', help='Seedr account password')
config_group.add_argument('-td', '--torrent-directory', default='torrents',
help='Directory containing torrent files (default: torrents)')
config_group.add_argument('-dd', '--download-directory', default='downloads',
help='Directory to store downloaded files (default: downloads)')
config_group.add_argument('-cs', '--chunk-size', default=1024, type=int,
help='Chunk size for downloading files in kilobytes (default: 1024)')
log_group = parser.add_argument_group('Logging Options')
log_group.add_argument('-ll', '--log-level', default=DEFAULT_LOG_LEVEL,
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help='Logging level (default: ERROR)')
log_group.add_argument('-lf', '--log-file', default=DEFAULT_LOG_FILE,
help='Log file path (default: auto_seedr_cli.log)')
args = parser.parse_args()
if not args.create_config and not args.load_config and not (args.email and args.password):
parser.print_help()
print(
"\nInvalid combination of arguments. Please provide either --create-config, --load-config, or valid email "
"and password.")
sys.exit(1)
return args
def prompt_for_missing_details():
"""
Prompt the user for missing details (email and password).
:return: Tuple containing email and password.
"""
email = get_user_input('Seedr account email: ')
password = get_user_input('Seedr account password: ')
return email, password
def main():
"""
The main function that orchestrates the AutoSeedr CLI functionality.
"""
args = parse_args()
torrent_directory, download_directory, chunk_size = (
args.torrent_directory,
args.download_directory,
args.chunk_size
)
if args.create_config:
if not args.email or not args.password:
logging.warning('Email and password are required to create a new config.')
email, password = prompt_for_missing_details()
else:
email, password = args.email, args.password
setup_config(
email=email,
password=password,
config_file=args.config_file,
torrent_directory=args.torrent_directory,
download_directory=args.download_directory,
chunk_size=args.chunk_size
)
logging.info(f'Config file "{args.config_file}" created successfully.')
elif args.load_config:
if not os.path.exists(args.config_file):
logging.error(f'Config file "{args.config_file}" not found.')
return
config = ConfigParser()
config.read(args.config_file)
email = config.get('SEEDR', 'user')
password = config.get('SEEDR', 'password')
torrent_directory = config.get('APP', 'torrent_folder')
download_directory = config.get('APP', 'download_folder')
chunk_size = config.get('APP', 'chunk_size')
else:
email, password, torrent_directory, download_directory, chunk_size = (
args.email,
args.password,
args.torrent_directory,
args.download_directory,
args.chunk_size
)
configure_logging(args.log_level, args.log_file)
seedr_client = AutoSeedrClient(
email=email,
password=password,
torrent_directory=torrent_directory,
download_directory=download_directory,
chunk_size=chunk_size,
)
try:
seedr_client.directory_download()
logging.info(f'Directory download completed successfully. Files downloaded to: {download_directory}')
except Exception as e:
logging.error(f'Error: {e}', exc_info=True)
if __name__ == '__main__':
main()