Linux: pkg-downloader.sh bash script

ps3george

Forum Noob
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.

xGyjHON.png


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
 
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?
 
Thanks. I guess it isn't the script's fault when it says "sha1sum mismatch !!!" for this file:
Code:
[Nr] [Version]                     [Filename]                           [Size]               [sha1sum]                 [PS3 System Version]
---------------------------------------------------------------------------------------------------------------------------------------------
  1    01.22   IP9100-NPIA00002_00-0000111122223333-A0122-V0100-PE.pkg    10MB 6c8b6fe053091b1a7104bb76b0adddba37caaa9a   04.2000
... and produces a 0 byte file?
 
Last edited:

Similar threads

Back
Top