Shell 脚本,用来获取基于 moebooru 的网站上的原图链接,如 yande.re、konachan.com 等。
下载
$ curl -O "https://github.com/qianbinbin/moebooru-crawler/raw/master/moebooru-crawler.sh"
$ # git clone git@github.com:qianbinbin/moebooru-crawler.git && cd moebooru-crawler
$ chmod +x ./moebooru-crawler.sh
使用
Usage: moebooru-crawler URL [ -n NUM, --num=NUM ]
-n NUM, --num=NUM print NUM links of images,
or print all if NUM is '0'
示例
获取指定页面上的图片链接
$ ./moebooru-crawler.sh "https://yande.re/post?tags=coffee-kizoku+order%3Ascore"
将链接保存到文件
$ ./moebooru-crawler.sh "https://yande.re/post?tags=coffee-kizoku+order%3Ascore" >>links.txt
然后可以用 aria2c 之类的工具批量下载。
获取指定数量的图片(当页面多于一页时)
$ ./moebooru-crawler.sh "https://yande.re/post?page=2&tags=coffee-kizoku" -n 100 # "page=2" 会被忽略
要下载所有页面上的图片,使用 -n 0
即可:
$ ./moebooru-crawler.sh "https://yande.re/post?tags=coffee-kizoku" -n 0
源码
https://github.com/qianbinbin/moebooru-crawler
#!/bin/bash
DIR=
NUM=0
URLS_ONLY=false
MAX_PROCS=8
################################################################################
# PARSE ARGS
usage() {
echo "Usage: moebooru-crawler URL [ -d | --dir DIR ]
[ -n | --num NUM ]
[ -u | --urls-only ]
[ -p | --max-procs PROCS ]"
exit 2
}
PARSED_ARGUMENTS=$(getopt -a -n moebooru-crawler -o d:n:up: --long dir:,num:,urls-only,max-procs: -- "$@")
VALID_ARGUMENTS=$?
[ "$VALID_ARGUMENTS" != 0 ] && usage
eval set -- "$PARSED_ARGUMENTS"
while :; do
case "$1" in
-d | --dir) DIR="$2" ; shift 2 ;;
-n | --num) NUM="$2" ; shift 2 ;;
-u | --urls-only) URLS_ONLY=true ; shift ;;
-p | --max-procs) MAX_PROCS="$2" ; shift 2 ;;
--) shift; break ;;
esac
done
URL="$1"
[ -z "$URL" ] && usage
################################################################################
# FETCH LINKS
get_links() {
local content
content=$(curl -fsSL "$1")
echo "$content" | grep -P -o 'file_url=".*?"' | grep -o 'http[^"]*'
}
IFS="?"; read -ra PARTS <<< "$URL"; unset IFS
path=${PARTS[0]}
path+=".xml"
query=${PARTS[1]}
links=
if [ "$NUM" -le 0 ]; then
url="$path"
[ -n "$query" ] && url+="?$query"
links=$(get_links "$url")
else
query=$(sed "s/&\?page=[0-9]*//g" <<< "$query")
[ -n "$query" ] && query+="&"
page=1
while [ "$(wc -w <<< "$links")" -lt "$NUM" ]; do
p="page=$page"
url="$path?$query$p"
new_links=$(get_links "$url")
[ "$(wc -w <<< "$new_links")" -eq 0 ] && break
links+=" $new_links"
((page+=1))
done
links=$(xargs -n 1 <<< "$links")
links=$(head -n "$NUM" <<< "$links")
fi
[ "$URLS_ONLY" = true ] && echo "$links" && exit 0
################################################################################
# DOWNLOAD FILES
if [ "$DIR" ] ; then
[ ! -d "$DIR" ] && mkdir -p "$DIR"
cd "$DIR" || exit 1
fi
dir=$(sed "s/[^A-Za-z0-9\._-]/_/g" <<< "${URL#*://}")
[ "$NUM" -gt 0 ] && dir+="-$NUM"
[ ! -d "$dir" ] && mkdir "$dir"
cd "$dir" || exit 1
# do not use buggy curl --parallel
echo "$links" | xargs -n 1 -P "$MAX_PROCS" -I {} bash -c 'curl -fsSL --retry 4 -O "{}" || echo "failed to download: {}" 1>&2'