]> Sergey Matveev's repositories - feeder.git/blob - cmd/download-warcs.zsh
0198ca25c744d6aacd9e2f8bf272064430590b34
[feeder.git] / cmd / download-warcs.zsh
1 #!/usr/bin/env zsh
2 set -e
3 fpath=($0:h:a/functions.zsh $fpath)
4 dst=$2:a
5 cd $1
6 [[ -n "$dst" ]] || { dst=warcs ; dst=$dst:a }
7 mkdir -p $dst
8 autoload url-to-filename
9 zmodload -F zsh/datetime b:strftime
10 setopt EXTENDED_GLOB
11 wget_opts=(
12     --page-requisites
13     --compression=auto
14     --no-warc-keep-log
15     --no-warc-digests
16     --no-warc-compression
17 )
18 for new (new/*(N)) {
19     while read line ; do
20         [[ "$line" != "" ]] || break
21         cols=(${(s: :)line})
22         [[ $cols[1] = "X-URL:" ]] || continue
23         url=$cols[2]
24         [[ -n "$url" ]]
25         [[ -n "$tmp" ]] || {
26             # Lazy temporary file creation
27             tmp=`mktemp`
28             trap "rm -f $tmp" HUP PIPE INT QUIT TERM EXIT
29             wget_opts=(--output-document=$tmp $wget_opts)
30         }
31         fn=$(url-to-filename $url)-$(strftime %Y%m%d-%H%M%S)
32         wget $wget_opts --output-file=warcs.log --warc-file=$dst/$fn $url
33         print $dst/$fn.warc
34     done < $new
35 }