summaryrefslogtreecommitdiff
path: root/scripts/socialdl
blob: f9f046ee05dd53725397d0b1e57098eb3630abc5 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env bash

# set -eou pipefail

branding() {
    echo
    cat <<'EOF'
  /$$$$$$   /$$$$$$   /$$$$$$  /$$$$$$  /$$$$$$  /$$       /$$$$$$$  /$$
 /$$__  $$ /$$__  $$ /$$__  $$|_  $$_/ /$$__  $$| $$      | $$__  $$| $$
| $$  \__/| $$  \ $$| $$  \__/  | $$  | $$  \ $$| $$      | $$  \ $$| $$
|  $$$$$$ | $$  | $$| $$        | $$  | $$$$$$$$| $$      | $$  | $$| $$
 \____  $$| $$  | $$| $$        | $$  | $$__  $$| $$      | $$  | $$| $$
 /$$  \ $$| $$  | $$| $$    $$  | $$  | $$  | $$| $$      | $$  | $$| $$
|  $$$$$$/|  $$$$$$/|  $$$$$$/ /$$$$$$| $$  | $$| $$$$$$$$| $$$$$$$/| $$$$$$$$
 \______/  \______/  \______/ |______/|__/  |__/|________/|_______/ |________/
EOF
echo
}

# Check for dependencies
check_deps=$(which {gum,yt-dlp})
if [ ! "$check_deps" ]; then
    echo
    gum log \
        --structured \
        --level error \
        "gum and yt-dlp not installed."
    echo
    gum spin \
        --spinner "globe" \
        --title "Done! Press any key to close..." \
        -- bash -c 'read -n 1 -s'
    exit 1
fi

# Check for download directories
# if doesn't exist make them
insta_vid_dl_dir="$HOME/Videos/Instagram"
insta_img_dl_dir="$HOME/Pictures/Instagram"
x_vid_dl_dir="$HOME/Videos/X"
x_img_dl_dir="$HOME/Pictures/X"
reddit_img_dl_dir="$HOME/Pictures/Reddit"
reddit_vid_dl_dir="$HOME/Videos/Reddit"
# Making directories if doesn't exist
if [ ! -d "$insta_vid_dl_dir" ]; then
    mkdir -p "$insta_vid_dl_dir"
elif [[ ! -d "$insta_img_dl_dir" ]]; then
    mkdir -p "$insta_img_dl_dir"
elif [[ ! -d "$x_vid_dl_dir" ]]; then
    mkdir -p "$x_vid_dl_dir"
elif [[ ! -d "$x_img_dl_dir" ]]; then
    mkdir -p "$x_img_dl_dir"
elif [[ ! -d "$reddit_img_dl_dir" ]]; then
    mkdir -p "$reddit_img_dl_dir"
elif [[ ! -d "$reddit_vid_dl_dir" ]]; then
    mkdir -p "$reddit_vid_dl_dir"
fi

# TODO: Add comments explaining the commands
# This here is a simple function which waits for the user
# to press a key and exits out
gum_done() {
    gum spin \
        --spinner "globe" \
        --title "Done! Press any key to close..." \
        -- bash -c 'read -n 1 -s'
}

gum_error() {
    gum log --structured --level error "$1"
}
branding
# Platform to download from
platform_choice=$(gum choose \
    --header="Choose:" \
    --header.bold \
    --header.foreground="#FCB8EF" \
    "Instagram" "X" "Reddit")
