summaryrefslogtreecommitdiffstatshomepage
path: root/scripts
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2021-06-24 21:31:21 -0500
committerToby Vincent <tobyv13@gmail.com>2021-06-24 21:31:21 -0500
commitf7dd83e5c58ae8b98a0cf595049173f34804e915 (patch)
tree7d0bce953841509f856b60e54ca857bf6396fafe /scripts
parent194e92a3b6d31d171881d379301e0ed230749f9b (diff)
feat: added output path argument
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/mega-dl.sh53
1 files changed, 40 insertions, 13 deletions
diff --git a/scripts/mega-dl.sh b/scripts/mega-dl.sh
index 433c9e7..b19be68 100755
--- a/scripts/mega-dl.sh
+++ b/scripts/mega-dl.sh
@@ -36,13 +36,24 @@
# $ mv -f file.name.new file.name
URL=""
+OUTPATH="$PWD"
+USAGE="Usage: ${0##*/} <url>"
if [[ $1 =~ ^https?:\/\/mega(\.co)?\.nz ]]; then
URL="$1"
fi
if [[ ! $URL ]]; then
- echo "Usage: ${0##*/} <url>" >&2
+ echo $USAGE >&2
+ exit 1
+fi
+
+if [[ ! -z "$2" ]]; then
+ OUTPATH="$2"
+fi
+
+if [[ ! -d "$OUTPATH" ]]; then
+ echo $USAGE >&2
exit 1
fi
@@ -60,35 +71,51 @@ if $missing; then
fi
if [[ $URL =~ .*/file/[^#]*#[^#]* ]]; then
- id="${URL#*file/}"; id="${id%%#*}"
- key="${URL##*file/}"; key="${key##*#}"
+ id="${URL#*file/}"
+ id="${id%%#*}"
+ key="${URL##*file/}"
+ key="${key##*#}"
else
- id="${URL#*!}"; id="${id%%!*}"
+ id="${URL#*!}"
+ id="${id%%!*}"
key="${URL##*!}"
fi
raw_hex=$(echo "${key}=" | tr '\-_' '+/' | tr -d ',' | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
-hex=$(printf "%016x" \
- $(( 0x${raw_hex:0:16} ^ 0x${raw_hex:32:16} )) \
- $(( 0x${raw_hex:16:16} ^ 0x${raw_hex:48:16} ))
+hex=$(
+ printf "%016x" \
+ $((0x${raw_hex:0:16} ^ 0x${raw_hex:32:16})) \
+ $((0x${raw_hex:16:16} ^ 0x${raw_hex:48:16}))
)
-json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "g":"1", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1; json="${json#"[{"}"; json="${json%"}]"}"
-file_url="${json##*'"g":'}"; file_url="${file_url%%,*}"; file_url="${file_url//'"'/}"
+json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "g":"1", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1
+json="${json#"[{"}"
+json="${json%"}]"}"
+file_url="${json##*'"g":'}"
+file_url="${file_url%%,*}"
+file_url="${file_url//'"'/}"
json=$($CURL -s -H 'Content-Type: application/json' -d '[{"a":"g", "p":"'"$id"'"}]' 'https://g.api.mega.co.nz/cs?id=&ak=') || exit 1
-at="${json##*'"at":'}"; at="${at%%,*}"; at="${at//'"'/}"
+at="${json##*'"at":'}"
+at="${at%%,*}"
+at="${at//'"'/}"
-json=$(echo "${at}==" | tr '\-_' '+/' | tr -d ',' | openssl enc -a -A -d -aes-128-cbc -K "$hex" -iv "00000000000000000000000000000000" -nopad | tr -d '\0'); json="${json#"MEGA{"}"; json="${json%"}"}"
+json=$(echo "${at}==" | tr '\-_' '+/' | tr -d ',' | openssl enc -a -A -d -aes-128-cbc -K "$hex" -iv "00000000000000000000000000000000" -nopad | tr -d '\0')
+json="${json#"MEGA{"}"
+json="${json%"}"}"
file_name="${json##*'"n":'}"
if [[ $file_name == *,* ]]; then
file_name="${file_name%%,*}"
fi
file_name="${file_name//'"'/}"
-curl -s "$file_url" | openssl enc -d -aes-128-ctr -K "$hex" -iv "${raw_hex:32:16}0000000000000000" > "$file_name"
+outfile="$OUTPATH/$file_name"
+
+$CURL -s "$file_url" | openssl enc -d -aes-128-ctr -K "$hex" -iv "${raw_hex:32:16}0000000000000000" >"$outfile"
+
+echo "$outfile"
# echo "$file_url"
# echo "$file_name"
# echo "$hex"
-# echo "${raw_hex:32:16}0000000000000000" \ No newline at end of file
+# echo "${raw_hex:32:16}0000000000000000"