Advertisement
Guest User

dlm

a guest
May 17th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.38 KB | None | 0 0
  1. #!/usr/bin/env -S bash -euo pipefail
  2.  
  3. # Sets display variables for notify-send
  4. # User might vary depending on machine, check /etc/passwd
  5. DBUS_SESSION_BUS_ADDRESS='unix:path=/run/user/1000/bus'
  6. DISPLAY=':0'
  7.  
  8. # Below provides some information when the script exits without completing successfully
  9. function exit_error(){
  10.     case "${1}" in
  11.         0)
  12.             notify-send "${HOME}/Music directory does not exist or cannot be created"
  13.             ;;
  14.         1)
  15.             notify-send 'A YouTube URL could not be found in the history'
  16.             ;;
  17.         2)
  18.             notify-send 'No kdeconnect devices could be found'
  19.             ;;
  20.         3)
  21.             notify-send 'youtube-dl failed to download'
  22.             ;;
  23.         4)
  24.             notify-send 'Could not find file in directory'
  25.             ;;
  26.         5)
  27.             notify-send 'kdeconnect could not send file'
  28.             ;;
  29.         99)
  30.             notify-send 'Required dependencies not found in $PATH' 'Dependencies are kdeconnect, xclip, youtube-dl'
  31.             ;;
  32.         127)
  33.             notify-send 'Unable to copy/delete the history database. Permission error?'
  34.             ;;
  35.     esac
  36.     exit 1
  37. }
  38.  
  39. # Checks for a list of commands to see if they're installed as required by the script
  40. # If any of the commands aren't found it will exit
  41. command -v notify-send &>/dev/null || echo 'notify-send is not in $PATH (provides error reporting)'
  42. command -v kdeconnect-cli &>/dev/null || exit_error 99
  43. command -v xclip &>/dev/null || exit_error 99
  44. command -v youtube-dl &>/dev/null || exit_error 99
  45. command -v sqlite3 &>/dev/null || exit_error 99
  46.  
  47. # Create the Music directory in $HOME if it does not already exist and changes the current working directory to it
  48. mkdir -p "${HOME}/Music" || exit_error 0
  49. cd "${HOME}/Music" || exit_error 0
  50.  
  51. # Adds a list of connected and reachable devices listed by kdeconnect to an array
  52. # If none are found it exits
  53. mapfile -t dev_ids < <(kdeconnect-cli -l | awk '/reachable/{print $3}')
  54. (( ${#dev_ids[@]} == 0 )) && exit_error 2
  55.  
  56. # Searches for a Youtube URL in the firefox cache files
  57. while read -r cache_file; do
  58.     video_url="$(grep -aoE 'https://www.youtube.com/watch\?v=[a-zA-Z0-9_-]{11}' "${cache_file}" | grep -oP '[\w-]{11}')" && break
  59.     true
  60. done < <(find "${HOME}"/.cache/mozilla/firefox/*.default/cache2/entries/ -type f -printf '%T@ %p\n' | sort -r | cut -d' ' -f2)
  61.  
  62. # If no URL was found in the cache files, it will use Firefox's history database to find one
  63. if [[ -z "${video_url}" ]]; then
  64.     hist_db="$(find "${HOME}/.mozilla/firefox/" -name "places.sqlite")"
  65.     cp "${hist_db}" places-copy.sqlite || exit_error 127
  66.  
  67.     hist_query="select p.url from moz_historyvisits as h, moz_places as p where substr(h.visit_date, 0, 11) >= strftime('%s', date('now')) and p.id == h.place_id order by h.visit_date;"
  68.     video_url="$(sqlite3 places-copy.sqlite "${hist_query}" | grep 'youtube.com' | awk -F'=|&' 'BEGIN {err=1} length($2) == 11 {err=0; print $2} END {exit err}' | tail -1)" || exit_error 1
  69.    
  70.     rm places-copy.sqlite
  71. fi
  72.  
  73. # Downloads the video, converts to vorbis (.ogg) and attempts to find the downloaded vorbis file
  74. youtube-dl -x --audio-format vorbis "https://www.youtube.com/watch?v=${video_url}" &>/dev/null || exit_error 3
  75. file="$(readlink -f <<< ls ./*"${video_url}."*)" || exit_error 4
  76.  
  77. # Attempts to send the converted file to all kdeconnect devices
  78. for dev_id in "${dev_ids[@]}"; do
  79.     kdeconnect-cli -d "${dev_id}" --share "${file}" &>/dev/null || exit_error 5
  80.     notify-send 'Success' "Sent ${file}\\nto ${dev_id}"
  81. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement