您的位置:首页 > 运维架构 > Shell

用 shell 脚本批量下载畅想听吧(CXT8)和 有声下吧(YSX8)的有声小说

2013-10-22 15:08 507 查看
眼睛不好又迷上了小说怎么办? 那就只能用耳朵听啦。。。上下班3个小时在路上,玩手机么伤眼睛,只能听听有声小说了。

最近一直在找一些有声小说听,发现想要下载下来同步到 itunes 里面还是有点麻烦的,都是体力活啊。 于是就写了两个脚本来下载这些 mp3。 脚本仅供参考,请勿用于商业用途,并且最好还是支持正版吧,至少也要花点钱支持一下自己喜欢的播音员哦。

CXT8 的下载脚本:

会自动下载所有的 mp3,前提是这些 mp3 的下载是免费的(单体下载免费即可)
目前默认使用的是电信线路,需要联通的话就自己动手改一下吧。
传入参数的 book_id 为 CXT8 的书籍 ID, 比如 http://www.cxt8.com/static/chapter_2238/index_10.shtml, book_id = 2238, page_id = 10
start_page 和 end_page 为 CXT8 中需要下载的 page 起始和终止页面,比如 2 10 的话既是下载 2-10 页中的所有集数
下载下来的 mp3 没有 id3 信息,可以使用 mid3v2 工具来批量修改

#!/bin/bash

fail_file=`pwd`/fail.url

if [ $# != 3 ];then
echo "Usag: book_id start_page end_page"
fi

id=$1
sta=$2
end=$3

download(){
real=$1
fileName=$2
sleep 1
curl -L --connect-timeout 100 -m 600 "$real" -o $fileName
}

real_url(){
temp=$1
retry=$2
perfix=`echo $temp|sed 's/\.com.*/\.com/g'`
end=`curl "$temp" -v -c cookie 2>&1 |grep "Location" | sed 's/^.*\//\//g' |sed 's/.$//g'`
#end=`curl "$temp" -v -c cookie 2>&1 |grep "Location" | sed 's/^.*\//\//g' |sed 's/true.*$/true/g'`
full_url=$perfix$end
real=`curl "$full_url" -v -b cookie 2>&1 |grep "Location"| sed 's/^.* //g' |sed 's/.$//g'`
if [ '$real' == '' ];then
echo "$temp" >> "$fail_file"
else
echo $real
fileName=`echo $real|sed 's/^.*com\///g'|sed 's/\..*/\.mp3/g'`
# 是否需要下载
if [ `find . -name "$fileName" -size +6M |wc -l` == 0 ];then
> "$fileName"
else
return 0
fi
echo "try download $fileName $retry times"
download "$real" "$fileName"

# 是否已经下载完成
if [ `find . -name "$fileName" -size +5M |wc -l` == 1 ];then
echo "$fileName download success!"
return 0
else
echo "$temp" >> "$fail_file"
return 1
fi
fi

return 0
}

decode_url(){
echo "start download page $1"
res=''
for url in `curl -s $1 |grep 'class="col5"' |grep "http:"|sed 's/^.*http/http/g'| sed 's/shtml".*/shtml/g'`;do
res=''
for i in `seq 1 5`;do
temp=`curl -s $url |grep 'class="auto"' |grep "http:"|sed 's/^.*http/http/g'| sed 's/">.*//g'`
#temp=`curl -s $url |grep 'class="dianxin"' |grep "http:"|sed 's/^.*http/http/g'| sed 's/" .*//g'`
if [ '$temp' == '' ];then
res=$url
else
real_url "$temp" "$i"
s=$?
if [ "$s" == "0" ];then
res=''
break
else
res=$url
fi
fi
done
done
}

#decode_url 'http://www.cxt8.com/static/chapter_1553/'
for i in `seq $sta $end`;do
[ ! -d $i ] && mkdir $i
cd $i
decode_url 'http://www.cxt8.com/static/chapter_'$id'/index_'$i'.shtml'
cd ..
done


YSX8 的下载脚本:

只是显示下载地址,不会直接下载,可以拷贝下载地址到迅雷中下载。
输入参数为 ysx8 的打包下载页面,比如:http://www.ysx8.net/down/d_13270.html

#!/bin/bash

if [ -z $1 ];then	echo "Usag: $0 download_url"	exit -1fi
file=$1fail_file="$file".fail
if [ -f $fail_file ];then	 rm $fail_filefi
#for url in `cat $file |grep "http"|sed '1s/^.*http/http/g'|grep "xunlei"|sed 's/".*$//g'`;do//g'|grep "kuai.xunlei"|sed "s/.*\(http[^']*\).*/\1/g"`;do	sleep 4	temp=`curl -s $url |grep 'class="file_name"'|grep -E 'xsid="[0-9]+"'|sed  's/^.*href="//g'|sed 's/".*$//g'`	if [ '$temp' == '' ];then		echo $url >> "$fail_file"	else		for t in $temp;do			echo $t		done	fidone
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: