video & music playlist scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
6.7KB

  1. #!/bin/bash
  2. ##this is going to end up becoming a python thing i just know it
  3. set -o pipefail
  4. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
  5. source ${DIR}/yml_prs.sh
  6. create_variables ${DIR}/.config.yaml
  7. #if socket exists, exit. otherwise, create the socket
  8. if [[ -f /tmp/ytmgr.sock ]]; then echo "$(date -Is) - Cron initiated script, but detected socket still exists from a previous run" >> ${ytmgr_log} ; exit 1; else touch /tmp/ytmgr.sock; fi
  9. #default dirs&files
  10. touch ${ytmgr_log}
  11. for dir in ${ytmgr_hd_mvid_dir} ${ytmgr_sd_mvid_dir} ${ytmgr_channel_dir} ${ytmgr_other_dirs} ${ytmgr_bookmark_dir} ${ytmgr_subs_dir}; do
  12. mkdir -p ${ytmgr_download_dir}/${dir}/logs/
  13. touch ${ytmgr_download_dir}/${dir}/logs/{success,fail}.log
  14. touch ${ytmgr_download_dir}/${dir}/fail.m3u
  15. done
  16. ## arg1 is the youtube url arg2 is the destination folder
  17. ytdl_cmd () {
  18. local video_url=$(echo ${1} | cut -d"&" -f1 | sed 's/invidio.us/youtube.com/')
  19. local path=${2}
  20. local country_loop=false
  21. local video_id=$(echo "${video_url}" | cut -d"=" -f2)
  22. if [[ -z $video_id ]]; then echo "$(date -Is) - Invalid line detected for destination ${path}" >> ${ytmgr_log} ; exit 1; fi
  23. if [[ ! -d ${path} ]]; then echo "$(date -Is) - Invalid line detected for the path ${path}" >> ${ytmgr_log}; exit 1; fi
  24. if [[ -n $(grep -- ${video_url} ${path}/logs/success.log) || -n $(grep -- ${video_url} ${path}/logs/fail.log) ]]; then return 0; fi
  25. local attempt_count=$(grep -- "${video_url}" ${ytmgr_log} | grep -i "attempt\|fail" | wc -l)
  26. if [[ ${attempt_count} -gt ${ytmgr_attemptsb4_global} ]]; then export country_loop=true; fi
  27. if [[ ${attempt_count} -gt ${ytmgr_max_attempts} ]]; then
  28. echo "$(date -Is) - The video ${video_url} has failed more than ${ytmgr_max_attempts} times. Cancelling its download." >> ${ytmgr_log}
  29. echo "${video_url}" >> ${path}/logs/fail.log
  30. echo "${video_id}" | sed "s ^ plugin://plugin.video.youtube/play/?video_id= " >> ${path}/fail.m3u
  31. elif [[ -n $(echo ${video_url} | grep -v "youtube") ]]; then
  32. echo "$(date -Is) - Trying to download a non-youtube video..." >> ${ytmgr_log}
  33. if /usr/local/bin/youtube-dl ${video_url} -v --no-playlist -o "${path}/%(uploader)s-%(title)s.%(ext)s" &>> ${ytmgr_log}; then
  34. echo "${video_url}" >> ${path}/logs/success.log; echo "$(date -Is) - Video ${video_url} has been downloaded successfully on attempt ${attempt_count}." >> ${ytmgr_log}
  35. rm -f ${path}/logs/${video_id}*.log
  36. else export fail_count=$(grep -- "$video_id" ${ytmgr_log} | grep -i fail | wc -l)
  37. echo "$(date -Is) - Video download of ${video_url} has failed. Failure no. ${fail_count} recorded." >> ${ytmgr_log}
  38. fi
  39. else touch ${path}/logs/${video_id}.log
  40. while true; do
  41. local attempt_count=$(grep -- "$video_id" ${ytmgr_log} | grep -i attempt | wc -l)
  42. echo "$(date -Is) - Video (${video_url}) found. Attempt ${attempt_count} to download..." >> ${ytmgr_log}
  43. if [[ "${country_loop}" == true ]]; then
  44. for co in ${ytmgr_countries_attempted}; do
  45. echo "$(date -Is) - Attempt ${attempt_count} for video ${video_url} - doing country ${co}." >> ${ytmgr_log}
  46. if /usr/local/bin/youtube-dl ${video_url} -v -4 --no-playlist --geo-bypass --geo-bypass-country ${co} -o "${path}/%(uploader)s_ %(title)s.%(ext)s" &>> ${path}/logs/${video_id}.log; then
  47. echo "${video_id}" >> ${path}/logs/success.log; echo "$(date -Is) - Video ${video_url} has been downloaded successfully on attempt ${attempt_count} to ${dir}." >> ${ytmgr_log}
  48. rm -f ${path}/logs/${video_id}.log
  49. break 2
  50. else attempt_count=$((${attempt_count} + 1)); continue
  51. fi
  52. done
  53. break
  54. else if /usr/local/bin/youtube-dl ${video_url} -v -4 --no-playlist -o "${path}/%(uploader)s_ %(title)s.%(ext)s" &>> ${path}/logs/${video_id}.log; then
  55. echo "${video_url}" >> ${path}/logs/success.log; echo "$(date -Is) - Video ${video_url} has been downloaded successfully on attempt ${attempt_count}." >> ${ytmgr_log}
  56. rm -f ${path}/logs/${video_id}*.log
  57. break
  58. else export fail_count=$(grep -- "$video_id" ${ytmgr_log} | grep -i fail | wc -l)
  59. echo "$(date -Is) - Video download of ${video_url} has failed. Failure no. ${fail_count} recorded." >> ${ytmgr_log}
  60. if [[ ${fail_count} -le ${ytmgr_attemptsb4_break} ]]; then
  61. export fail_reason="$(grep -i ERROR ${path}/logs/${video_id}.log |tail -n1)"
  62. if [[ -n $(echo ${fail_reason} | grep "removed\|private\|unsupported\|not available\|Incomplete") ]]; then
  63. echo "${video_url}" >> ${path}/logs/fail.log
  64. echo "$(date -Is) - The video ${video_url} is not available for download. to search the uri, please click on the following URL: ${global_search}${video_id}" >> ${ytmgr_log}
  65. elif [[ -n $(echo ${fail_reason} | grep "country\|copyright") ]]; then
  66. export country_loop=true
  67. elif [[ -n $(echo ${fail_reason} | grep "429") ]]; then
  68. echo "$(date -Is) - Got an error 429 for too many http requests. Sleeping for 30 minutes..." >> ${ytmgr_log}
  69. sleep 1800
  70. elif [[ -n $(echo ${fail_reason} | grep "urlopen") ]]; then
  71. continue
  72. else echo "$(date -Is) - The video ${video_url} has done something weird to my script or has an uncaught error." >> ${ytmgr_log}
  73. break
  74. fi
  75. elif [[ ${fail_count} -le ${ytmgr_attemptsb4_break} ]]; then sleep 5
  76. elif [[ ${fail_count} -gt ${ytmgr_attemptsb4_global} ]]; then export country_loop=true
  77. elif [[ ${fail_count} -le ${ytmgr_max_attempts} ]]; then break
  78. else echo "$(date -Is) - Some weird unknown thing happened while trying to download this video, ${video_url}." >> ${ytmgr_log}
  79. break
  80. fi
  81. fi
  82. fi
  83. done
  84. fi
  85. sleep 15
  86. }
  87. echo "$(date -Is) - Starting ytmgr script with ${ytmgr_url_file} directories" >> ${ytmgr_log}
  88. for dir in ${ytmgr_other_dirs} ${ytmgr_hd_musicvid_dir} ${ytmgr_sd_musicvid_dir} ${ytmgr_subscription_dir}; do
  89. for video in $(cat ${ytmgr_download_dir}/${dir}/logs/${ytmgr_url_file} | sed 's/invidio.us/youtube.com/'); do
  90. ytdl_cmd ${video} ${ytmgr_download_dir}/${dir}
  91. done
  92. done
  93. ##subscriptions
  94. echo "$(date -Is) - Checking subscription feed (${ytmgr_sub_feed})" >> ${ytmgr_log}
  95. for video in $(curl -sL "${ytmgr_sub_feed}" | grep youtube.com/watch | cut -d'"' -f2 | grep -v html); do
  96. ytdl_cmd ${video} ${ytmgr_download_dir}/${ytmgr_subscription_dir}/
  97. done
  98. echo "$(date -Is) - Checking bookmark feed (${ytmgr_bookmark_feed})" >> ${ytmgr_log}
  99. for video in $(curl -sL "${ytmgr_bookmark_feed}" | grep href | grep "invidio.us\|youtube.com\|vimeo.com\|dailymotion.com" | cut -d'"' -f2); do
  100. ytdl_cmd ${video} ${ytmgr_download_dir}/${ytmgr_bookmark_dir}/
  101. done
  102. ##channels m3u files
  103. echo "$(date -Is) - Youtube manager script finished running." >> ${ytmgr_log}
  104. rm -f /tmp/ytmgr.sock