Skip to content

Instantly share code, notes, and snippets.

@ErnestoCobos
Created December 1, 2023 05:15
Show Gist options
  • Save ErnestoCobos/d5784a069798127579c46e920826e29c to your computer and use it in GitHub Desktop.
Save ErnestoCobos/d5784a069798127579c46e920826e29c to your computer and use it in GitHub Desktop.
This Bash script automates the process of downloading videos from a specified YouTube channel, uploading them to a designated Dropbox folder, and notifying the user upon completion via AWS SNS. It is designed to run on a Linux environment and utilizes yt-dlp for downloading videos, Dropbox-Uploader for uploading to Dropbox, and awscli for sendin…
#!/bin/bash
# Configurar ARN de tu tema SNS de AWS y URL del canal de YouTube
sns_topic_arn="arn:aws:sns:region:account:id-name"
youtube_channel_url="$1"
dropbox_folder="$2"
# Verificar que se proporcionen ambos argumentos
if [ "$#" -ne 2 ]; then
echo "Uso: $0 URL_DEL_CANAL /ruta/en/Dropbox"
exit 1
fi
# Directorios de log
log_dir=~/logs/videos
mkdir -p "$log_dir"
channel_name=$(basename "$youtube_channel_url")
fecha=$(date +%Y-%m-%d)
log_success_file="$log_dir/${channel_name}-${fecha}-successful.log"
log_failure_file="$log_dir/${channel_name}-${fecha}-failure.log"
# Crear y cambiar al directorio temporal
temp_dir=$(mktemp -d /tmp/upload_webm_from_youtube.XXXXXX)
cd "$temp_dir"
# Marcar tiempo de inicio
start_time=$(date +%s)
# Obtener una lista de URLs de videos individuales
yt-dlp --get-id "$youtube_channel_url" | while read -r video_id; do
video_url="https://www.youtube.com/watch?v=$video_id"
# Descargar un video
yt-dlp -o "%(title)s.%(ext)s" --write-info-json "$video_url"
for file in *.webm; do
if [[ -f "$file" ]]; then
# Extraer la fecha de subida del video desde el archivo JSON de metadatos
fecha=$(jq --raw-output '.upload_date' "${file%.webm}.info.json" | sed 's/\(....\)\(..\)\(..\)/\1-\2-\3/')
# Modificar la fecha del archivo
touch -d "$fecha" "$file"
# Subir el archivo a Dropbox y registrar en logs
if ~/Dropbox-Uploader/dropbox_uploader.sh upload "$file" "$dropbox_folder/$(basename "$file")"; then
echo "$file subido exitosamente." >> "$log_success_file"
rm "$file" "${file%.webm}.info.json"
else
echo "$file fallo al subir. Error: $?" >> "$log_failure_file"
fi
fi
done
# Limpiar archivos descargados para el video actual
rm -f *.webm *.info.json
done
# Limpiar: Borrar el directorio temporal
rm -rf "$temp_dir"
# Marcar tiempo de finalización y calcular tiempo total
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
# Enviar notificación a través de SNS
message="El script ha finalizado. Tiempo total de ejecución: $elapsed_time segundos"
aws sns publish --topic-arn "$sns_topic_arn" --message "$message"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment