shell和awk配合使用

#!/bin/sh
#$1 video id
#$2 save result file
#####################################################################

#function decrypt_signature() {
decrypt_signature() {
    encrypted_sig="$1"
    video_id="$2"
    player_url="$3"
    age_gate="$4"

    url_start=`awk -v url=$player_url 'BEGIN{print index(url,"//");}'`
    echo url_start=$url_start
    if [ "$url_start" = "1" ];then
        player_url="https:""$player_url"
    fi
    echo player_url=$player_url
    wget $player_url -O "$fpipe_new0"
    echo fpipe_new0=$fpipe_new0
}

 

version="youtube_get_play_url:v2.0\n"
helpContent="Parameter1:video_id\nParameter2:result save file\nexample:\nyoutube_get_play_url ACFADSFxf file\n"
if [ "$1" = "--version" ];then
    printf "$version"
    exit 0
fi
if [ "$1" = "--help" ];then
    printf "$helpContent"
    exit 0
fi
if [ -z "$1" ];then printf "$helpContent"; exit 0; fi
if [ -z "$2" ];then printf "$helpContent"; exit 0; fi

vurl="$1"
fresult="$2"
fpipe="$2""_tmp"
fpipe2="$2""_tmp2"

fpipe_new0="$2""_tmp3"
fpipe_new1="$2""_tmp4"
echo vurl=$vurl
echo fresult=$fresult

rm "$fresult" -rf
rm "$fpipe" -rf
rm "$fpipe2" -rf

url="http://www.youtube.com/watch?v=""$vurl""&gl=US&hl=en&has_verified=1"
detail_url="https://www.youtube.com/get_video_info?&video_id=""$vurl""&el=detailpage&ps=default&eurl=&gl=US&hl=en"

wget $url -O "$fpipe"
cp "$fpipe" "$fpipe_new0"
age_gate=`grep "player-age-gate-content" < "$fpipe_new0"`

 

 

tr -d "\0" < "$fpipe" > "$fpipe2"
sed -i 's/, "/\n/g' "$fpipe2"
grep "url_encoded_fmt_stream_map" < "$fpipe2" > "$fpipe"
sed -i 's/,/\n/g' "$fpipe"
grep -m 1 "x-flv" < "$fpipe" > "$fpipe2"
sed -i 's/\\u0026/\n/g' "$fpipe2"
grep -E "url=|sig=|s=" < "$fpipe2" > "$fpipe"

str_grep=`grep "s=" < "$fpipe"`
if [ -n "$str_grep" ];then
    echo "$str_grep"
    echo "$str_grep" > "$fpipe2"
    sed -i "s/^s=//" "$fpipe2"
    if [ -n "$age_gate" ];then
        echo age_gate=$age_gate
    else
        grep -E "\"assets\":*" <"$fpipe_new0" >"$fpipe_new1"
        sed -i 's/;/\n/g' "$fpipe_new1"
        sed -i 's/,/\n/g' "$fpipe_new1"
        sed -i 's/}/\n/g' "$fpipe_new1"
        sed -i 's/:/\n/g' "$fpipe_new1"
        sed -i 's/ /\n/g' "$fpipe_new1"
        sed -i 's/"//g' "$fpipe_new1"
        sed -i 's/\\//g' "$fpipe_new1"
        player_url=`grep "html5player" < "$fpipe_new1"`
        echo player_url=$player_url
        decrypt_signature $str_grep $vurl $player_url $age_gate
    fi
    str_sig=`youtube_rev "$fpipe2"`
    echo "sig=""$str_sig"
    sed -i "s/^s=.*$/signature=""$str_sig""/" "$fpipe"
fi

 

sed -i 's/%26/\&/g' "$fpipe"
sed -i 's/%3D/=/g' "$fpipe"
sed -i 's/%25/%/g' "$fpipe"
sed -i 's/%2F/\//g' "$fpipe"
sed -i 's/%3A/:/g' "$fpipe"
sed -i 's/%3F/?/g' "$fpipe"
sed -i 's/^sig=/signature=/' "$fpipe"
#sed -i 's/^s=/signature=/' "$fpipe"
ARG1=`cat "$fpipe" | grep "url=http" | sed -e 's/^.*url=//g'`
ARG2=`cat "$fpipe" | grep "signature="`
echo "$ARG1""&""$ARG2" > "$fpipe2"

echo "[Http] URL="$url
if [ -s "$fpipe2" ];then
    mv "$fpipe2" "$fresult"
    echo "got video url."
else
    echo "this video is removed."
    rm "$fpipe" -rf
    rm "$fpipe2" -rf
fi
exit 0

你可能感兴趣的:(shell和awk配合使用)