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

本文介绍了一个用于自动从水木社区MyPhoto版块下载图片的Shell脚本。此脚本可以根据指定天数抓取并下载相应时间范围内的所有图片,同时避免重复下载已存在的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写了一个脚本,用于自动从水木社区(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 -$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" -$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 [ -"../${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 -$PIC_URL_FILE | awk '{print $1}'`
PicIndex
=1
mkdir -../$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" -../$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 
-$WORKING_DIR

echo 
"All Pictures Downloading finished."
http://write.blog.youkuaiyun.com/postedit

一个可自定义的扩展名,用于保存当前标签页中的选定图像 下载所有图像这是一个高度可定制的图像保护程序扩展程序,使您可以浏览和下载任何网页上显示的所有图像。 功能:1.可以检测当前页面中加载的所有图像,而不管它们是否嵌套在iframe中。2.许多过滤规则仅查找和下载所需的扩展名。3.可以捕获链接,后台脚本和CSS文件中的图像4。下载图像之前,先在图库视图中显示它们。5.支持两级深度图像搜索过滤器:1.按文件大小过滤2.按图像尺寸过滤3.按图像类型过滤4.按图像URL(正则表达式匹配)过滤5.按相同的来源策略过滤有关常见问题解答,请访问:http://add0n.com/save-images.html有关错误报告,请使用:https://github.com/belaviyo/save-images/issues注意:当您按时单击“保存”按钮,所有选择的图像均保存到浏览器的默认下载目录,或者如果指定子文件夹名称,则保存到浏览器中的目录。 更改日志:本0.1.1现在,该扩展名将图像存储在子文件夹中(默认的子文件夹名称为域名)。修复了一个安全漏洞。 本0.2.0图像现在保存在zip存档中,以防止多次下载,并且文件命名更好。改进了图像检测算法。 0.2.1现在可以保留用户首选项。 仍然可以从UI重置它们。现在可以从UI要求“保存对话框”。有一个进度指示要处理多少个图像,当下载所有图像时,UI会自行关闭。 本0.2.2通过所有链接进行深度图像搜索(最多两个级别)更新JSZip库本0.3.4可以在新的浏览器选项卡中打开图库视图可以在图库视图中单击单个图像可以从以下位置下载图像本0.3.7同时具有多个选项卡。两个新过滤器:删除相同的图像并使用列入黑名单的关键字删除图像现在,可以在不关闭UI的情况下停止UI的图像蜘蛛本0.5.6 Deeps搜索级别3,该级别搜索两个链接和目标HTML页面中的图像自定义RegExp过滤器可提高搜索速度。 支持语言:English
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

福海鑫森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值