[分享]我用到的更新小说的脚本(欢迎指教,并不定期修改)

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
linpinger
帖子: 54
注册时间: 2006-06-15 22:44

[分享]我用到的更新小说的脚本(欢迎指教,并不定期修改)

#1

帖子 linpinger » 2007-10-17 14:10

本人是个小说迷,一天不看小说就像一天没吃饭一样,呵呵
自己写了几个脚本,用来更新小说,很方便哦

以下的脚本是在windows下的Cygwin环境下运行的
是从Linux环境下移植过来的,主要是在文件编码方面有一点区别

在啃书论坛更新小说的脚本 gengxin.sh:

代码: 全选

#! /bin/bash
#	作者:小色狐狸
#	最后修改时间:2007年 11月 2日 星期5 7:13:06 CST
#	功能:下载啃书论坛更新页面,进行分析,然后下载图片文件,并自动命名
#	建立文件夹:foxgengxindir
#	下载的文件:view_table_*.html,viewthread*,*gif
#	建立的文件:entrys.tmp*;entry.now;pasted_entrys;page.lst;gif.lst

#####################################函数
function foxsname
{
# 功能:生成小说章节所包含的文件列表
# 使用的文件:pasted_entrys xiaoshuo viewthread.php*
# 建立的文件:name.sh;aaaaa.tmp;bbbbb.tmp
echo "#! /bin/bash" >> name.sh
for ffatmpa in $(tac foxgengxindir/pasted_entrys | sed -n 's/.*>//gp')
do
	echo "# $(sed -n "/$ffatmpa/p" foxgengxindir/pasted_entrys)" >> name.sh
	ffatmpb=$(sed -n "/$ffatmpa/ s/^\([^ ]*\).*/\1/gp" foxgengxindir/pasted_entrys)
	ffbtmpa=$(sed -n "/$ffatmpb/ s/^\([^:]*\).*/\1/gp" /home/etc/xiaoshuo)
	gifpath="/home/novel/gifnovel/$ffbtmpa-$ffatmpb"
	echo "[ -d $gifpath ] || mkdir $gifpath " >> name.sh
	sed -n 's#^[^"]*"attachments[^"]*/\([^"]*\)".*$#mv \1#gp' "foxgengxindir/viewthread.php@tid=$ffatmpa" > aaaaa.tmp
	for ffatmpd in $(seq $(cat aaaaa.tmp | wc -l))
	do
		echo "$gifpath/$(date +%Y%m%d_%H%M%S_%N.gif)" >> bbbbb.tmp
	done
	paste -d\  aaaaa.tmp bbbbb.tmp >> name.sh
	[ -f bbbbb.tmp ] && rm bbbbb.tmp ;
done
[ -f aaaaa.tmp ] && rm aaaaa.tmp ;

vim  +"1" name.sh
}

