summaryrefslogtreecommitdiff
path: root/ar/.local/bin/qndl
blob: 15d7a4396b7aded3b54381acf41c7a8015c32d7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/sh

# check browser
case "$BROWSER" in
librewolf) cookies="librewolf:~/.librewolf" ;;
firefox) cookies="firefox:~/.mozilla/firefox" ;;
esac

# [url] [type] [cmd]
if [ $# -eq 1 ]; then
  type="$1"
  url="$(xclip -selection clipboard -o)"
elif [ $# -eq 2 ]; then
  if echo "$1" | grep -qE "https?://"; then
    url="$1"
  elif echo "$2" | grep -qE "https?://"; then
    type="$1"
    url="$2"
  fi
fi

# check url
[ -z "$url" ] && notify-send "⛔ No URL provided and clipboard is empty or does not contain a valid URL." && exit 1
! echo "$url" | grep -qE '^https?://[a-zA-Z0-9.-]+(/[a-zA-Z0-9./?&%=_-]*)?$' && notify-send "⛔ Invalid URL format: $url" && exit 1
! curl --head --silent --fail "$url" >/dev/null && notify-send "⛔ URL is not accessible: $url" && exit 1

# opts
[ -n "$cookies" ] && base="yt-dlp --cookies-from-browser $cookies -civ --no-force-overwrites --no-playlist" ||
  base="yt-dlp -civ --no-force-overwrites --no-playlist"

case $type in
-m | --music)
  downloadtype="music"
  dest="${XDG_MUSIC_DIR:-${HOME}/Music}"
  record="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}/default/Music/.music.txt"
  output="$dest/%(artist|)s%(artist& - |)s%(title)s.%(ext)s"
  cmd="$base --audio-format best --audio-quality 0 --download-archive \"$record\" --extract-audio --recode-video mp3"
  ;;
-r | --restore)
  dest="${XDG_MUSIC_DIR:-${HOME}/Music}"
  record="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}/default/Music/.music.txt"
  output="$dest/%(artist|)s%(artist& - |)s%(title)s.%(ext)s"
  cmd="$base --audio-format best --audio-quality 0 --extract-audio --recode-video mp3"
  [ ! -f "$record" ] && exit 1
  while read -r line; do
    video_id=$(echo "$line" | awk '{print $2}')
    cmd="$cmd -o $output"
    idnum=$(tsp $cmd "https://www.youtube.com/watch?v=$video_id")
  done <"$record"
  tsp -D "$idnum" notify-send "🎵 Music restored!"
  exit 0
  ;;
-v | --video)
  downloadtype="video"
  dest="${XDG_VIDEOS_DIR:-${HOME}/Videos}"
  output="$dest/%(title)s [%(id)s].%(ext)s"
  encode=$(printf "best\n60fps\n30fps\nmp4\nmkv" | dmenu -i -p "Choose an encoding (default: 1080p)") || exit
  option=""
  case $encode in
  best) format="--format bestvideo+bestaudio/best" ;;
  60fps) format='--format "((bv*[fps=60]/bv*)[height<=1080]/(wv*[fps=60]/wv*)) + ba / (b[fps=60]/b)[height<=1080]/(w[fps=60]/w)"' ;;
  30fps) format='--format "((bv*[fps=30]/bv*)[height<=1080]/(wv*[fps=30]/wv*)) + ba / (b[fps=30]/b)[height<=1080]/(w[fps=30]/w)"' ;;
  *)
    format="--format bestvideo+bestaudio/best"
    option="--recode-video $encode"
    ;;
  esac
  cmd="$base --buffer-size 1M --embed-metadata --embed-thumbnail --no-sponsorblock $format $option"
  cmd="${cmd%* }"
  ;;
esac

# check playlist
case $url in
*playlist* | *list=*)
  filetype=$(printf "playlist\na content" | dmenu -i -p "Download entire playlist or just this content?")
  [ "$filetype" = "playlist" ] &&
    base=$(echo "$base" | sed 's/ --no-playlist//') &&
    base="$base --yes-playlist" &&
    echo 🪏 >/tmp/qplaylist
  [ "$downloadtype" = "video" ] &&
    channel=$(yt-dlp -s -O "%(channel)s" "$url" | head -n 1 | sed 's/, /,/g;s/[\/:*?"<>| ]/-/g' | tr '[:upper:]' '[:lower:]') &&
    playlisttitle=$(yt-dlp -s -O "%(playlist_title)s" "$url" | head -n 1 | sed 's/, /,/g;s/[\/:*?"<>| ]/-/g' | tr '[:upper:]' '[:lower:]') &&
    subdest="$channel/$playlisttitle" &&
    mkdir -p "$dest/$subdest" &&
    output="$dest/$subdest/%(playlist_index)02d. %(title)s [%(id)s].%(ext)s"
  ;;
esac

filename=$(yt-dlp -s -O "%(filename)s [%(id)s].%(ext)s" "$url")

[ -z "$type" ] && output="$HOME/$filename"

# queue
echo "$filename" | while IFS= read -r file; do
  notify-send "📥 Queuing..." "$(basename "$file")"
done

# download
rm -rf /tmp/qplaylist
notify-send "⏳ Downloading..." "$filename"
idnum="$(tsp $cmd -o "$output" "$url")"

# done
tsp -D "$idnum" notify-send "👍 Completed download!" "$filename" || notify-send "❌ Failed to donwload:" "$filename"
[ "$downloadtype" = "music" ] && tsp -D "$idnum" "mpc update"