socialdl (8125B)
1 #!/usr/bin/env bash 2 3 # set -eou pipefail 4 5 branding() { 6 echo 7 cat <<'EOF' 8 /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$$$$$$ /$$ 9 /$$__ $$ /$$__ $$ /$$__ $$|_ $$_/ /$$__ $$| $$ | $$__ $$| $$ 10 | $$ \__/| $$ \ $$| $$ \__/ | $$ | $$ \ $$| $$ | $$ \ $$| $$ 11 | $$$$$$ | $$ | $$| $$ | $$ | $$$$$$$$| $$ | $$ | $$| $$ 12 \____ $$| $$ | $$| $$ | $$ | $$__ $$| $$ | $$ | $$| $$ 13 /$$ \ $$| $$ | $$| $$ $$ | $$ | $$ | $$| $$ | $$ | $$| $$ 14 | $$$$$$/| $$$$$$/| $$$$$$/ /$$$$$$| $$ | $$| $$$$$$$$| $$$$$$$/| $$$$$$$$ 15 \______/ \______/ \______/ |______/|__/ |__/|________/|_______/ |________/ 16 EOF 17 echo 18 } 19 20 # Check for dependencies 21 check_deps=$(which {gum,yt-dlp}) 22 if [ ! "$check_deps" ]; then 23 echo 24 gum log \ 25 --structured \ 26 --level error \ 27 "gum and yt-dlp not installed." 28 echo 29 gum spin \ 30 --spinner "globe" \ 31 --title "Done! Press any key to close..." \ 32 -- bash -c 'read -n 1 -s' 33 exit 1 34 fi 35 36 # Check for download directories 37 # if doesn't exist make them 38 insta_vid_dl_dir="$HOME/Videos/Instagram" 39 insta_img_dl_dir="$HOME/Pictures/Instagram" 40 x_vid_dl_dir="$HOME/Videos/X" 41 x_img_dl_dir="$HOME/Pictures/X" 42 reddit_img_dl_dir="$HOME/Pictures/Reddit" 43 reddit_vid_dl_dir="$HOME/Videos/Reddit" 44 # Making directories if doesn't exist 45 if [ ! -d "$insta_vid_dl_dir" ]; then 46 mkdir -p "$insta_vid_dl_dir" 47 elif [[ ! -d "$insta_img_dl_dir" ]]; then 48 mkdir -p "$insta_img_dl_dir" 49 elif [[ ! -d "$x_vid_dl_dir" ]]; then 50 mkdir -p "$x_vid_dl_dir" 51 elif [[ ! -d "$x_img_dl_dir" ]]; then 52 mkdir -p "$x_img_dl_dir" 53 elif [[ ! -d "$reddit_img_dl_dir" ]]; then 54 mkdir -p "$reddit_img_dl_dir" 55 elif [[ ! -d "$reddit_vid_dl_dir" ]]; then 56 mkdir -p "$reddit_vid_dl_dir" 57 fi 58 59 # TODO: Add comments explaining the commands 60 # This here is a simple function which waits for the user 61 # to press a key and exits out 62 gum_done() { 63 gum spin \ 64 --spinner "globe" \ 65 --title "Done! Press any key to close..." \ 66 -- bash -c 'read -n 1 -s' 67 } 68 69 gum_error() { 70 gum log --structured --level error "$1" 71 } 72 branding 73 # Platform to download from 74 platform_choice=$(gum choose \ 75 --header="Choose:" \ 76 --header.bold \ 77 --header.foreground="#FCB8EF" \ 78 "Instagram" "X" "Reddit") 79 # Apply methods to determine which platform is choosen 80 # Accordingly, choose how to download 81 case "$platform_choice" in 82 Instagram) 83 # Taking input whether downloading videos or images 84 insta_format_choice=$(gum choose \ 85 --header="Choose:" \ 86 --header.bold \ 87 --header.foreground="#FCB8EF" \ 88 "Video" "Image" || exit 1) 89 case "$insta_format_choice" in 90 Video) 91 # Taking instagram video url 92 insta_vid_url=$(gum input \ 93 --header.bold \ 94 --header.foreground="#FCB8EF" \ 95 --header="Paste Instagram video URL." \ 96 --placeholder="Enter URL here...") 97 if [[ -n "$insta_vid_url" ]]; then 98 yt-dlp "$insta_vid_url" -o "$insta_vid_dl_dir/%(title)s.%(ext)s" || exit 1 99 echo 100 gum_done 101 echo 102 elif [[ -z "$insta_vid_url" ]]; then 103 echo 104 gum_error "Please input a URL." 105 echo 106 gum_done 107 fi 108 ;; 109 Image) 110 if command -v gallery-dl >/dev/null; then 111 # Taking instagram image url 112 insta_img_url=$(gum input \ 113 --header.bold \ 114 --header.foreground="#FCB8EF" \ 115 --header="Paste Instagram image URL." \ 116 --placeholder="Enter URL here...") 117 # Instagram requires login to download images; that is why 118 # cookies from browser is required 119 gallery-dl --cookies-from-browser chromium --destination "$insta_img_dl_dir" "$insta_img_url" 120 echo 121 gum_done 122 echo 123 else 124 gum_error "gallery-dl is not installed." 125 echo 126 gum_done 127 echo 128 fi 129 ;; 130 esac 131 ;; 132 X) 133 # Taking input whether to download video or image from X 134 x_format_choice=$(gum choose \ 135 --header="Choose:" \ 136 --header.bold \ 137 --header.foreground="#FCB8EF" \ 138 "Video" "Image" || exit 1) 139 case "$x_format_choice" in 140 Video) 141 # Taking input of X video url 142 x_vid_url=$(gum input \ 143 --header.bold \ 144 --header.foreground="#FCB8EF" \ 145 --header="Paste X video URL." \ 146 --placeholder="Paste URL here...") 147 if [ -z "$x_vid_url" ]; then 148 # Give out error if no url provided 149 echo 150 gum_error "Please enter a URL." 151 echo 152 gum_done 153 elif [[ -n "$x_vid_url" ]]; then 154 # Output directory with title and extension required as 155 # downloaded file is being downloaded to a particular 156 # directory not the dir where script is being executed 157 yt-dlp "$x_vid_url" -o "$x_vid_dl_dir/%(title)s.%(ext)s" || exit 1 158 echo 159 gum_done 160 echo 161 fi 162 ;; 163 Image) 164 if command -v gallery-dl >/dev/null; then 165 # Take input of image url from instagram 166 x_img_url=$(gum input \ 167 --header.bold \ 168 --header.foreground="#FCB8EF" \ 169 --header="Paste X image URL." \ 170 --placeholder="Paste URL here...") 171 if [ -z "$x_img_url" ]; then 172 # If no url given then throw error and exit 173 echo 174 gum_error "Please enter a URL." 175 echo 176 elif [[ -n "$x_img_url" ]]; then 177 # FIX: Change the default browser to automatically continue 178 # if supported browser is default otherwise exit 179 gallery-dl --cookies-from-browser chromium --destination "$x_img_dl_dir" "$x_img_url" 180 echo 181 gum_done 182 echo 183 fi 184 else 185 echo 186 gum_error "gallery-dl is not installed." 187 echo 188 gum_done 189 fi 190 ;; 191 esac 192 ;; 193 Reddit) 194 reddit_format_choice=$(gum choose \ 195 --header="Choose:" \ 196 --header.bold \ 197 --header.foreground="#FCB8EF" \ 198 "Video" "Image" || exit 1) 199 case "$reddit_format_choice" in 200 Video) 201 reddit_vid_url=$(gum input \ 202 --header.bold \ 203 --header.foreground="#FCB8EF" \ 204 --header="Paste Reddit video URL." \ 205 --placeholder="Paste URL here...") 206 if [ -z "$reddit_vid_url" ]; then 207 echo 208 gum_error "Enter a URL..." 209 echo 210 elif [ -n "$reddit_vid_url" ]; then 211 yt-dlp "$reddit_vid_url" -o "$reddit_vid_dl_dir/%(title)s.%(ext)s" || exit 1 212 echo 213 gum_done 214 echo 215 fi 216 ;; 217 Image) 218 echo 219 gum_error "Image download on Reddit not yet supported." 220 echo 221 ;; 222 esac 223 ;; 224 esac