-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxsyncpy2.py
More file actions
249 lines (207 loc) · 9.39 KB
/
axsyncpy2.py
File metadata and controls
249 lines (207 loc) · 9.39 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import os
import requests
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import argparse
import time
# ----------------------------
# Funciones comunes
# ----------------------------
def check_or_create_directory(path, auto_create=False):
if not os.path.exists(path):
if auto_create:
os.makedirs(path)
print(f"Carpeta '{path}' creada automáticamente.")
return True
else:
print(f"La carpeta de destino '{path}' no existe.")
respuesta = input("¿Deseas crearla? (Y/N): ").strip().lower()
if respuesta == 'y':
os.makedirs(path)
print(f"Carpeta '{path}' creada.")
return True
else:
print("Descarga cancelada.")
return False
return True
def get_filename_from_url(url):
return os.path.basename(urlparse(url).path)
# ----------------------------
# Descarga con Multithreading
# ----------------------------
def download_chunk(url, start, end, file_name, part_number):
headers = {
'Range': f'bytes={start}-{end}',
'Accept-Encoding': 'identity' # Evitar compresión gzip
}
response = requests.get(url, headers=headers, stream=True)
response.raise_for_status()
part_file = f"{file_name}.part{part_number}"
with open(part_file, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return part_file, part_number
def combine_parts(file_name, parts):
with open(file_name, 'wb') as f:
for part in parts:
with open(part, 'rb') as p:
f.write(p.read())
os.remove(part)
def download_file_multithread(url, file_path, num_threads=8):
response = requests.head(url, headers={'Accept-Encoding': 'identity'})
total_size = int(response.headers.get('content-length', 0))
chunk_size = total_size // num_threads
ranges = [(i * chunk_size, (i + 1) * chunk_size - 1) for i in range(num_threads)]
ranges[-1] = (ranges[-1][0], total_size - 1)
parts = [None] * num_threads
with ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = []
for i, (start, end) in enumerate(ranges):
futures.append(executor.submit(download_chunk, url, start, end, file_path, i))
with tqdm(total=total_size, unit='B', unit_scale=True, desc=os.path.basename(file_path)) as pbar:
for future in as_completed(futures):
part_file, part_number = future.result()
parts[part_number] = part_file
pbar.update(os.path.getsize(part_file))
combine_parts(file_path, parts)
print(f"{os.path.basename(file_path)} descargado con éxito.")
# ----------------------------
# Descarga Secuencial (Sin Multithreading)
# ----------------------------
def download_file_single_thread(url, file_path):
response = requests.get(url, headers={'Accept-Encoding': 'identity'}, stream=True)
response.raise_for_status()
total_size = int(response.headers.get('content-length', 0))
with open(file_path, 'wb') as f:
with tqdm(total=total_size, unit='B', unit_scale=True, desc=os.path.basename(file_path)) as pbar:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
pbar.update(len(chunk))
# ----------------------------
# Lógica Principal (Modificada)
# ----------------------------
def download_file(url, file_path, num_threads=8):
response = requests.head(url, headers={'Accept-Encoding': 'identity'})
total_size = int(response.headers.get('content-length', 0))
if total_size < 1024 * 1024: # Si el archivo es menor a 1MB
print(f"El archivo {os.path.basename(file_path)} es pequeño. Descargando en un solo hilo...")
download_file_single_thread(url, file_path)
else:
print(f"El archivo {os.path.basename(file_path)} es grande. Descargando con {num_threads} hilos...")
download_file_multithread(url, file_path, num_threads)
def download_directory(base_url, destination_folder, num_threads=8, auto_create=False, file_delay=0):
if not check_or_create_directory(destination_folder, auto_create):
return
response = requests.get(base_url, headers={'Accept-Encoding': 'identity'})
soup = BeautifulSoup(response.text, 'html.parser')
for link in soup.find_all('a'):
file_name = link.get('href')
if not file_name or file_name.startswith('?') or file_name.endswith('/') or file_name == '../':
continue
file_url = base_url + file_name
file_path = os.path.join(destination_folder, file_name)
try:
download_file(file_url, file_path, num_threads)
if file_delay > 0: # Aplicar retraso entre archivos
print(f"Esperando {file_delay} segundos antes de la próxima descarga...")
time.sleep(file_delay)
except Exception as e:
print(f"Error al descargar {file_name}: {e}")
def process_urls(input_file, base_destination, num_threads=8, auto_create=False, url_delay=0):
if not check_or_create_directory(base_destination, auto_create):
return
with open(input_file, 'r') as f:
urls = f.read().splitlines()
for url in urls:
parsed_url = urlparse(url)
folder_name = parsed_url.path.strip('/').split('/')[-1]
destination_folder = os.path.join(base_destination, folder_name)
if not check_or_create_directory(destination_folder, auto_create):
continue
print(f"\nDescargando carpeta: {folder_name}")
print(f"URL: {url}")
print(f"Destino: {destination_folder}")
download_directory(url, destination_folder, num_threads, auto_create, file_delay=args.file_delay)
if url_delay > 0: # Aplicar retraso entre URLs
print(f"Esperando {url_delay} segundos antes de la próxima URL...")
time.sleep(url_delay)
# ----------------------------
# Configuración de Argumentos (Modificada)
# ----------------------------
def positive_int(value):
ivalue = int(value)
if ivalue <= 0:
raise argparse.ArgumentTypeError("El número de hilos debe ser mayor que 0.")
return ivalue
def positive_float(value):
fvalue = float(value)
if fvalue < 0:
raise argparse.ArgumentTypeError("El valor no puede ser negativo.")
return fvalue
def parse_arguments():
parser = argparse.ArgumentParser(description="Descargar archivos desde URLs FTP.")
parser.add_argument("--input", "-i", help="Archivo de texto con URLs de carpetas.")
parser.add_argument("--output", "-o", required=True, help="Carpeta base de destino.")
parser.add_argument(
"--threads", "-t",
type=positive_int, # Validación personalizada
default=8,
help="Número de hilos (debe ser mayor que 0, default: 8)."
)
parser.add_argument("--url", "-u", help="URL de un directorio para descargar todos sus archivos.")
parser.add_argument(
"--auto-create-dirs", "-a",
action="store_true", # Parámetro booleano
help="Crear automáticamente las carpetas necesarias sin preguntar."
)
parser.add_argument(
"--file-delay",
type=positive_float, # Validación personalizada
default=0,
help="Retraso en segundos entre la descarga de cada archivo (default: 0, no puede ser negativo)."
)
parser.add_argument(
"--url-delay",
type=positive_float, # Validación personalizada
default=0,
help="Retraso en segundos entre la descarga de cada URL (default: 0, no puede ser negativo)."
)
return parser.parse_args()
# ----------------------------
# Punto de Entrada (Modificado)
# ----------------------------
def print_configuration(args):
print("\nConfiguración de descarga:")
print(f"- Carpeta de destino: {args.output}")
print(f"- Multithreading: Activado ({args.threads} hilos)")
if args.url:
print(f"- URL del directorio: {args.url}")
elif args.input:
print(f"- Archivo de URLs: {args.input}")
print(f"- Crear carpetas automáticamente: {'Sí' if args.auto_create_dirs else 'No'}")
print(f"- Retraso entre archivos: {args.file_delay} segundos")
print(f"- Retraso entre URLs: {args.url_delay} segundos")
print("-" * 40)
if __name__ == "__main__":
# Parsear argumentos de la línea de comandos
args = parse_arguments()
# Mostrar configuración de descarga
print_configuration(args)
if args.url:
# Descargar todos los archivos de la URL del directorio
parsed_url = urlparse(args.url)
folder_name = parsed_url.path.strip('/').split('/')[-1]
destination_folder = os.path.join(args.output, folder_name)
print(f"\nDescargando carpeta: {folder_name}")
print(f"URL: {args.url}")
print(f"Destino: {destination_folder}")
download_directory(args.url, destination_folder, args.threads, args.auto_create_dirs, args.file_delay)
elif args.input:
# Procesar el archivo de texto con URLs
process_urls(args.input, args.output, args.threads, args.auto_create_dirs, args.url_delay)
else:
print("Error: Debes proporcionar --input o --url.")
exit(1)
print("\n¡Descarga completada!")