# Apply methods to determine which platform is choosen
# Accordingly, choose how to download
case "$platform_choice" in
    Instagram)
        # Taking input whether downloading videos or images
        insta_format_choice=$(gum choose \
            --header="Choose:" \
            --header.bold \
            --header.foreground="#FCB8EF" \
            "Video" "Image" || exit 1)
        case "$insta_format_choice" in
            Video)
                # Taking instagram video url
                insta_vid_url=$(gum input \
                    --header.bold \
                    --header.foreground="#FCB8EF" \
                    --header="Paste Instagram video URL." \
                    --placeholder="Enter URL here...")
                if [[ -n "$insta_vid_url" ]]; then
                    yt-dlp "$insta_vid_url" -o "$insta_vid_dl_dir/%(title)s.%(ext)s" || exit 1
                    echo
                    gum_done
                    echo
                elif [[ -z "$insta_vid_url" ]]; then
                    echo
                    gum_error "Please input a URL."
                    echo
                    gum_done
                fi
                ;;
            Image)
                if command -v gallery-dl >/dev/null; then
                    # Taking instagram image url
                    insta_img_url=$(gum input \
                        --header.bold \
                        --header.foreground="#FCB8EF" \
                        --header="Paste Instagram image URL." \
                        --placeholder="Enter URL here...")
                    # Instagram requires login to download images; that is why
                    # cookies from browser is required
                    gallery-dl --cookies-from-browser chromium --destination "$insta_img_dl_dir" "$insta_img_url"
                    echo
                    gum_done
                    echo
                else
                    gum_error "gallery-dl is not installed."
                    echo
                    gum_done
                    echo
                fi
                ;;
        esac
        ;;
    X)
        # Taking input whether to download video or image from X
        x_format_choice=$(gum choose \
            --header="Choose:" \
            --header.bold \
            --header.foreground="#FCB8EF" \
            "Video" "Image" || exit 1)
        case "$x_format_choice" in
            Video)
                # Taking input of X video url
                x_vid_url=$(gum input \
                    --header.bold \
                    --header.foreground="#FCB8EF" \
                    --header="Paste X video URL." \
                    --placeholder="Paste URL here...")
                if [ -z "$x_vid_url" ]; then
                    # Give out error if no url provided
                    echo
                    gum_error "Please enter a URL."
                    echo
                    gum_done
                elif [[ -n "$x_vid_url" ]]; then
                    # Output directory with title and extension required as
                    # downloaded file is being downloaded to a particular
                    # directory not the dir where script is being executed
                    yt-dlp "$x_vid_url" -o "$x_vid_dl_dir/%(title)s.%(ext)s" || exit 1
                    echo
                    gum_done
                    echo
                fi
                ;;
            Image)
                if command -v gallery-dl >/dev/null; then
                    # Take input of image url from instagram
                    x_img_url=$(gum input \
                        --header.bold \
                        --header.foreground="#FCB8EF" \
                        --header="Paste X image URL." \
                        --placeholder="Paste URL here...")
                    if [ -z "$x_img_url" ]; then
                        # If no url given then throw error and exit
                        echo
                        gum_error "Please enter a URL."
                        echo
                    elif [[ -n "$x_img_url" ]]; then
                        # FIX: Change the default browser to automatically continue
                        # if supported browser is default otherwise exit
                        gallery-dl --cookies-from-browser chromium --destination "$x_img_dl_dir" "$x_img_url"
                        echo
                        gum_done
                        echo
                    fi
                else
                    echo
                    gum_error "gallery-dl is not installed."
                    echo
                    gum_done
                fi
                ;;
        esac
        ;;
    Reddit)
        reddit_format_choice=$(gum choose \
            --header="Choose:" \
            --header.bold \
            --header.foreground="#FCB8EF" \
            "Video" "Image" || exit 1)
        case "$reddit_format_choice" in
            Video)
                reddit_vid_url=$(gum input \
                    --header.bold \
                    --header.foreground="#FCB8EF" \
                    --header="Paste Reddit video URL." \
                    --placeholder="Paste URL here...")
                if [ -z "$reddit_vid_url" ]; then
                    echo
                    gum_error "Enter a URL..."
                    echo
                elif [ -n "$reddit_vid_url" ]; then
                    yt-dlp "$reddit_vid_url" -o "$reddit_vid_dl_dir/%(title)s.%(ext)s" || exit 1
                    echo
                    gum_done
                    echo
                fi
                ;;
            Image)
                echo
                gum_error "Image download on Reddit not yet supported."
                echo
                ;;
        esac
        ;;
esac