历物语 – Shell

丢在这里了
所有文件保存在/tmp/koyomi
运行时需要密钥,可通过抓包客户端保存

https://lambda.ap-northeast-1.amazonaws.com/2015-03-31/functions/koyomimonogatari_app_auth_get/invocations

请求,保存json为/tmp/koyomi/key,并在脚本中指定HTTPS代理 $proxy ,即可正常运作
koyomi为批量下载一整集视频,自动选择最高清晰度
koyomi-raw为下载单个文件

#!/bin/sh

proxy="45.32.251.178:8888"

function downitem(){
	keydata=$(cat /tmp/koyomi/key)
	k="${keydata#*k\"\:\"}"; until [[ "$k" == "${k%\"*}" ]]; do k="${k%\"*}"; done
	p="${keydata#*p\"\:\"}"; until [[ "$p" == "${p%\"*}" ]]; do p="${p%\"*}"; done
	s="${keydata#*s\"\:\"}"; until [[ "$s" == "${s%\"*}" ]]; do s="${s%\"*}"; done
	cookie="Cookie: CloudFront-Key-Pair-Id=$k; CloudFront-Policy=$p; CloudFront-Signature=$s"
  s=; if [[ $2 == "s" ]]; then s="-s "; fi
  if [[ $2 == "c" ]]; then s="-C - "; fi
  mkdir -p /tmp/koyomi/${1%/*}
  done=no
  while [[ $done == no ]]; do
    curl --connect-timeout 30 -x "$proxy" "https://d3249smwmt8hpy.cloudfront.net/$1" -H "$cookie" -o /tmp/koyomi/$1 $s
    errno=$?
    if [[ $errno == 6 || $errno == 7 ]]; then echo "无网络连接"; exit; fi
    if [[ $errno == 28 || $errno == 56 ]]; then echo "连接超时,重试"; else if [[ $errno != 18 ]]; then done=done; fi; fi
  done
  if [[ -e /tmp/koyomi/$1 ]]; then
  if [[ $(grep -F "AccessDenied" /tmp/koyomi/$1) != "" ]]; then
    echo "密钥已失效"
    rm -f /tmp/koyomi/key
    exit
  fi; fi
}


if [[ $1 == "" ]]; then
  echo "请输入集数"
  exit
fi

if [[ ! -e /tmp/koyomi/key ]]; then
  echo "密钥文件缺失"
  exit
fi



basepath=movie/koyomi_$1
#basepath=movie/$1
echo -n "获取信息..."
downitem "$basepath/index.m3u8" s
if [[ $(grep -F "NoSuchKey" /tmp/koyomi/$basepath/index.m3u8) != "" ]]; then
  echo "该集不存在"
  exit
fi
echo "完成"
info=$(cat /tmp/koyomi/$basepath/index.m3u8)
br=0k
bw=0
until [[ "$info" == "${info%.m3u8*}" ]];do
  info=${info%.m3u8*}
  curbw=$(echo -n "$info" | tail -n 2)
  curbr=$(echo -n "$curbw" | tail -n 1)
  curbw=${curbw#*BANDWIDTH=}; until [[ "$curbw" == "${curbw%,*}" ]];do curbw=${curbw%,*}; done
  curbr=${curbr%bps/*}
  echo "$br $bw $curbr $curbw"
  if (( $curbw > $bw )); then bw=$curbw; br=$curbr; fi
done
filepath=$basepath/${br}bps
echo -n "${br}bps 获取分段..."
downitem "$filepath/movie_.m3u8" s
seginfo=$(cat /tmp/koyomi/$filepath/movie_.m3u8)
segs=${seginfo%.ts*}
until [[ "$segs" == "${segs#*movie_}" ]];do segs=${segs#*movie_}; done
until [[ "$segs" == "${segs#0}" ]];do segs=${segs#0}; done
let segs++
echo "共$segs分段"

downitem $filepath/vdata
cur=0
while (( $cur < $segs )); do
  if (( $cur < 1000 )); then file="movie_00$cur.ts"; fi
  if (( $cur < 100 )); then file="movie_000$cur.ts"; fi
  if (( $cur < 10 )); then file="movie_0000$cur.ts"; fi
  echo -e "\r\n\r-----------------------\n$file"
  downitem $filepath/$file c
  if [[ ! -e /tmp/koyomi/$filepath/$file ]]; then let cur--; fi
  let cur++
done
#!/bin/sh

proxy="45.32.251.178:8888"

function downitem(){
	keydata=$(cat /tmp/koyomi/key)
	k="${keydata#*k\"\:\"}"; until [[ "$k" == "${k%\"*}" ]]; do k="${k%\"*}"; done
	p="${keydata#*p\"\:\"}"; until [[ "$p" == "${p%\"*}" ]]; do p="${p%\"*}"; done
	s="${keydata#*s\"\:\"}"; until [[ "$s" == "${s%\"*}" ]]; do s="${s%\"*}"; done
	cookie="Cookie: CloudFront-Key-Pair-Id=$k; CloudFront-Policy=$p; CloudFront-Signature=$s"
  s=; if [[ $2 == "s" ]]; then s="-s "; fi
  if [[ $2 == "c" ]]; then s="-C - "; fi
  mkdir -p /tmp/koyomi/${1%/*}
  done=no
  while [[ $done == no ]]; do
    curl -v -x "$proxy" "https://d3249smwmt8hpy.cloudfront.net/$1" -H "$cookie" -o /tmp/koyomi/$1 $s
    errno=$?
    if [[ $errno == 6 || $errno == 7 ]]; then echo "无网络连接"; exit; fi
    if [[ $errno == 28 || $errno == 56 ]]; then echo "连接超时,重试"; else done=done; fi
  done
  if [[ -e /tmp/koyomi/$1 ]]; then
  if [[ $(grep -F "AccessDenied" /tmp/koyomi/$1) != "" ]]; then
    echo "密钥已失效"
    rm -f /tmp/koyomi/key
    exit
  fi; fi
}


if [[ $1 == "" ]]; then
  echo "请输入集数"
  exit
fi

if [[ ! -e /tmp/koyomi/key ]]; then
  echo "密钥文件缺失"
  exit
fi

downitem $1

if [[ $(grep -F "NoSuchKey" /tmp/koyomi/$1) != "" ]]; then
  echo "文件不存在"
  rm /tmp/koyomi/$1
  exit
fi

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax