Linux: pkg-downloader.sh bash script

Discussion in 'Misc. PS3 Homebrew' started by ps3george, Feb 26, 2019.

  1. 14
    8
    7
    ps3george

    ps3george Forum Noob

    Joined:
    Feb 21, 2019
    Messages:
    14
    Likes Received:
    8
    Trophy Points:
    7
    Hi,

    may someone can need this, a pkg dwnloader bash shell script for Linux,
    written by my self.

    This script will download the TITLE-ver.xml from the Sony Update Server
    and if avaible displays the update/s as a list where the user can choose
    download of a single update file or all update packages at once include
    automaticly sha1sum check after downloading of a package file, if the
    sha1sum mismatch an blinking error indicator will shown otherwise if
    the sha1sum is correct no notice will give out.

    [​IMG]

    Code:
    #!/bin/bash
    #
    # ****************************************************************************
    # *                   * * *  pkg-downloader.sh  * * *                        *
    # *                   * * *  *****************  * * *                        *
    # ****************************************************************************
    # * This script will download the TITLE-ver.xml from the Sony Update Server  *
    # * and if avaible displays the update/s as a list where the user can choose *
    # * download of a single update file or all update packages at once include  *
    # * automaticly sha1sum check after downloading of a package file, if the    *
    # * sha1sum mismatch an blinking error indicator will shown otherwise if     *
    # * the sha1sum is correct no notice will give out.                          *
    # *                                                                          *
    # * Version 0.0.1 15.02.2019                                                 *
    # * Version 0.0.2 26.02.2019                                                 *
    # * requires: bash4.2, wget, sha1sum                                         *
    # *                                                                          *
    # * usage: ./pkg-downloader.sh TITLEID                                       *
    # * example: ./pkg-downloader.sh BCES01893                                   *
    # ****************************************************************************
    #
    # !!!NO WARRANTY OF USAGE/DAMAGE etc., USE THIS CODE AT YOUR OWN RISK!!!
    #
    # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
    # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    # SUCH DAMAGE.
    
    
    # input check
    if [ -z $1 ]; then
        echo "usage ./psn-pkg-finder.sh TITLEID"
        exit 0
    fi
    
    # function for downloading the xml file
    f_get_xml(){
        # set TITLEID
        TITLEID="$1"
    
        # create TEMPFILE
        TEMPFILE=$(mktemp /tmp/output.XXXXXXXXXX) || { echo "Failed to create temp file"; exit 1; }
    
        # Retail Updates
        MIRROR="https://a0.ww.np.dl.playstation.net/tpl/np/$TITLEID/$TITLEID-ver.xml" || (echo "Error while getting updatelist. Aborting!" && exit 2) #\
    
        # get xml-file
        echo "Getting xml update file..."
        for i in $MIRROR; do
            wget --quiet --no-check-certificate "$i" -O ->> $TEMPFILE
        done
    }
    # function for show the menu
    f_parse_xml(){
        # parse TEMPFILE put into variables
        titleid=$(cat $TEMPFILE | grep -Po 'titleid="\K[^>]*(?=")')
        tagname=$(cat $TEMPFILE | grep -Po 'tag name="\K[^"]*()')
        title=$(cat $TEMPFILE | grep -Po '<TITLE>\K[^<]*()')
        # put into array
        declare -ag pkg_list
        while read -r; do
            pkg_list+=($(cat $TEMPFILE | grep -Po 'package version="\K[^"]*()'))
            pkg_list+=($(cat $TEMPFILE | grep -Po 'size="\K[^"]*()'))
            pkg_list+=($(cat $TEMPFILE | grep -Po 'sha1sum="\K[^"]*()'))
            pkg_list+=($(cat $TEMPFILE | grep -Po 'url="\K[^"]*()'))
            pkg_list+=($(cat $TEMPFILE | grep -Po 'ps3_system_ver="\K[^"]*()'))
        done < $TEMPFILE
        update_amount=$((${#pkg_list[*]} / 5 ))
    
        # Program Exit here if no Updates where found
        if [ "$update_amount" -eq "0" ]; then
            echo "Sorry, No Updates found."
            exit 0
        fi
    }
    # function for showing the menu
    f_show_menu(){
        COUNTER=0
        printf "\e[35m%s\e[0m\n" "---------------------------------------------------------------------------------------------------------------------------------------------"
        printf "\e[32m%s %s %30s %32s %23s %36s\e[0m\n" "[Nr]" "[Version]" "[Filename]" "[Size]" "[sha1sum]" "[PS3 System Version]"
        printf "\e[34m%s\e[0m\n" "---------------------------------------------------------------------------------------------------------------------------------------------"
    
        while [ $COUNTER -lt $update_amount ]; do
            printf "%3s %8s %57s %7s %s %9s\n" "$((COUNTER+1))" "${pkg_list[@]:$COUNTER:1}" "$(echo ${pkg_list[@]:((COUNTER+(($update_amount*3)))):1} | sed 's:.*/::')" "$(((${pkg_list[@]:((COUNTER+(($update_amount*1)))):1} / 1024 / 1024)))MB" "${pkg_list[@]:((COUNTER+(($update_amount*2)))):1}" "${pkg_list[@]:((COUNTER+(($update_amount*4)))):1}"
        COUNTER=$((COUNTER+1))
        done
        printf "\e[34m%s\e[0m\n" "---------------------------------------------------------------------------------------------------------------------------------------------"
    }
    # function for downloading the update/s
    f_download_pkg(){
        if [ "$input" == "a" ]; then
            COUNTER=0
            while [ $COUNTER -lt "$update_amount" ]; do
                echo "Downloading $(echo ${pkg_list[@]:(($COUNTER+(($update_amount*3)))):1} | sed 's:.*/::')"
                wget -q --show-progress --progress=bar:force 2>&1 "${pkg_list[@]:(($COUNTER+(($update_amount*3)))):1}" -O "$(echo ${pkg_list[@]:(($COUNTER+(($update_amount*3)))):1} | sed 's:.*/::')"
                # do sha1sum check of the downloaded update package
                if [ "${pkg_list[@]:(($COUNTER+(($update_amount*2)))):1}" != "$(cat $(echo ${pkg_list[@]:(($COUNTER+(($update_amount*3)))):1} | sed 's:.*/::') | head --bytes -32 | sha1sum | awk '{ print $1}')" ]; then
                    printf "\e[1;5;41m%s\e[0m\n" "sha1sum mismatch !!!"
                fi
                COUNTER=$((COUNTER +1 ))
            done
        else
            echo "Downloading $(echo ${pkg_list[@]:(($input+(($update_amount*3-1)))):1} | sed 's:.*/::')"
            wget -q --show-progress --progress=bar:force 2>&1 "${pkg_list[@]:(($input+(($update_amount*3-1)))):1}" -O "$(echo ${pkg_list[@]:(($input+(($update_amount*3-1)))):1} | sed 's:.*/::')"
            # do sha1sum check of the downloaded update package
            if [ "${pkg_list[@]:(($input+(($update_amount*2-1)))):1}" != "$(cat $(echo ${pkg_list[@]:(($input+(($update_amount*3-1)))):1} | sed 's:.*/::') | head --bytes -32 | sha1sum | awk '{ print $1}')" ]; then
                printf "\e[1;5;41m%s\e[0m\n" "sha1sum mismatch !!!"
            fi
        fi
    }
    
    # call f_get_xml
    f_get_xml "$1"
    # parse xml file
    f_parse_xml
    
    # main loop
    while : ;do
        # show menu
        f_show_menu
        printf "%s\n" "Which file you want to Download?  | Hint: a=all Updates | x=Program Exit"
    
        # read user input
        read -p 'Input: ' input
    
        # check for valid input
        if ! [[ "$input" =~ ^[0-9]{1,2}$ ]] && [[ "$input" != a ]] && [[ "$input" != "x" ]]; then
            echo "Please enter a valid input!"
            continue
        # check that the entered number is valid and inside the number range
        elif [[ "$input" =~ ^[0-9]{1,2}$ ]] && ! [[ "$input" -ge 1 ]] || ! [[ $input -le $update_amount ]]; then
            echo "Please enter a valid Number from (1-$update_amount) !"
            continue
        # Download a single Update Package
        elif [[ "$input" =~ ^[0-9]{1,2}$ ]] && [[ "$input" -ge 1 ]] && [[ $input -le $update_amount ]]; then
            f_download_pkg "$input"
            continue
        # download all Update Packages
        elif [[ "$input" == "a" ]]; then
            f_download_pkg "$input"
        # exit the program
        elif [[ "$input" == "x" ]]; then
            echo "GoodBye, Have a nice Day. :-)"
            # delete TEMPFILE on exit
            [ -f "$TEMPFILE" ] && rm -f "$TEMPFILE"
            exit 0
        fi
    done
    
     
  2. 1,871
    1,690
    272
    Berion

    Berion Developer

    Joined:
    Feb 3, 2015
    Messages:
    1,871
    Likes Received:
    1,690
    Trophy Points:
    272
    Gender:
    Male
    Location:
    rom0:/
    Awesome job. Thanks for Your time.

    Temp should be in $home as most of the users have tiny as possible system, and huge the rest. IMO.

    BTW: someone know server address for PS4 and PSV updates? They didn't changed method since PS3 didn't they?
     
    DeViL303 likes this.
  3. 154
    82
    57
    jacobsson

    jacobsson Member

    Joined:
    Aug 11, 2017
    Messages:
    154
    Likes Received:
    82
    Trophy Points:
    57
    Gender:
    Male
    Good job man!
     
  4. 1,683
    763
    172
    Cypher_CG89

    Cypher_CG89 Senior Member

    Joined:
    Jul 30, 2018
    Messages:
    1,683
    Likes Received:
    763
    Trophy Points:
    172
    Gender:
    Male
    Occupation:
    Lead Graphic Artist/Dev, VENOM ELITE GAMING
    Location:
    North East, England, UK
    Very handy script. Very good piece of work here.
     

Share This Page