tools_eget 2.89 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1
#!/bin/sh
2
# eget - simply shell on wget for loading directories over http (wget does not support wildcard for http)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3
# Example use:
4
# eget http://ftp.altlinux.ru/pub/security/ssl/*
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5
#
6 7
# Copyright (C) 2014-2014, 2016  Etersoft
# Copyright (C) 2014 Daniil Mikhailov <danil@etersoft.ru>
8
# Copyright (C) 2016-2017 Vitaly Lipatov <lav@etersoft.ru>
Vitaly Lipatov's avatar
Vitaly Lipatov committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

Vitaly Lipatov's avatar
Vitaly Lipatov committed
24
WGET="wget"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
25

26
# TODO: passthrou all wget options
Vitaly Lipatov's avatar
Vitaly Lipatov committed
27 28 29 30 31
if [ "$1" = "-q" ] ; then
    WGET="wget -q"
    shift
fi

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
if [ "$1" = "--list" ] ; then
    LISTONLY="$1"
    shift
fi

fatal()
{
    echo "$*" >&2
    exit 1
}

# check man glob
filter_glob()
{
	# translate glob to regexp
	grep "^$(echo "$1" | sed -e "s|\*|.*|g" -e "s|\?|.|g")$"
}


51 52 53 54 55 56 57 58
# download to this file
WGET_OPTION_TARGET=
if [ "$1" = "-O" ] ; then
    TARGETFILE="$2"
    WGET_OPTION_TARGET="-O $2"
    shift 2
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
59 60
# TODO:
# -P support
Vitaly Lipatov's avatar
Vitaly Lipatov committed
61

62 63
if [ -z "$1" ] ; then
    echo "eget - wget wrapper" >&2
64 65 66
    fatal "Run with URL, like http://somesite.ru/dir/*.log"
fi

67 68 69 70 71 72 73 74 75 76
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
    echo "eget - wget wrapper, with support"
    echo "Usage: eget [-O target file] [--list] http://somesite.ru/dir/na*.log"
    echo "Options:"
    echo "    --list - print files frm url with mask"
    echo
    wget --help
    exit
fi

77 78 79
# do not support /
if echo "$1" | grep -q "/$" ; then
    fatal "Use http://example.com/e/* to download all files in dir"
80 81 82 83
fi

# If ftp protocol, just download
if echo "$1" | grep -q "^ftp://" ; then
84
    [ -n "$LISTONLY" ] && fatal "Error: list files for ftp:// do not supported yet"
85 86 87 88
    $WGET $WGET_OPTION_TARGET "$1"
    exit
fi

89 90
# drop mask part
URL="$(dirname "$1")/"
91

92 93
if echo "$URL" | grep -q "[*?]" ; then
    fatal "Error: there are globbing symbols (*?) in $URL"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
94 95
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
96 97
# mask allowed only in last part of path
MASK=$(basename "$1")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
98

99 100 101 102 103 104 105
# If have no wildcard symbol like asterisk, just download
if echo "$MASK" | grep -qv "[*?]" ; then
    $WGET $WGET_OPTION_TARGET "$1"
    exit
fi

get_urls()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
106
{
107 108
    $WGET -O- $URL | \
        grep -o -E 'href="([^\*/"#]+)"' | cut -d'"' -f2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
109 110
}

111 112 113 114
if [ -n "$LISTONLY" ] ; then
    WGET="$WGET -q"
    for fn in $(get_urls | filter_glob "$MASK") ; do
        echo "$(basename "$fn")"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
115
    done
116 117
    exit
fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
118

119 120 121 122 123
ERROR=0
for fn in $(get_urls | filter_glob "$MASK") ; do
    $WGET "$URL/$(basename "$fn")" || ERROR=1
done
exit $ERROR
Vitaly Lipatov's avatar
Vitaly Lipatov committed
124