|
|
import pandas as pd
|
|
|
import os
|
|
|
import requests
|
|
|
from urllib.parse import urlparse, parse_qs
|
|
|
import time
|
|
|
import random
|
|
|
|
|
|
|
|
|
df = pd.read_csv('metadata.csv')
|
|
|
|
|
|
|
|
|
os.makedirs('files', exist_ok=True)
|
|
|
|
|
|
ifile = 0
|
|
|
log = open("log.txt", "w")
|
|
|
|
|
|
for index, row in df.iterrows():
|
|
|
if row['nombre'] == 'filelink':
|
|
|
value = row['value']
|
|
|
if 'isAllowed=y' in value:
|
|
|
ifile += 1
|
|
|
print(ifile)
|
|
|
log.write(str(ifile)+"\n")
|
|
|
log.flush()
|
|
|
if ifile < 43310:
|
|
|
continue
|
|
|
|
|
|
file_url = "https://repositorio.uchile.cl" + value
|
|
|
|
|
|
try:
|
|
|
|
|
|
parsed_url = urlparse(row['origen'])
|
|
|
path_parts = parsed_url.path.split('/')
|
|
|
id_coleccion = path_parts[2]
|
|
|
id_recurso = path_parts[3]
|
|
|
|
|
|
|
|
|
file_name = value.split('/')[-1].split('?')[0]
|
|
|
|
|
|
|
|
|
final_file_name = f"files/{id_coleccion}_{id_recurso}_{file_name}"
|
|
|
|
|
|
|
|
|
time.sleep(random.uniform(3,5))
|
|
|
|
|
|
|
|
|
response = requests.get(file_url)
|
|
|
if response.status_code == 200:
|
|
|
with open(final_file_name, 'wb') as f:
|
|
|
f.write(response.content)
|
|
|
log.write(f"Archivo descargado: {final_file_name}\n")
|
|
|
else:
|
|
|
log.write(f"Error al descargar {final_file_name}: {response.status_code}\n")
|
|
|
log.flush()
|
|
|
except:
|
|
|
log.write("Error")
|
|
|
log.flush()
|
|
|
print(ifile) |