Skip to content

Instantly share code, notes, and snippets.

@gawaooooo
Created March 12, 2014 04:47
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gawaooooo/9501048 to your computer and use it in GitHub Desktop.
指定URLから連番のファイルを取得するスクリプト作ってみた
#!/bin/bash
#
# @(#) download.sh ver.1.0.1 2014.03.12
#
# Usage:
# download.sh start end dir
# start - 取得するファイルの開始番号.
# end - 取得するファイルの終了番号.
# dir - 保存先ディレクトリ.
# isSleep - 1/スリープあり 0/スリープなし
#
# Description:
# 指定URLから連番のファイルを取得するスクリプトです.
#
###################################################################
echo "start.................."
# 取得したいファイルがあるURL
readonly URL="http://example.com/"
readonly PREFIX="image_"
readonly SUFFIX=".jpg"
start=$1
end=$2
# save指定がない場合はカレント
if [ -z "$3" ]; then
echo "保存先指定なし....."
dir=`pwd`
else
dir=$3
fi
# スリープ指定がない場合はなしで
if [ -z "$4" ]; then
echo "スリープ指定なし..."
isSleep=0
else
isSleep=$4
fi
# ディレクトリかどうかチェック。なかったら作る
if [ ! -d "${dir}" ]; then
echo "ディレクトリ作る: ${dir}"
mkdir "${dir}"
fi
# 保存先へ移動
cd "${dir}"
echo "==========================="
echo "${URL}"
echo "${PREFIX}${start}${SUFFIX}から${PREFIX}${end}${SUFFIX}までのファイルをゲットしにいくよ"
echo "保存先は:${dir}"
echo "==========================="
echo "[http_code] URL" >get.txt
# 1秒のスリープをいれない場合はこちらで。。
if [ ${isSleep} -eq 0 ]; then
# 取得ファイルの一覧を保存できないかな?
curl -O -s ${URL}${PREFIX}[${start}-${end}]${SUFFIX}
else
for i in `seq ${start} ${end}`
do
geturl=${URL}${PREFIX}${i}${SUFFIX}
echo ${geturl} >>get.txt
# 1つずつHTTP STATUS をチェック
#code=`curl -s ${geturl} -w "status:%{http_code}" | grep 'status:' | cut -d':' -f2`
#code=`curl -s -I ${geturl} | grep HTTP/ | cut -d' ' -f2`
#echo [${code}] ${geturl} >>get.txt
#sleep 1.5s
# 404じゃなかったらファイルを保存
#if [ ${code} -eq 200 ]; then
echo "download $geturl} ..."
curl -O -s ${geturl}
echo "saved..." >>get.txt
#fi
sleep 1s
done
fi
# 存在しなかったファイルを削除(file sizeが10k以下の場合とする)
{
echo "==========================="
echo "存在しなかったファイル(file sizeが10k以下のもの)を削除"
echo "==========================="
} >>get.txt
find . -name "*${SUFFIX}" -size -10k >>get.txt
count=`find . -name "*${SUFFIX}" -size -10k | wc -l`
echo "存在しないファイルを[${count}個]削除します"
find . -name "*${SUFFIX}" -size -10k | xargs rm
echo "end.................."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment