#!/bin/sh # eget - simply shell on wget for loading directories over http (wget does not support wildcard for http) # Example use: # eget http://ftp.altlinux.ru/pub/security/ssl/* # # Copyright (C) 2014-2014, 2016 Etersoft # Copyright (C) 2014 Daniil Mikhailov <danil@etersoft.ru> # Copyright (C) 2016-2017 Vitaly Lipatov <lav@etersoft.ru> # # 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/>. # WGET="wget" # TODO: passthrou all wget options if [ "$1" = "-q" ] ; then WGET="wget -q" shift fi 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")$" } # download to this file WGET_OPTION_TARGET= if [ "$1" = "-O" ] ; then TARGETFILE="$2" WGET_OPTION_TARGET="-O $2" shift 2 fi # TODO: # -P support if [ -z "$1" ] ; then echo "eget - wget wrapper" >&2 fatal "Run with URL, like http://somesite.ru/dir/*.log" fi 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 # do not support / if echo "$1" | grep -q "/$" ; then fatal "Use http://example.com/e/* to download all files in dir" fi # If ftp protocol, just download if echo "$1" | grep -q "^ftp://" ; then [ -n "$LISTONLY" ] && fatal "Error: list files for ftp:// do not supported yet" $WGET $WGET_OPTION_TARGET "$1" exit fi # drop mask part URL="$(dirname "$1")/" if echo "$URL" | grep -q "[*?]" ; then fatal "Error: there are globbing symbols (*?) in $URL" fi # mask allowed only in last part of path MASK=$(basename "$1") # If have no wildcard symbol like asterisk, just download if echo "$MASK" | grep -qv "[*?]" ; then $WGET $WGET_OPTION_TARGET "$1" exit fi get_urls() { $WGET -O- $URL | \ grep -o -E 'href="([^\*/"#]+)"' | cut -d'"' -f2 } if [ -n "$LISTONLY" ] ; then WGET="$WGET -q" for fn in $(get_urls | filter_glob "$MASK") ; do echo "$(basename "$fn")" done exit fi ERROR=0 for fn in $(get_urls | filter_glob "$MASK") ; do $WGET "$URL/$(basename "$fn")" || ERROR=1 done exit $ERROR