From 46cf852324ef58f5c7f28e643ee98b10af99e58e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Sol=C3=ADs?= Date: Mon, 9 Jun 2025 15:07:05 +0000 Subject: [PATCH] feat: Optimize file analysis loop --- youtube-download-channel.sh | 61 ++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/youtube-download-channel.sh b/youtube-download-channel.sh index 770fce9..5e2e657 100755 --- a/youtube-download-channel.sh +++ b/youtube-download-channel.sh @@ -76,41 +76,46 @@ if [[ ! -f "${sortcsv}" ]]; then fi db=$(date -d"${breaktime}" +"%s") find . -type f -iname "*.info.json" -exec ls -t {} + | while read -r xp; do - x="${xp##./}" - if [[ "${channel}" != "subscriptions" && $(jq -rc ".uploader_id" "${subfolder}/${x}") != "@${channel}" ]]; then - echo "Video ${x} not uploaded from ${channel}, removing..." && rm "${subfolder}/${x}" & - fi - if [[ -f "${subfolder}/${x}" && "${breaktime}" =~ ^[0-9]+$ ]]; then - df=$(jq -rc '.timestamp' "${subfolder}/${x}") - if [[ "${db}" -ge "${df}" ]]; then - echo "Video ${x} uploaded before ${breaktime}, removing..." && rm "${subfolder}/${x}" & - else - touch "${subfolder}/${x}" -d "@${df}" & - fi - fi - if [[ -f "${subfolder}/${x}" ]]; then - echo "youtube $(jq -cr '.id' "${subfolder}/${x}")" | tee -a "${archive}" & - if [[ ${enablecsv} = "1" ]]; then - jq -c '[.upload_date, .timestamp, .uploader , .title, .webpage_url]' "${subfolder}/${x}" | while read -r i; do - echo "${i}" | sed -e "s/^\[//g" -e "s/\]$//g" -e "s/\\\\\"/"/g" | tee -a "${csv}" & - done - fi - if [[ ${enablecsv} = "1" || ${enabledb} = "1" ]]; then - jq -c '[.upload_date, .timestamp]' "${subfolder}/${x}" | while read -r i; do - echo "${i},${x}" | sed -e "s/^\[//g" -e "s/\],/,/g" -e "s/\\\\\"/"/g" | tee -a "${sortcsv}" & - done - fi - if [[ $(jobs -r -p | wc -l) -ge $(($(getconf _NPROCESSORS_ONLN) * 3 * 2)) ]]; then - wait -n - fi + ( + x="${xp##./}" + if [[ -f "${subfolder}/${x}" && "${channel}" != "subscriptions" && $(jq -rc ".uploader_id" "${subfolder}/${x}") != "@${channel}" ]]; then + echo "Video ${x} not uploaded from ${channel}, removing..." && rm "${subfolder}/${x}" #& + fi + if [[ -f "${subfolder}/${x}" && "${breaktime}" =~ ^[0-9]+$ && "${db}" -ge "${df}" ]]; then + echo "Video ${x} uploaded before ${breaktime}, removing..." && rm "${subfolder}/${x}" #& + fi + if [[ -f "${subfolder}/${x}" ]]; then + df=$(jq -rc '.timestamp' "${subfolder}/${x}") + touch "${subfolder}/${x}" -d "@${df}" #& + fi + if [[ -f "${subfolder}/${x}" ]]; then + echo "youtube $(jq -cr '.id' "${subfolder}/${x}")" | tee -a "${archive}" & + if [[ ${enablecsv} = "1" ]]; then + jq -c '[.upload_date, .timestamp, .uploader , .title, .webpage_url]' "${subfolder}/${x}" | while read -r i; do + echo "${i}" | sed -e "s/^\[//g" -e "s/\]$//g" -e "s/\\\\\"/"/g" | tee -a "${csv}" #& + done + fi + if [[ ${enablecsv} = "1" || ${enabledb} = "1" ]]; then + jq -c '[.upload_date, .timestamp]' "${subfolder}/${x}" | while read -r i; do + echo "${i},${x}" | sed -e "s/^\[//g" -e "s/\],/,/g" -e "s/\\\\\"/"/g" | tee -a "${sortcsv}" #& + done + fi + fi + ) & + #if [[ $(jobs -r -p | wc -l) -ge $(($(getconf _NPROCESSORS_ONLN) * 3 * 2)) ]]; then + if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]]; then + wait -n fi + done wait if [[ ${enablecsv} = "1" || ${enabledb} = "1" ]]; then sort "${sortcsv}" | uniq >"/tmp/${channel}-sort-ordered.csv" fi if [[ ${enabledb} = "1" ]]; then - rm "/tmp/${channel}.db" + if [[ -f "/tmp/${channel}.db" ]]; then + rm "/tmp/${channel}.db" + fi echo "{\"playlistName\":\"${channel}\",\"protected\":false,\"description\":\"Videos from ${channel} to watch later\",\"videos\":[" >"/tmp/${channel}.db" fi if [[ ${enablecsv} = "1" || ${enabledb} = "1" ]]; then