写了一个脚本,用于自动从水木社区(Newsmth.net)的MyPhoto版自动下载图片到本地。
运行该脚本后,将自动从水木社区的MyPhoto版自动下载最近N(默认为3,通过参数1指定)天内的所有图片到本地的photo目录下。
用法:
1、把以下代码保存为autoPicSmth.sh
2、为脚本增加可执行权限,并运行脚本。
CHEYO:~/auto # chmod +x autoPicSmth.sh
CHEYO:~/auto # ./autoPicSmth.sh
脚本写得比较粗糙,欢迎优化改进。
源码:
#!/bin/bash

#####################################################################
#       Script:  autoPicSmth.sh
#       Author:  cheyo
#        Email:  icheyo at Gmail dot com
#         From:  www.icheyo.net
#         Date:  2008-02-22
#
#  Description:
#      This script is used for downloading pictures from the MyPhoto 
#      board in the newsmth.net automatically.
#
#####################################################################

# Usage: autoPicSmth.sh [days] 
# days:  download all pictures of recent /days/ days
# For Example:   ./autoPicSmth.sh 3

WORKING_DIR=working
PIC_OUT_DIR=photo
DAYS_TO_DOWN=3
QUERY_FILE=QueryResult.tmp
THREAD_FILE=ThreadUrl.tmp
FORMAT_FILE=ThreadInfo.tmp
CURR_THREAD_FILE=CurrThread.tmp 
PIC_URL_FILE=PicUrl.tmp
PIC_DOWN_LOG=PicDown.log
PIC_INFO_FILE1=PicInfo1.tmp
PIC_INFO_FILE2=PicInfo2.tmp
PIC_INFO_FILE3=PicInfoFinal.tmp

# ------------------------------------------------------------------ #
# ShowUsage()
# Show the usage of this script
# ------------------------------------------------------------------ #
ShowUsage()
{
    echo "This script is used for automatic downloading pictures from MyPhoto board in the newsmth.net"
    echo "  Usage: autoPicSmth.sh [days]"
    echo "  days:  download all pictures of recent /days/ days. 3 for default."
    echo "  Example:   ./autoPicSmth.sh 3"
}

