Files
repos_scripts/download
2024-06-02 20:06:46 +05:30

147 lines
5.2 KiB
Bash
Executable File

#!/bin/sh
# script for downloading videos/audios from sites...
#shellcheck disable=SC2317
info() {
printf "\033[1;36m[ %s ] [ %s ] %s\033[0m\n" "$2" "$3" "$1"
}
error() {
printf "\033[1;31m[ %s ] [ %s ] %s\033[0m\n" "$2" "$3" "$1"
}
notify_droid() {
termux-notification -c "$1"
}
notify_linux() {
notify-send -e "$1" -h "string:x-canonical-private-synchronous:${0##*/}"
}
conv_to_octal(){
base=$2
i=0
res=0
for key in $(printf "%s" "$1" | rev | sed 's/./&\n/g'); do
if [ "$key" -ne 0 ];then
j=$i
tmp=1
while [ "$j" -ne 0 ];do
: $((tmp *= base))
: $((j-=1))
done
res=$((res + (key * tmp)))
fi
: $((i+=1))
done
res=$(printf '%03o' "$((res-${3}))")
#shellcheck disable=SC2059
printf "\\$res" | sed 's/[\/&]/\\&/g'
}
instagram() {
info "Using Alternate Site" "Instagram" "$2"
sed_file="${TMPDIR:-/tmp}/${0##*/}-sed-file"
response=$(curl -s "https://snapinsta.app" -o /dev/null -c - | curl -s 'https://snapinsta.app/action2.php' -e 'https://snapinsta.app/' -A 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' -b - -F "url=$1" -F 'action=post' -F 'lang=' --compressed | sed -nE 's|.*\}\(([^\)]*)\).*|\1|p' | tr -d '"' | tr ',' '\n')
n=$(printf "%s" "$response" | sed -n '3p')
t=$(printf "%s" "$response" | sed -n '4p')
e=$(printf "%s" "$response" | sed -n '5p')
printf '%s' "$n" | cut -c-"$e" | sed 's/./&\n/g' | tr -s '\n' | nl -nln -v0 | sed -nE 's|^([0-9]*)[[:space:]]*(.*)|s\/\2\/\1\/g|p' > "$sed_file"
printf 's/%s/\\n/g' "$(printf "%s" "$n" | cut -c"$((e+1))")" >> "$sed_file"
data=$(printf "%s" "$response" | head -1 | sed -f "$sed_file" | sed -n '1200,$p')
info "Decoding JS" "Instagram" "$2"
printf "" > "$sed_file"
for num in $(printf "%s" "$data" | sort -u); do
printf 's/^%s$/%s/g\n' "$num" "$(conv_to_octal "$num" "$e" "$t")" >> "$sed_file" &
done
wait
video_url=$(printf "%s" "$data" | sed -f "$sed_file" -e 's/\\//g' | tr -d '\n' | sed -nE 's|.*a href="([^"]*)".*|\1|p')
}
#main
link="$1"
agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/12$(head /dev/urandom | tr -dc '2-5' | cut -c1).0.0.0 Safari/537.36"
case $(uname -o) in
*ndroid*)
download_dir="/sdcard"
[ -z "$link" ] && link=$(termux-clipboard-get)
os="droid"
;;
*)
download_dir="$HOME"
[ -z "$link" ] && link=$(wl-paste)
os="linux"
;;
esac
printf "\033[1;34m Video link :\033[0m%s\n" "$link"
case "$link" in
*instagram*)
app="Instagram"
id=$(printf "%s" "$link" | cut -d'/' -f5)
[ -z "$id" ] && error "Unable to extract ID" "$app" "$id"
info "Decoding ID" "$app" "$id"
res=0
enc_char="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
table=$(printf "%s" "$enc_char" | sed 's/\(.\)/\1\n/g' | nl -v0 | tr '\t' ':' | tr -d ' ')
for key in $(printf "%s" "$id" | sed -e 's/\(.\)/\1\n/g');do
value=$(printf "%s" "$table" | sed -nE "s/([^:]*):$key/\1/p")
res=$((res*64+value))
done
info "Fetching Video URL" "$app" "$id"
video_url=$(curl -s "https://i.instagram.com/api/v1/web/get_ruling_for_content/?content_type=MEDIA&target_id=$res" -H "x-ig-app-id:936619743392459" -H "x-ig-www-claim:0" -A "$agent" -c - -o /dev/null | curl -A "$agent" -s -G "https://www.instagram.com/graphql/query/" -H "content-type:application/json" --data-urlencode 'query_hash=9f8827793ef34641b2fb195d4d41151c' --data-urlencode 'variables={"shortcode":"'"$id"'","include_reel":false,"include_logged_out":false}' -e "$link" -b - | tr '{}' '\n' | sed -nE 's|.*video_url":"([^"]*)".*|\1|p' | sed 's/\u0026/\&/g;s/\\//g')
printf "%s" "$video_url" | grep -qE 'instagram.*mp4' || instagram "$link" "$id"
;;
*youtu*)
app="Youtube"
id=$(printf "%s" "$link" | cut -d"=" -f2 | cut -d"/" -f4)
[ -z "$id" ] && error "Unable to extract ID" "$app" "$id"
info "Fetching Video URL" "$app" "$id"
yt_ver="2.20231219.04.00"
json="{
\"context\": {
\"client\": {
\"clientName\": \"WEB\",
\"clientVersion\": \"$yt_ver\",
\"userAgent\": \"$agent\",
\"hl\": \"en\",
\"timeZone\": \"Asia/Calcutta\",
\"utcOffsetMinutes\": 330
}
},
\"videoId\": \"$id\",
\"playbackContext\": {
\"contentPlaybackContext\": {
\"html5Preference\": \"HTML5_PREF_WANTS\"
}
},
\"contentCheckOk\": true,
\"racyCheckOk\": true
}"
video_url=$(curl -X POST -A "$agent" -s "https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false" -H "content-type:application/json" -H "X-Youtube-Client-Version: $yt_ver" -d "$json" -e "$link" | tr '{}' '\n' | sed -nE 's|.*,"title":"([^"]*)".*|\tout=\1\.mp4|p;s|.*itag":18,"url":"([^"]*)".*|\1|p;s|.*itag":22,"url":"([^"]*)".*|\1|p' | tail -2)
printf "%s" "$video_url" | grep -q "googlevideo\.com" || fallback=1
;;
*)
info "Invalid URL" "none" "none"
exit 0
;;
esac
if [ -n "$fallback" ]; then
info "Using fallback" "$app" "$id"
yt-dlp "$link" -o "$download_dir/%(title)s.%(ext)s"
else
info "Downloading Video" "$app" "$id"
printf "%s" "$video_url" | aria2c -x16 -s16 -j10 -k'1M' --user-agent="$agent" -d "$download_dir" -i - --download-result=hide --summary-interval=0 || notify_$os "Unable to Download $app Video"
fi
notify_$os "$app Video Downloaded"
exit 0