Advertisement
drpanwe

vaultscript

Oct 5th, 2020 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.60 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [ $# -eq 0 ]; then
  4.     echo "No arguments supplied. Example: ./script.sh https://vimm.net/vault/GB"
  5.     exit 1
  6. fi
  7.  
  8. arg="$1"
  9. console=$(echo "$arg" | awk -F '/' '{print $(NF)}')
  10. echo "$console"
  11. for letter in \# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do
  12.     # Construct letterpage
  13.     if [ "$letter" = "#" ]; then
  14.         letterpage="https://vimm.net/vault/?p=list&system=$console&section=number"
  15.     else
  16.         letterpage="https://vimm.net/vault/$console/$letter"
  17.     fi
  18.  
  19.     echo
  20.     echo "$letter: $letterpage"
  21.     echo "======================="
  22.  
  23.     # Base URL (should be without trailing slash)
  24.     baseURL="https://vimm.net"
  25.  
  26.     # find all gamepages in a letterpage
  27.     for gamepage in $(curl -s "$letterpage" | grep 'a href="\/vault' | awk -F 'href="' '{print $2}' | cut -d '"' -f1 | grep -v '?p=' | grep -v "NES" | grep -v "$console" | grep -o '[[:digit:]]*'); do
  28.         # Construct the gamepage URL
  29.         gamepage="$baseURL/vault/$gamepage"
  30.  
  31.         skipit=false
  32.         retries=1
  33.  
  34.         # Find the mediaID for this specific game page
  35.         while true; do
  36.             echo "Looking for mediaID into: $gamepage"
  37.             id=$(curl -s "$gamepage" | grep 'var media = {"ID":' | awk -F ':' '{ print $2}' | awk -F ',' '{print $1}')
  38.             if [ -z "$id" ]; then
  39.                 echo "\$id is empty. Retrying ...$retries"
  40.                 ((retries = retries + 1))
  41.  
  42.                 # But ... if you re-tried 10 times already, then give up on this one
  43.                 if [[ "$retries" -gt 10 ]]; then
  44.                     skipit=true
  45.                     break
  46.                 fi
  47.             else
  48.                 echo "FOUND! ID is: $id"
  49.                 break
  50.             fi
  51.         done
  52.  
  53.         # If you bailed out because the mediaID was not found, then proceed to the next gamepage
  54.         if [ "$skipit" = true ]; then
  55.             echo 'Giving up on this one...'
  56.             echo "$id@$console@$gamepage" >>"$console.err"
  57.             continue
  58.         fi
  59.  
  60.         # Find Download URL
  61.         skipit=false
  62.         retries=1
  63.         while true; do
  64.             echo "Looking for download URL"
  65.             URL=$(curl -s "$gamepage" | grep '<form action="//' | awk -F '<form action="//' '{ print $2 }' | awk -F '"' '{ print $1 }')
  66.             if [ -z "$URL" ]; then
  67.                 echo "\$URL is empty. Retrying ...$retries"
  68.                 ((retries = retries + 1))
  69.  
  70.                 # But ... if you re-tried 10 times already, then give up on this one
  71.                 if [[ "$retries" -gt 10 ]]; then
  72.                     skipit=true
  73.                     break
  74.                 fi
  75.             else
  76.                 echo "FOUND! URL is: $URL"
  77.                 break
  78.             fi
  79.         done
  80.  
  81.         # If you bailed out because the download URL was not found, then proceed to the next gamepage
  82.         if [ "$skipit" = true ]; then
  83.             echo 'Giving up on this one...'
  84.             echo
  85.             echo
  86.             echo "$id@$console@$gamepage" >>"$console.err"
  87.             continue
  88.         fi
  89.  
  90.         # Constrcut the download URL
  91.         # e.g. URL="https://download.vimm.net/download/?mediaId=$id"
  92.         URL="https://$URL?mediaId=$id"
  93.  
  94.         echo "Looking for filename into: $URL"
  95.  
  96.         # Find the filename  (Check there is 404, so skip it)
  97.         skipit=false
  98.         retries=1
  99.         while true; do
  100.             filename=$(curl -s --max-time 5 -D - \
  101.                 -H "Referer: https://vimm.net/" \
  102.                 -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" \
  103.                 "$URL" | grep "Content-Disposition" | awk -F 'filename="' '{ print $2 }' | awk -F '"' '{ print $1 }' 2>/dev/null)
  104.             if [ -z "$filename" ]; then
  105.                 echo "\$filename is empty. Retrying ... $retries"
  106.                 ((retries = retries + 1))
  107.  
  108.                 # But ... if you re-tried 10 times already, then give up on this one
  109.                 if [[ "$retries" -gt 10 ]]; then
  110.                     skipit=true
  111.                     break
  112.                 fi
  113.             else
  114.                 echo "FOUND! Filename is: $filename"
  115.                 break
  116.             fi
  117.         done
  118.  
  119.         #  If you bailed out because the filename was not found, then proceed to the next gamepage
  120.         if [ "$skipit" = true ]; then
  121.             echo 'Giving up on this one...'
  122.             echo
  123.             echo
  124.             echo "$id@$console@$gamepage@$URL@$filename" >>"$console.txt"
  125.             continue
  126.         fi
  127.  
  128.         # Download the rom
  129.         skipit=false
  130.         retries=1
  131.         while true; do
  132.             if curl \
  133.                 -H "Referer: https://vimm.net/" \
  134.                 -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" \
  135.                 "$URL" --output "$filename" --progress-bar; then
  136.                 echo "Start downloading $filename..."
  137.             fi
  138.  
  139.             # Check if the file integrity is correct
  140.             ZIP=false
  141.             SEVEN=false
  142.  
  143.             if test -f "$filename"; then
  144.                 echo "Check integrity..."
  145.                 if file "$filename" | grep 'Zip archive data\|7-zip archive data' &>/dev/null; then
  146.                     echo "VALID!"
  147.                     if file "$filename" | grep 'Zip archive data' &>/dev/null; then
  148.                         echo "Compression Type: Zip archive"
  149.                         ZIP=true
  150.                     fi
  151.                     if file "$filename" | grep '7-zip archive data' &>/dev/null; then
  152.                         echo "Compression Type: 7z archive"
  153.                         SEVEN=true
  154.                     fi
  155.                     if [ "$ZIP" = true ] || [ "$SEVEN" = true ]; then
  156.                         echo "It's a known compressed format"
  157.                     else
  158.                         echo "I don't know this compression format"
  159.                         ((retries = 11))
  160.                         break
  161.                     fi
  162.  
  163.                     # All fine, break the loop to proceed to the next step that is extraction
  164.                     break
  165.                 else
  166.                     # PROBLEM: In case compressed archive is damaged, retry 10 times or give up
  167.                     echo "INVALID! $filename is not a valid Zip/7z archive. Retrying ..."
  168.                     ((retries = retries + 1))
  169.                     # But ... if you re-tried 10 times already, then give up on this one
  170.                     if [[ "$retries" -gt 10 ]]; then
  171.                         skipit=true
  172.                         break
  173.                     else
  174.                         continue # retry (this will re-download the file)
  175.                     fi
  176.                 fi
  177.             else
  178.                 # PROBLEM: In case download failed, retry 10 times or give up
  179.                 echo "$filename has not been downloaded. Retrying ... $retries"
  180.                 ((retries = retries + 1))
  181.  
  182.                 # But ... if you re-tried 10 times already, then give up on this one
  183.                 if [[ "$retries" -gt 10 ]]; then
  184.                     skipit=true
  185.                     break
  186.                 fi
  187.  
  188.                 continue # Retry (this will re-download the file)
  189.  
  190.             fi
  191.         done
  192.  
  193.         # If you bailed out because the zip/7z compressed archive is invalid or failed to download, then proceed to the next GAMELINK
  194.         if [ "$skipit" = true ]; then
  195.             echo 'Giving up on this one...'
  196.             echo "Deleting filename: $filename"
  197.             rm "$filename" 2>/dev/null
  198.             echo "Writing logs at $console.err"
  199.             echo "$id@$console@$gamepage@$URL@$filename" >>"$console.err"
  200.             echo "Proceeding to the next game ..."
  201.             echo
  202.             echo
  203.             echo "================================================="
  204.             continue # Proceed to the next Game from the outer loop
  205.         fi
  206.  
  207.         # ------------ #
  208.  
  209.         # Extract the rom
  210.         skipit=false
  211.         retries=1
  212.         while true; do
  213.             folder=$(date)
  214.             echo "Trying to extract..."
  215.             echo "Creating a ./tmp directory and moving $filename into it"
  216.             mkdir ./tmp
  217.             mv "$filename" ./tmp
  218.         echo "tmp created and file moved into it"
  219.  
  220.             if [ "$SEVEN" = true ]; then
  221.                 if 7z x "./tmp/$filename" -o./tmp; then
  222.                     echo "Extracted!"
  223.                     folder=$(echo "$filename" | awk -F '.7z' '{print $1}')
  224.                     echo "Deleting the compressed 7z archive: $filename"
  225.                     rm "./tmp/$filename"
  226.                     echo "Deleted!"
  227.                     echo "Renaming the extracted folder ./tmp to $folder"
  228.                     mv ./tmp "$folder"
  229.                     echo "Renamed!"
  230.                 else
  231.                     echo "FAILED! Cannot un7zip the $filename"
  232.                     echo "Deleting the ./tmp directory"
  233.                     rm -rf tmp
  234.                     echo "Deleted!"
  235.                     echo "Retrying ... $retries"
  236.                     ((retries = retries + 1))
  237.                     # But ... if you re-tried 10 times already, then give up on this one
  238.                     if [[ "$retries" -gt 10 ]]; then
  239.                         skipit=true
  240.                         break
  241.                     else
  242.                         continue # retry (this will re-extract the file)
  243.                     fi
  244.                 fi
  245.             fi
  246.  
  247.             if [ "$ZIP" = true ]; then
  248.                 if unzip -o "tmp/$filename" -d ./tmp; then
  249.                     echo "Extracted!"
  250.                     folder=$(echo "$filename" | awk -F '.zip' '{print $1}')
  251.                     echo "Deleting the compressed zip archive: $filename"
  252.                     rm "tmp/$filename"
  253.                     echo "Deleted!"
  254.                     echo "Renaming the extracted folder ./tmp to $folder"
  255.                     mv ./tmp "$folder"
  256.                     echo "Renamed!"
  257.                 else
  258.                     echo "FAILED! Cannot unzip the $filename"
  259.                     echo "Deleting the ./tmp directory"
  260.                     rm -rf tmp
  261.                     echo "Deleted!"
  262.                     echo "Retrying ... $retries"
  263.                     ((retries = retries + 1))
  264.                     # But ... if you re-tried 10 times already, then give up on this one
  265.                     if [[ "$retries" -gt 10 ]]; then
  266.                         skipit=true
  267.                         break
  268.                     else
  269.                         continue # retry (this will re-extract the file)
  270.                     fi
  271.                 fi
  272.             fi
  273.  
  274.             # All fine, break the loop to proceed to the next game
  275.             break
  276.         done
  277.  
  278.         # If you bailed out because the zip/7z compressed archive is invalid or failed to download, then proceed to the next GAMELINK
  279.         if [ "$skipit" = true ]; then
  280.             echo 'Giving up on this one...'
  281.             echo "Deleting filename: $filename"
  282.             rm "$filename" 2>/dev/null
  283.             echo "Writing logs at $console.err"
  284.             echo "$id@$console@$gamepage@$URL@$filename" >>"$console.err"
  285.             echo "Proceeding to the next game ..."
  286.             echo
  287.             echo
  288.             echo "================================================="
  289.             continue # Proceed to the next Game from the outer loop
  290.         fi
  291.  
  292.         # ------------ #
  293.         echo "DONE! Game $filename with $id has been downloaded and extracted into $folder!"
  294.         echo
  295.         echo
  296.     done
  297.     echo "---"
  298. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement