fix: Correct parsing, add comments

This commit is contained in:
Carlos Solís 2025-05-30 16:09:56 +00:00
parent f2c4332d89
commit fedd8f544d

View file

@ -1,83 +1,88 @@
#!/bin/bash #!/bin/bash
#Parameters:
#1st parameter: Channel you want to turn into a playlist. Leave blank to save your subscriptions (cookie file required)
channel=${1:-"subscriptions"} channel=${1:-"subscriptions"}
#2nd parameter: Time limit for the download. Leave blank to save all videos from the last month.
breaktime=${2:-"today-1month"} breaktime=${2:-"today-1month"}
#3rd parameter: Seconds between data requests. Decrease to make downloads faster, but your account may be temporarily blocked if you use a number too low.
sleeptime=${3:-"1.0"} sleeptime=${3:-"1.0"}
#Internal variables:
#Via https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script #Via https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script
folder=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) folder=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
#Required to download your own subscriptions. #Required to download your own subscriptions.
#Obtain this file through the procedure listed at #Obtain this file through the procedure listed at
# https://github.com/yt-dlp/yt-dlp/wiki/FAQ#how-do-i-pass-cookies-to-yt-dlp # https://github.com/yt-dlp/yt-dlp/wiki/FAQ#how-do-i-pass-cookies-to-yt-dlp
#and place it next to your script. #and place it next to your script.
cookies="$folder/yt-cookies.txt" cookies="${folder}/yt-cookies.txt"
subfolder="$folder/$channel" subfolder="${folder}/${channel}"
archive="$subfolder/$channel.txt" archive="${subfolder}/${channel}.txt"
sortcsv="$subfolder/$channel-sort.csv" sortcsv="${subfolder}/${channel}-sort.csv"
csv="$subfolder/$channel.csv" csv="${subfolder}/${channel}.csv"
json="$subfolder/$channel.json" json="${subfolder}/${channel}.json"
python="python" python="python"
if [[ -f "/opt/venv/bin/python" ]]; then if [[ -f "/opt/venv/bin/python" ]]; then
python="/opt/venv/bin/python" python="/opt/venv/bin/python"
fi fi
ytdl="/usr/bin/yt-dlp" ytdl="/usr/bin/yt-dlp"
if [[ -f "/opt/venv/bin/yt-dlp" ]]; then if [[ -f "/opt/venv/bin/yt-dlp" ]]; then
ytdl="/opt/venv/bin/yt-dlp" ytdl="/opt/venv/bin/yt-dlp"
fi fi
if [[ -z "$subfolder" ]]; then if [[ -z "${subfolder}" ]]; then
mkdir "$subfolder" mkdir "${subfolder}"
fi fi
cd "$subfolder" || exit cd "${subfolder}" || exit
#If available, you can use the cookies from your browser directly: #If available, you can use the cookies from your browser directly:
# --cookies-from-browser "firefox" # --cookies-from-browser "firefox"
url="https://www.youtube.com/@$channel" url="https://www.youtube.com/@${channel}"
if [[ "$channel" = "subscriptions" ]]; then if [[ "${channel}" = "subscriptions" ]]; then
url="https://www.youtube.com/feed/subscriptions" url="https://www.youtube.com/feed/subscriptions"
fi fi
if [[ -z "$cookies" ]]; then if [[ -z "${cookies}" ]]; then
"$python" "$ytdl" "$url" \ "${python}" "${ytdl}" "${url}" \
--skip-download --download-archive "$archive" \ --skip-download --download-archive "${archive}" \
--dateafter "$breaktime" \ --dateafter "${breaktime}" \
--extractor-args youtubetab:approximate_date \ --extractor-args youtubetab:approximate_date \
--break-on-reject --lazy-playlist --write-info-json \ --break-on-reject --lazy-playlist --write-info-json \
--sleep-requests "$sleeptime" --sleep-requests "${sleeptime}"
else else
"$python" "$ytdl" "$url" \ "${python}" "${ytdl}" "${url}" \
--cookies "$cookies" \ --cookies "${cookies}" \
--skip-download --download-archive "$archive" \ --skip-download --download-archive "${archive}" \
--dateafter "$breaktime" \ --dateafter "${breaktime}" \
--extractor-args youtubetab:approximate_date \ --extractor-args youtubetab:approximate_date \
--break-on-reject --lazy-playlist --write-info-json \ --break-on-reject --lazy-playlist --write-info-json \
--sleep-requests "$sleeptime" --sleep-requests "${sleeptime}"
fi fi
rm -rf "$csv" rm -rf "${csv}"
ls -t | grep -e ".info.json" | while read -r x; do ls -t | grep -e ".info.json" | while read -r x; do
echo youtube $(jq -c '.id' "$x" | sed -e "s/\"//g") | tee -a "$archive" & echo youtube $(jq -c '.id' "${x}" | sed -e "s/\"//g") | tee -a "${archive}" &
jq -c '[.upload_date, .timestamp, .uploader , .title, .webpage_url]' "$subfolder/$x" | while read -r i; do 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" & echo "${i}" | sed -e "s/^\[//g" -e "s/\]$//g" -e "s/\\\\\"//g" | tee -a "${csv}" &
done done
jq -c '[.upload_date, .timestamp]' "$subfolder/$x" | while read -r i; do 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" & echo "${i},${x}" | sed -e "s/^\[//g" -e "s/\],/,/g" -e "s/\\\\\"//g" | tee -a "${sortcsv}" &
done done
if [[ $(jobs -r -p | wc -l) -ge $(($(getconf _NPROCESSORS_ONLN) * 3 * 2 )) ]]; then if [[ $(jobs -r -p | wc -l) -ge $(($(getconf _NPROCESSORS_ONLN) * 3 * 2)) ]]; then
wait -n wait -n
fi fi
done done
wait wait
sort "$sortcsv" | uniq > "/tmp/$channel-sort-ordered.csv" sort "${sortcsv}" | uniq >"/tmp/${channel}-sort-ordered.csv"
echo "{\"playlistName\":\"$channel\",\"protected\":false,\"description\":\"Videos to watch later\",\"videos\":[" > "/tmp/$channel.db" echo "{\"playlistName\":\"${channel}\",\"protected\":false,\"description\":\"Videos to watch later\",\"videos\":[" >"/tmp/${channel}.db"
cat "/tmp/$channel-sort-ordered.csv" | while read -r line; do cat "/tmp/${channel}-sort-ordered.csv" | while read -r line; do
file=$(echo "$line" | cut -d ',' -f3-) file=$(echo "${line}" | cut -d ',' -f3-)
echo "$file" echo "${file}"
jq -c "{\"videoId\": .id, \"title\": .title, \"author\": .uploader, \"authorId\": .channel_id, \"lengthSeconds\": .duration, \"published\": .epoch, \"timeAdded\": $(date +%s), \"playlistItemId\": \"$(cat /proc/sys/kernel/random/uuid)\", \"type\": \"video\"}" "$subfolder/$file" | tee -a "/tmp/$channel.db" jq -c "{\"videoId\": .id, \"title\": .title, \"author\": .uploader, \"authorId\": .channel_id, \"lengthSeconds\": .duration, \"published\": .epoch, \"timeAdded\": $(date +%s), \"playlistItemId\": \"$(cat /proc/sys/kernel/random/uuid)\", \"type\": \"video\"}" "${subfolder}/${file}" | tee -a "/tmp/${channel}.db"
echo "," >> "/tmp/$channel.db" echo "," >>"/tmp/${channel}.db"
done done
echo "],\"_id\":\"$channel\",\"createdAt\":$(date +%s),\"lastUpdatedAt\":$(date +%s)}" >> "/tmp/$channel.db" echo "],\"_id\":\"${channel}\",\"createdAt\":$(date +%s),\"lastUpdatedAt\":$(date +%s)}" >>"/tmp/${channel}.db"
rm "$json" rm "${json}"
cat "/tmp/$channel.db" | tr '\n' '\r' | sed -e "s/,\r\]/\]/g" | tr '\r' '\n' | jq -c "." > "$json" && rm "/tmp/$channel.db" cat "/tmp/${channel}.db" | grep -v -e ":[ ]*null" | tr '\n' '\r' | sed -e "s/,\r\]/\]/g" | tr '\r' '\n' | jq -c "." >"${json}" && rm "/tmp/${channel}.db"
rm "/tmp/$channel-sort-ordered.csv" rm "/tmp/${channel}-sort-ordered.csv" "${sortcsv}"
sort "$csv" | uniq > "/tmp/$channel-without-header.csv" sort "${csv}" | uniq >"/tmp/${channel}-without-header.csv"
echo '"Upload Date", "Timestamp", "Uploader", "Title", "Webpage URL"' > "/tmp/$channel.csv" echo '"Upload Date", "Timestamp", "Uploader", "Title", "Webpage URL"' >"/tmp/${channel}.csv"
cat "/tmp/$channel-without-header.csv" >> "/tmp/$channel.csv" cat "/tmp/${channel}-without-header.csv" >>"/tmp/${channel}.csv"
mv "/tmp/$channel.csv" "$csv" mv "/tmp/${channel}.csv" "${csv}"
rm "/tmp/$channel-without-header.csv" rm "/tmp/${channel}-without-header.csv"
sort "$archive" | uniq > "/tmp/$channel.txt" sort "${archive}" | uniq >"/tmp/${channel}.txt"
mv "/tmp/$channel.txt" "$archive" mv "/tmp/${channel}.txt" "${archive}"