Advertisement
someguy18

mega-dl

Feb 17th, 2018
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.81 KB | None | 0 0
  1. #!/bin/bash
  2. # Formats mega links for download or obfuscation
  3.  
  4. function help {
  5.     script=$(basename "$0")
  6.     echo "Help:"
  7.     echo ${script}" 'link'     # download mega link"
  8.     echo ${script}" -b 'link'  # open mega link in browser"
  9.     echo ${script}" -d 'link'  # print mega link without downloading"
  10.     echo ${script}" -o 'link'  # prints an obfuscated mega link"
  11.     echo ${script}" -h         # displays the help menu"
  12. }
  13.  
  14. dl=true
  15. useTools=true
  16. hideLink=false
  17. case "$1" in
  18.     -h|--h|-help|--help )
  19.         help
  20.         exit 0
  21.         ;;
  22.     -d|--d|-D|--D )
  23.         link="$2"
  24.         dl=false
  25.         ;;
  26.     -b|--b|-B|--B )
  27.         link="$2"
  28.         useTools=false
  29.         ;;
  30.     -o|--o|-O|--O )
  31.         link="$2"
  32.         hideLink=true
  33.         ;;
  34.     -* )
  35.         echo "Invalid argument: '$1'"
  36.         help
  37.         exit 0
  38.         ;;
  39.     * ) link="$1";;
  40. esac
  41.  
  42. if [[ -z "${link}" ]]; then
  43.     echo "No links entered."
  44.     exit 0
  45. fi
  46.  
  47. link=$(echo "$link" | tr -d '\r\n[:space:]' ) # remove any newline or whitespace characters
  48. link="${link##*:}"        # extract the link from mega:   links
  49. link="${link##*/}"        # extract the link from mega.nz links
  50. link='#!'${link//'%'/'!'} # ensure special characters are present and properly formatted
  51. link=${link//'!#'/''}     # remove any extraneous characters
  52. link=${link//'!!'/'!'}    # remove any extraneous characters
  53.  
  54. if [ "${hideLink}" = true ]; then
  55.     link=$(echo "$link" | sed "s/!/\n%/g" | tail -n2)
  56.     echo "#${link}"
  57.     exit 0
  58. fi
  59.  
  60. link="https://mega.nz/${link}"
  61.  
  62. if [ "${dl}" = true ]; then
  63.     if [ "${useTools}" = true ]; then
  64.         if [[ -z "$(which megadl)" ]]; then
  65.             echo "'megadl' not found."
  66.             read -p "Would you like to open the link in your default browser (y/n)? " choice
  67.             case "$choice" in
  68.                 y|Y|yes|Yes ) xdg-open "${link}";;
  69.                 * ) echo "Please install megatools.";;
  70.             esac
  71.         else
  72.             megadl "${link}"
  73.         fi
  74.     else
  75.         xdg-open "${link}"
  76.     fi
  77. else
  78.     echo "${link}"
  79. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement