#!/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配合使用的更多相关文章

  1. AWK增强的文本处理shell特征--AWK完全手册

    AWK这是一个很好的文字处理工具. 它不仅 Linux 中也是不论什么环境中现有的功能最强大的数据处理引擎之中的一个. 本文主要摘录池中龙写的Unixawk使用手冊(第二版),对当中内容略微修改.感谢 ...

  2. CU社区shell板块awk十三问整理

    CU社区shell板块awk十三问整理 一.RS="" 当 RS="" 时,会将\n强制加入到FS变量中,因为RS为空时,是将连续多空行作为分隔符,近似于\n\ ...

  3. Shell 学习—AWK介绍

    Shell 学习—AWK = = = 安装awk root@kiki-desktop:~/shell# apt-get install gawk gawk-doc = = = awk 是一种程序语言. ...

  4. Shell之awk

    Shell之awk 目录 Shell之awk 一.awk概述 1. awk的工作原理 2. 命令格式 3. awk常见的内建变量(可直接用) 二.操作实例 1. 按行输出文本 2. 按字段输出文本 3 ...

  5. shell中awk用法

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  6. shell 统计 awk

    time awk '{a[$1]++}END{for(i in a){printf("%d\t%s\n",a[i],i)}}' access.log | sort -nr | he ...

  7. shell 之awk 关联数组高级应用

    最近由于数据迁移过,有些用户信息需要再次确认下,也许数据量比较大,但是需要最终确认的比如说是用户ID和其对应的用户积分数,这样就会导致出现文本a(老的数据),文本b(新的数据).比如 这是文本a.tx ...

  8. shell脚本 awk工具

    awk工具概述awk编程语言/数据处理引擎基于模式匹配检查输入文本,逐行处理并输出通常在shell脚本中,或取指定的数据单独用时,可对文本数据做统计 命令格式格式一:awk [选项] '[条件]{编辑 ...

  9. 【shell】awk按域去除重复行

    首先解释一下什么叫“按域去除重复行”: 有的时候我们需要去除的重复行并不是整行都重复,两行的其中一列的元素相同我们有的时候就需要认定这两行重复,因此有了今天的内容. 去除重复行shell有一个原生命令 ...

随机推荐

  1. ZOJ 3235 Prototype

    Prototype Time Limit: 1 Second      Memory Limit: 32768 KB Prototype is a 3D game which allow you to ...

  2. Jmeter实现webservice的接口测试

    前提条件 测试的URL:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx 测试接口:getSupportCityString 获取城市的编码:htt ...

  3. js异步任务处理方式

    一.es6(es2015)之前:使用原始的callback函数,会陷入回掉地域 this.$http.jsonp('/login', (res) => { this.$http.jsonp('/ ...

  4. 使用LibZ合并.Net程序集,支持WPF

    最近写了一个小的WPF程序,发布的时候发现依赖着两三个20~30k的小dll的,感觉有点不爽,就想把它合并一下.以前在WinForm下用过微软的ILMerge合并程序集,不过记得它对WPF程序支持不大 ...

  5. 改变 Windows 用户文件夹默认位置

    https://social.microsoft.com/Forums/zh-CN/b6602d50-1a75-42d2-90bd-3dc6c2c82147/programdata?forum=win ...

  6. 腾讯旗下网站的很多URL都包含“cgi-bin”,是什么意思?他们后台用什么语言?

    cgi-bin 这很有可能说明后台是C/C++写的. 动态Web技术刚出来的时候, 服务器调用本地应用程序处理http请求的技术. 通常是C/C++程序. 后来有了新的web开发技术后这类用的就比较少 ...

  7. PostgreSQL SystemTap on Linux 转

    PostgreSQL 支持动态跟踪, 可以通过dtrace或者systemtap工具统计相关探针的信息. 安装systemtap yum install systemtap kernel-debugi ...

  8. Apache Mina入门

    Mina第一次听到这个名称的时候,我以为是个MM的名字米娜,后来才知道… Apache MINA(Multipurpose Infrastructure for Network Application ...

  9. MVC把表格导出到Excel

    有关Model: namespace MvcApplication1.Models { public class Coach { public int Id { get; set; } public ...

  10. 【BZOJ】【1089】【SCOI2003】严格n元树

    高精度/递推 Orz Hzwer…… 然而我想多了…… 理解以后感觉黄学长的递推好精妙啊 顺便学到了一份高精度的板子= =233 引用下题解: f[i]=f[i-1]^n+1 ans=f[d]-f[d ...