Skip to content

Commit

Permalink
Update LinkDownloader.py
Browse files Browse the repository at this point in the history
Changed the comments to english.
  • Loading branch information
languagemaniac authored Feb 16, 2023
1 parent 740b88c commit 1a8e378
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions LinkDownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,85 @@
import time
from concurrent.futures import ThreadPoolExecutor

# Nombre del archivo que contiene las urls a descargar
# Name of the file where the URLs are
archivo_urls = "urls.txt"

# Directorio de descarga de los archivos
directorio_descarga = "descargas"
# File download folder
directorio_descarga = "Downloads"

# Obtener el número del último archivo descargado
# Get the number of the last downloaded file
if not os.path.exists(directorio_descarga):
os.makedirs(directorio_descarga)
files = os.listdir(directorio_descarga)
last_file_num = max([int(f.split(".")[0]) for f in files]) if files else 0

# Abrir el archivo de urls
# Open the URL list textfile
with open(archivo_urls, "r", encoding="utf-8") as f:
urls = f.read().splitlines()

# Pedir al usuario el número a partir del cual se debe continuar descargando
continuar_desde = input("¿A partir de qué número de archivo desea continuar descargando? (0 para comenzar desde el principio): ")
# Ask the user for the number from which to continue downloading
continuar_desde = input("From what file number do you want to continue downloading? (0 to start from the beginning): ")
try:
continuar_desde = int(continuar_desde)
if continuar_desde < 0:
continuar_desde = 0
except:
continuar_desde = 0

# Descargar archivos en paralelo
# Download files in parallel
def descargar_archivo(url, nombre_archivo):
ruta_archivo = os.path.join(directorio_descarga, nombre_archivo)

# Descargar archivo
print(f"Descargando {nombre_archivo}...")
# Download files
print(f"Downloading {nombre_archivo}...")
try:
response = requests.get(url)
with open(ruta_archivo, "wb") as f:
f.write(response.content)
print(f"Descarga de {nombre_archivo} completa.")
print(f"{nombre_archivo} Downloaded.")
except Exception as e:
print(f"Error al descargar {nombre_archivo}: {str(e)}")
# Esperar un tiempo y volver a intentar
print(f"Error downloading {nombre_archivo}: {str(e)}")
# Wait some time and try again
time.sleep(60)
try:
response = requests.get(url)
with open(ruta_archivo, "wb") as f:
f.write(response.content)
print(f"Descarga de {nombre_archivo} completa.")
print(f"{nombre_archivo} Downloaded.")
except Exception as e:
print(f"Error al descargar {nombre_archivo}: {str(e)}")
# Esperar un tiempo adicional y volver a intentar
print(f"Error when downloading {nombre_archivo}: {str(e)}")
# Wait some more time and try again
time.sleep(120)

with ThreadPoolExecutor() as executor:
# Descargar archivos en paralelo
# Download files in parallel
futures = []
for i, url in enumerate(urls[continuar_desde:]):
num_archivo = i + continuar_desde + 1
nombre_archivo = f"{num_archivo}.mp3"
futures.append(executor.submit(descargar_archivo, url, nombre_archivo))

# Esperar a que se completen todas las descargas
# Wait for all the downloads to finish
for future in futures:
future.result()

print("Descarga completa.")
print("Download completed")

# Crear lista de URLs que no se pudieron descargar y motivo
# Create list with the URLs that couldn't be downloaded and the reason for that
errores_descarga = []
for i, url in enumerate(urls[continuar_desde:]):
num_archivo = i + continuar_desde + 1
nombre_archivo = f"{num_archivo}.mp3"
ruta_archivo = os.path.join(directorio_descarga, nombre_archivo)

if not os.path.exists(ruta_archivo):
errores_descarga.append((url, "Archivo no descargado"))
errores_descarga.append((url, "File not downloaded"))
elif os.path.getsize(ruta_archivo) == 0:
errores_descarga.append((url, "Archivo vacío"))
errores_descarga.append((url, "Empty file"))

# Escribir lista de errores en archivo de texto
# Write error list in a text file
if errores_descarga:
with open("errores.txt", "w", encoding="utf-8") as f:
with open("errors.txt", "w", encoding="utf-8") as f:
for url, mensaje in errores_descarga:
f.write(f"{url}\t{mensaje}\n")
print("Se han registrado errores de descarga. Verifique el archivo 'errores.txt' para más información.")
print("Download errors have been recorded. Check the 'errors.txt' file for more information.")

0 comments on commit 1a8e378

Please sign in to comment.