###################################附    加################################
# 帮助
if [ $# -eq 1 -a "$1" = "h" -o "$1" = "-h" ]
then
cat <<- FOXEND
gengxin.sh , 下载啃书论坛的小说
用法:  gengxin.sh [选项] [N]

  -c       测试cookie是否有效
  -t [N]   下载网页列表 view_table_N.html (其中N范围[1-3])
  -p       下载 page.lst 中的页面
  -g       生成图片下载列表 gif.lst
  -n       生成命名列表 name.sh
  -q       清空历史记录:genxin.sh q
FOXEND
exit 0
fi

# 测试cookie是否有效:gengxin.sh c
if [ $# -eq 1 -a "$1" = "c" -o "$1" = "-c" ]
then
	cp ~/.lynx_cookies kshuw.cookie
	wget --load-cookies=kshuw.cookie -O kenshuaa.html "http://www.kshuw.net"
[ $(sed -n '/linpinger/p' kenshuaa.html | wc -l) -eq 0 ] && {
echo -e '\E[00;34m'"\033[1m##### Cookie过期了,快使用Lynx, ^_^ #####\033[0m" 
} || echo -e '\E[00;34m'"\033[1m##### Cookie有效,恭喜 ^_^ #####\033[0m" 
	rm kenshuaa.html
	rm kshuw.cookie
	exit 0
fi
# 清空历史记录:gengxin.sh q
if [ $# -eq 1 -a "$1" = "q" -o "$1" = "-q" ]
then
	: > /home/etc/kshuw.jilu
	exit 0
fi

# 下载网页列表:gengxin.sh t [1-3]
if [ $# -eq 2 -a "$1" = "t" -o "$1" = "-t" ]
then
	[ -f foxgengxindir/view_table_$2.html ] && rm foxgengxindir/view_table_$2.html
	wget -O foxgengxindir/view_table_$2.html "http://www.kshuw.net/archiver/?fid-2-page-$2.html"
	exit 0
fi

# 下载网页页面:gengxin.sh p
if [ $# -eq 1 -a "$1" = "p" -o "$1" = "-p" ]
then
	for ppppp in $(ls -lh viewthread.php* | grep -v K | cut -d\= -f2)
	do
		wget -N --load-cookies=kshuw.cookie "http://www.kshuw.net/viewthread.php?tid=$ppppp"
	done
	exit 0
fi

# 生成图片下载列表:gengxin.sh g
if [ $# -eq 1 -a "$1" = "g" -o "$1" = "-g" ]
then
	sed -n 's#^.*src="\(attachments[^"]*\).*$#http://www.kshuw.net/\1#gp' viewthread.php* > gif.lst
	exit 0
fi

# 生成命名列表:gengxin.sh n
if [ $# -eq 1 -a "$1" = "n" -o "$1" = "-n" ]
then
	foxsname
       	exit 0
fi

###################################程序开始################################

# 1.创建临时文件夹
[ -d foxgengxindir ] || mkdir foxgengxindir 
cd foxgengxindir 

# 2.下载条目列表
for table_seq in $(seq 3)
do
	[ -f view_table_$table_seq.html ] && echo "文件 view_table_$table_seq.html 存在,不做处理" || {
	wget -c -O view_table_$table_seq.html "http://www.kshuw.net/archiver/?fid-2-page-$table_seq.html"
}
done

# 3.[将下载条目列表转换为utf8编码格式(对Linux)]
#   3.1.处理并生成'条目>地址'格式的文件,并按发帖时间降序排列(即前新后旧)
sed -n '/.*tid-\([0-9]*\).html">\(.*\)<\/a>.*$/ s//\2>\1/gp' *.html | sort -t\> +1 -r | uniq > entrys.tmpa

#   3.2.删除记录中存在的条目
cat /home/etc/kshuw.jilu entrys.tmpa | sort -t\> +1 -r /home/etc/kshuw.jilu - | uniq -u > entrys.tmp

#   3.3.将'条目>地址'格式的文件与小说列表比较,生成比较文件
for foxnovelname1 in $(cut -d\: -f3 /home/etc/xiaoshuo)
do
	sed -n "/$foxnovelname1/p" entrys.tmp >> pasted_entrys
	sed -n "/$foxnovelname1/p" /home/etc/xiaoshuo >> pasted_entrys
done

# 4.记录条目,若文件pasted_entrys另一部分为空,则退出
sed -n "/^[^A-Z]/p" pasted_entrys > entry.now
[ $(cat entry.now | wc -l) -eq 0 ] && {
echo -e '\E[00;34m'"\033[1m##### 没有更新 , ^_^ #####\033[0m" 
exit 0;
}

# 5.1.人工处理比较文件,你需要做:将列表更新,删除其中不符的条目(旧的或者不是的)
vim -c "/^[^A-Z]" +"1" pasted_entrys

# 5.2.更新条目
cat entry.now >> /home/etc/kshuw.jilu
echo "#####  $(date +%Y%m%d_%H%M%S.gif)  ######" >> /home/etc/kshuw.jilu

# 5.3.若文件pasted_entrys另一部分为空,则退出
[ $(sed -n '/^[^A-Z]/p' pasted_entrys | wc -l) -eq 0 ] && {
echo -e '\E[00;34m'"\033[1m##### 没有更新 , ^_^ #####\033[0m" 
exit 0;
}

# 5.4.小说记录备份
cp /home/etc/xiaoshuo /home/etc/xiaoshuo.bak
echo -e '\E[00;34m'"\033[1m小说记录备份完成\033[0m"

# 6.1.将文件pasted_entrys一部分更新为小说列表
sed '/^[^A-Z]/d' pasted_entrys > /home/etc/xiaoshuo
echo -e '\E[00;34m'"\033[1m#####小说列表共 $(sed -n "$=" /home/etc/xiaoshuo) 行#####\033[0m"

# 6.2.否则将文件另一部分更新为条目网页下载列表,并下载
sed -n 's#.*>#http://www.kshuw.net/viewthread.php?tid=#gp' pasted_entrys > page.lst
#	下载
cp ~/.lynx_cookies kshuw.cookie
wget --load-cookies=kshuw.cookie -i page.lst

# 7.分析并处理这些网页,导出gif下载列表,并下载
#	使用电信时,由于grep将该网页识别为二进制文件,故在这一步使用sed
sed -n 's#^.*src="\(attachments[^"]*\).*$#http://www.kshuw.net/\1#gp' viewthread.php* > gif.lst
#	下载gif文件
cd ..
wget -c -i foxgengxindir/gif.lst

# 8.生成小说章节所包含的文件列表
foxsname

echo -e '\E[00;34m'"\033[1m####################################################\033[0m";
echo -e '\E[00;34m'"\033[1m######################程序结束######################\033[0m";
echo -e '\E[00;34m'"\033[1m####################################################\033[0m";

###################################程序结束################################


在起点中文下载文本小说的脚本 cmfu.sh:

代码: 全选

#! /bin/bash



function foxhelp
{
cat <<- FOXEND
cmfu.sh , 处理起点中文的文本或列表
用法:  cmfu.sh [选项] [N]

  -d [N]   下载list_top[N].js(其中N范围[1-14],默认值3)
  -n [N]   下载list_yqtop[N].js(其中N范围[1-14],默认值3)
  -t [N]   制作top400.lst(从文件list_top[N].js(其中N范围[1-14],默认值3)),格式:书号 书名 类型
  -r       处理top400.lst , 删除其中已经存在的小说
  -l       制作down400.lst
  -m       重命名小说名:number.txt-->number中文名.txt
  -h       帮助

N值:
 	1.总点击榜	2.月点击榜	3.周点击榜	4.总推荐榜	5.月推荐榜
 	6.周推荐榜	7.总收藏榜	8.总字数榜	9.新人周榜	10.入站时间
 	11.新书周榜	12.未知榜	13.短信PK榜	14.全本回顾榜 
FOXEND
}

function foxdownload
{
	downtype=${2:-3}
	if [ "$1" = "d" -a $downtype -ge 1 -a $downtype -le 14 ]
	then
		wget http://www.cmfu.com/script/list_top$downtype.js
	elif [ "$1" = "n" -a $downtype -ge 1 -a $downtype -le 14 ]
	then
		wget -O list_top$downtype.js http://www.cmfu.com/script/list_yqtop$downtype.js
	else
		echo "输入错误!"
	fi
}

function create_top400
{
	ffilename="list_top${2:-3}.js"
	[ -f $ffilename ] && {
	awk 'BEGIN{FS=",";OFS="\t";RS=";"}{print $2,$3,$6}' $ffilename | sed -n "/^'[0-9]/ s/'//gp" > top400.lst 
} || echo "文件$ffilename不存在!"
}

function proc_top400
{
	for delname in $(cut -d\: -f3 /home/novel/xiaoshuo /home/novel/wenben)
	do
		sed "/$delname/d" top400.lst > tmpfile
		mv tmpfile top400.lst
	done
	sed -e "/游戏$/d" -e "/竞技$/d" top400.lst > tmpfile
	mv tmpfile top400.lst
}

function create_download
{
	for novellist in $(cut -f1 top400.lst)
	do
		echo "http://download.cmfu.com/pda/$novellist.txt" >>download.lst
	done
}

function move_file
{
	for txtname in $(ls *.txt)
	do
		ffnumber=${txtname%.txt}
		filename=$(grep $ffnumber top400.lst | cut -f2)
		if [ -d /media/usbdisk/novel ] 
		then
			sudo mv $txtname /media/usbdisk/novel/"$ffnumber$filename".txt
			sync;sync;
		else
			mv $txtname ./"$ffnumber$filename".txt
			echo "文件夹/media/usbdisk/novel不存在!"
		fi
	done
}

################################程序开始################################
if [ $# -eq 0 -o "$1" = "h" -o "$1" = "-h" -o "$1" = "--help" ] #帮助选项
then
	foxhelp
elif [ "$1" = "n" -o "$1" = "-n" ] #功能:下载list_top[1-14].js
then
	foxdownload $*
elif [ "$1" = "d" -o "$1" = "-d" ] #功能:下载list_top[1-14].js
then
	foxdownload $*
elif [ "$1" = "t" -o "$1" = "-t" ] #功能:生成top400.lst
then
	create_top400 $*
elif [ "$1" = "r" -o "$1" = "-r" ] #功能:处理top400.lst:删除xiaoshuo和wenben中包含的小说名
then
	proc_top400 
elif [ "$1" = "l" -o "$1" = "-l" ] #功能:制作下载列表download.lst
then
	create_download
elif [ "$1" = "m" -o "$1" = "-m" ] #功能:将文本移到MP3中
then
	bunzip2 *.bz2
	move_file
else
	echo "输入错误!"
fi
################################程序结束################################

我的小说记录的格式:

代码: 全选

AM:471:迷失在康熙末年:86057:凌啸:
格式字段意义:

代码: 全选

【小说等级】【小说首字母】:【更新到多少章】:【小说名】:【小说在起点的书号】:【小说主人公姓名】:
----------------------------------------------更新日志--------------------------------------------------
2007年11月2号由于论坛改版,浏览帖子内容需要登录,故在使用wget下载网页内容时,调用lynx浏览器的cookie文件,当cookie过期时,使用lynx登录,然后再执行脚本
2007年10月19号增加历史记录功能,并进一步优化步骤
2007年10月10号移植到ubuntu上时,发现使用sed处理GBK编码的文件时,须先使用iconv将编码转换为UTF-8
----------------------------------------------------------------------------------------------------------
欢迎 各位指教!!
上次由 linpinger 在 2007-11-02 7:49,总共编辑 6 次。
jieying
帖子: 371
注册时间: 2005-11-03 17:52

#2

帖子 jieying » 2007-10-17 19:50

不看小说,纯支持。
aBiNg
帖子: 1331
注册时间: 2006-07-09 12:22
来自: 南京

#3

帖子 aBiNg » 2007-10-18 14:12

嗯,不错。顶一下。:)
回复