# check arguments
if [ $# -gt 1 ]; then
    ShowUsage
    exit 1;
elif [ $# -eq 1 ]; then
    DAYS_TO_DOWN=$1
fi

mkdir -p $WORKING_DIR 
cd $WORKING_DIR

# Get the thread search result HTML page to local
SearchUrl="http://bbs4.newsmth.net/bbsbfind.php?q=1&board=MyPhoto&dt=${DAYS_TO_DOWN}&ag=1"
curl "${SearchUrl}" -o ${QUERY_FILE}

# Create a file to store all Thread URLs
egrep "<a href="bbscon.php?bid=" $QUERY_FILE | awk -F[<>"] '{print "http://bbs4.newsmth.net/"$9}' > $THREAD_FILE
ThreadCount=`cat $THREAD_FILE | wc -l`
echo 
"Total ${ThreadCount} threads are found."

# Create a file to store all BoardId and ThreadId
awk -F[=&] '{print $2,$4}' $THREAD_FILE > $FORMAT_FILE 

# Create a file to sotre all pictures infomation
# Format: BoardId ArticleId FileName FileSize FileId
echo 
"# BoardId ArticleId FileName FileSize FileId" > $PIC_INFO_FILE1

cat $FORMAT_FILE | while read BoardId ArticleId
do
    ThreadUrl=`echo "http://bbs4.newsmth.net/bbscon.php?bid=$BoardId&id=$ArticleId"`
    curl "$ThreadUrl" -o $CURR_THREAD_FILE
    grep "attach" $CURR_THREAD_FILE | tr ");" "" | grep "attach" | awk -F[' ,)] -v BoardId=$BoardId -v ArticleId=$ArticleId '{print BoardId,ArticleId,$2,$5,$7}' >> $PIC_INFO_FILE1
done

# Create a file to store all pictures info with file extention name 
# but not full file name.
# Format:  BoardId ArticleId FileExt FileSize FileId
# echo "# BoardId ArticleId FileExt FileSize FileId" > $PIC_INFO_FILE2
awk -F[. ]  
'$0~/^[^#]/ {print $1,$2,$4,$5,$6}' $PIC_INFO_FILE1 >> $PIC_INFO_FILE2
# Remove the records which don't contain enough info.
# in normal case, it should be 5 columns in the file.
awk '$5~/^[^$]/ {print $0}' $PIC_INFO_FILE2 > $PIC_INFO_FILE3 

# Create a file to store all picture url
grep ^[^#] $PIC_INFO_FILE3 | while read BoardId ArticleId FileExt FileSize FileId
do
    if [ $FileSize -gt 51200 ]; then
        FileType="p"
    else
        FileType="s"
    fi

    PicUrl=`echo "http://att.newsmth.net/att.php?$FileType.$BoardId.$ArticleId.$FileId.$FileExt"`
    echo "$PicUrl" >> $PIC_URL_FILE 
done

# Remove all duplicted URL from the file
mv ${PIC_URL_FILE} ${PIC_URL_FILE}.tmp
sort -dfu ${PIC_URL_FILE}.tmp > ${PIC_URL_FILE}
rm ${PIC_URL_FILE}.tmp

# Remove the URLs which have been downed before
if [ -f "../${PIC_OUT_DIR}/${PIC_DOWN_LOG}" ]; then
    cp ../$PIC_OUT_DIR/${PIC_DOWN_LOG} .
    awk '{print $3}' ${PIC_DOWN_LOG} > ${PIC_URL_FILE}.history
    sort -dfu ${PIC_URL_FILE}.history > ${PIC_URL_FILE}.tmp
    mv ${PIC_URL_FILE}.tmp ${PIC_URL_FILE}.history
    comm -1 -3 ${PIC_URL_FILE}.history ${PIC_URL_FILE} > ${PIC_URL_FILE}.tmp
    mv ${PIC_URL_FILE}.tmp ${PIC_URL_FILE}
    rm ${PIC_URL_FILE}.history
fi

# Download all pictures from server to local 
PicCount=`wc -l $PIC_URL_FILE | awk '{print $1}'`
PicIndex=1
mkdir -p ../$PIC_OUT_DIR 
echo "Total number of pictures to be downloaded: $PicCount"

cat $PIC_URL_FILE | while read CurrUrl
do
    FileName=`echo "$CurrUrl" | awk -F[?] '{print $2}'`
    echo "[$PicIndex/$PicCount] Start to download $CurrUrl"
    curl "$CurrUrl" -o ../$PIC_OUT_DIR/$FileName
    # Write download log to log file
    CurrTime=`date +"%Y-%m-%d %H:%M:%S"`
    echo "$CurrTime $CurrUrl" >> "../$PIC_OUT_DIR/$PIC_DOWN_LOG"
    echo "[$PicIndex/$PicCount] Download finished."
    echo ""
    PicIndex=`expr $PicIndex + 1`
done

#mv $PIC_URL_FILE ../$PIC_OUT_DIR/PicUrl.list
#mv $PIC_INFO_FILE3 ../$PIC_OUT_DIR/PicInfo.list
cd ..
rm -r $WORKING_DIR

echo "All Pictures Downloading finished."
http://write.blog.csdn.net/postedit

用Shell脚本实现自动从NewSmth.net的MyPhoto版下载照片相关推荐

  1. python生成shell脚本_Python设置在shell脚本中自动补全功能的方法

    本篇博客将会简短的介绍,如何在ubuntu中设置python自动补全功能. 需求:由于python中的内建函数较多,我们在百纳乘时,可能记不清函数的名字,同时自动补全功能,加快了我们开发的效率. 方法 ...

  2. Linux Shell - 脚本中自动确认需要输入确认的命令

    文章目录 Linux Shell - 脚本中自动确认需要输入确认的命令 1.yum 命令 2.其它命令 Linux Shell - 脚本中自动确认需要输入确认的命令 1.yum 命令 通过指定 -y ...

  3. linux脚本 程序输入,[转]Linux中shell脚本如何自动输入…

    shell脚本在处理自动循环或大的任务方面可节省大量的时间,通过创建一个处理任务的命令清单,使用变量.条件.算术和循环等方法快速创建脚本以完成相应工作,这比在命令行下一个个敲入命令要省时省力得多. 但 ...

  4. xml配置linux启动脚本,linux中利用Shell脚本实现自动安装部署weblogic服务

    身在物流行业,临近618和双十一,都需要进行系统压力测试,今年也不例外.这个时候需要随时切换自己开发和压测的身份,一面要完成新需求的开发任务,另一面要完成压测任务:虽然4月刚开始,但是压测任务已经排上 ...

  5. linux新建备份数据库的脚本文件,Linux下shell脚本:自动每日备份网站文件和数据库上传FTP空间...

    前言 服务器.vps,难免会遇到各种问题,丢失数据,则必然痛心疾首啊!!! 数据无价,so建议养成定期备份的习惯!而且,要多备份几份,本地.网盘.FTP空间等,都多保存几份! 这里,就为大家带来一个l ...

  6. linux 界面自动输入密码,Linux中shell脚本如何自动输入密码

    shell脚本在处理自动循环或大的任务方面可节省大量的时间,通过创建一个处理任务的命令清单,使用变量.条件.算术和循环等方法快速创建脚本以完成相应工作,这比在命令行下一个个敲入命令要省时省力得多. 但 ...

  7. shell脚本:自动检测网络掉线和自动重连。

    在ppp移植成功后,有时会出现ppp掉线等情况.这篇文章写了一个自动检测连接网络的解决方法. 创建一个shell脚本,在里面添加一下内容.(记得给操作权限) #!/bin/sh //根据你自己的she ...

  8. shell脚本telnet自动登录并执行命令

    通过shell脚本实现telnet自动登录,并发送命令给telnet实现telent登录之后在远端执行对应的命令. #!/bin/shuser="admin" pass=" ...

  9. linux互信封装脚本,使用shell脚本实现自动SSH互信功能

    说假设有一个1000台节点的Hadoop集群,要配置节点之间的SSH免密码登录,该如何用shell脚本实现? #!/bin/expect #循环1000台机器的IP地址,生成密钥文件authorize ...

最新文章

  1. UltraEdit不高亮解决办法
  2. 《Drupal实战》——2.5 使用Node clone快速添加测试数据
  3. 注意scrapy中SgmlLinkExtractor的默认deny_extensions
  4. IOS下将文字转成图片方法
  5. mysql 预留一个自定义字段_mysql-预留字段
  6. codeforces 385C Bear and Prime Numbers
  7. RestFul的初步理解
  8. Mac好用的文件对比工具Beyond Compare 4
  9. mciSendCommand对本地音乐的播放
  10. C语言学生成绩排名系统
  11. STM32F103如何使用串口下载程序
  12. maven集成tomcat7
  13. 识别PDF文字,教你两招
  14. verilog符号 与或非 异或_与非门、或非门、异或门、同或门的逻辑表达式和逻辑符号怎么写...
  15. java面试题——常见项目真实面试题(实际面试被问到)
  16. Protel DXP 使用教程 - 自定义集成库
  17. 海思HI3751HiDPTAndroidV200R001 UNF 接口版本差异说明
  18. 【接口篇 / Wan】(7.0) ❀ 05. 将 4G 作为备用宽带使用 ❀ FortiGate 防火墙
  19. Proverif分析handshake协议
  20. 求助:为什么用八爪鱼采集器抓取特定时间段的微博会出现漏抓情况?

热门文章

  1. 已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。...
  2. python中国大学慕课平台_计算机程序设计-Python,中国大学MOOC(慕课)答案公众号搜题...
  3. ffmpeg处理视频与声音
  4. KafkaConsumer源码翻译(中英对照)
  5. jmeter 如何开展性能测试
  6. 无心剑七绝《梅西封王》
  7. macOS安装Scrapy,不要踩坑了
  8. 课程预约小程序制作功能介绍
  9. plsql-数据查询(二、条件查询)
  10. 为iPhone 6设计自适应布局