aboutsummaryrefslogtreecommitdiff
path: root/scripts/yt
blob: ba7feb2087a61ead893e96c03553d6af4a3c0e1f (about) (plain) (blame)
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
#!/bin/sh
set -eu

export DEFAULT_PLAYLIST_END=15
export DEFAULT_INC_STEP=10
export YT_TEMPLATE="${HOME}/Downloads/yt-dl/%(uploader)s/%(upload_date)s %(title)s.%(ext)s"

download() {
  youtube-dl "$1" \
             --download-archive ~/archive/youtube-dl-seen.conf \
             --prefer-free-formats                             \
             --playlist-end "$2"                               \
             --write-description                               \
             --output "$YT_TEMPLATE"
}

download_user() {
  download "https://www.youtube.com/user/$1" "${2-$DEFAULT_PLAYLIST_END}"
}

download_channel() {
  download "https://www.youtube.com/channel/$1" "${2-$DEFAULT_PLAYLIST_END}"
}

download_playlist() {
  download "https://www.youtube.com/playlist?list=$1" "${2-$DEFAULT_PLAYLIST_END}"
}

inc_download() {
  fn="$1"
  id="$2"
  step="${3-$DEFAULT_INC_STEP}"

  file="$HOME/.yt-db/$id"
  mkdir -p "$HOME/.yt-db"
  cat "$file" 2> /dev/null
  n_count="$(cat "$file" 2> /dev/null || printf 10)"
  n_count_new="$((n_count + step))"

  echo "$n_count_new" > "$file"

  "$fn" "$id" "$n_count"
}

# Always downloads video, doesn't look at the download-archive

URL_OR_PATH="${1:-}"
[ -z "${URL_OR_PATH}" ] && {
  # shellcheck disable=2016
  echo 'Input "$URL_OR_PATH" is undefined.' > /dev/stderr
  exit 2
}

if [ -f "${URL_OR_PATH}" ]; then
  echo "File '${URL_OR_PATH}' exists, loading URLs from it..." > /dev/stderr
  youtube-dl --batch-file "${URL_OR_PATH}" \
             --format best                 \
             --output "${YT_TEMPLATE}"     \
             --write-description           \
             1>&2
  FILES="$(youtube-dl --batch-file "${URL_OR_PATH}" \
                     --output "${YT_TEMPLATE}"      \
                      --format best                 \
                     --get-filename)"
else
  echo "File '${URL_OR_PATH}' doesn't exist, treating it as an URL..." > /dev/stderr
  youtube-dl "${URL_OR_PATH}"          \
             --output "${YT_TEMPLATE}" \
             --format best             \
             --write-description       \
             1>&2
  FILES="$(youtube-dl "${URL_OR_PATH}"         \
                     --output "${YT_TEMPLATE}" \
                     --format best             \
                     --get-filename)"
fi

for f in $FILES; do
  videq "$f"
done