aboutsummaryrefslogtreecommitdiff
path: root/fetch.sh
blob: 446e150f10af6985a3bbab5d6e42a95d6018c837 (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
#!/bin/sh

# arg1 is the url and arg2 is the playlist name
# if arg1 is not defined. just fetch thumbnails

# cant do whats below because i need to replace all ' within a subset of 'string' with '' to escape it.

# yt-dlp --get-filename -o "insert into ytlist values('%(title)s','%(channel)s','%(id)s','shorts','');" "https://www.youtube.com/playlist?list=PLwpvCCyacwS9Us6F1_UUUre1Py9p4ex4J" 2> out2.txt #| sed "s/'/''/g" #| sqlite3 ./youtube_stuffs.db

cd "$(dirname "$0")" || exit
file="./download.txt"
emptyvar=""

[ "$3" = "t" ] && Cookies="--cookies cookies.txt"

if [ "$1" ]; then
    [ -z "$2" ] && echo playlist not defined. continue? && printf "continue? (press any key)" && read -r emptyvar
    yt-dlp $Cookies --get-filename -o "%(title)s----%(id)s----%(channel)s" "$1" > "$file" 2> download_err.txt 
    printf "\n\n"

    i=1
    max=$(( $(wc -l "$file" | sed 's/ .*//g') ))
    while :; do
        printf -- "insert into ytlist values('%s','%s','')" "$(head -n $i $file | tail -n 1 | sed "s/'/''/g" | sed "s/----/','/g" )" "$2"
        printf "\n"
        printf -- "insert into ytlist values('%s','%s','')" "$(head -n $i $file | tail -n 1 | sed "s/'/''/g" | sed "s/----/','/g" )" "$2" | sqlite3 ./youtube_stuffs.db
        printf "insert: %s\n" "%i"
        i=$((i+1))
        if [ $i -gt $max ]; then
            break
        fi
    done
    echo ----PLAYLIST DONE
    printf "continue? (Y,n)"
    read -r input
    [ "$input" = "n" ] && exit 1;
    [ "$input" = "N" ] && exit 1;
fi


echo ----Downloading Thumbnails
i=1
max=$(( $(sqlite3 ./youtube_stuffs.db 'select rowid from ytlist order by rowid desc limit 1;') ))
while :; do
    urlid="$(sqlite3 ./youtube_stuffs.db 'select id from ytlist where rowid='$i';')"

    if [ "$urlid" ] && [ ! -e "./thumbnails/$urlid."* ]; then
        printf "%s::%s\n" "$i" "$urlid"
        thumbnailurl="$(yt-dlp $Cookies --get-thumbnail "https://youtube.com/watch?v=$urlid" 2>> thumberr.txt)"
        if [ ! "$thumbnailurl" = "" ]; then
            ext="$(printf -- "%s" "$thumbnailurl" | sed 's/.*\.//g' | sed 's/?.*//')"
            wget "$thumbnailurl" -O "thumbnails/$urlid.$ext"
        else
            printf "could not download thumbnail %s\n" "$urlid"
        fi
    fi

    i=$((i+1))
    if [ $i -gt $max ]; then
        break
    fi
done