Commit 89894e9c authored by Vitaly Lipatov's avatar Vitaly Lipatov

update internal eget up to 3.0

parent 3d1ff000
...@@ -23,12 +23,31 @@ ...@@ -23,12 +23,31 @@
WGET="wget" WGET="wget"
# TODO: passthrou all wget options
if [ "$1" = "-q" ] ; then if [ "$1" = "-q" ] ; then
WGET="wget -q" WGET="wget -q"
shift shift
fi fi
# TODO: 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 # download to this file
WGET_OPTION_TARGET= WGET_OPTION_TARGET=
if [ "$1" = "-O" ] ; then if [ "$1" = "-O" ] ; then
...@@ -42,61 +61,54 @@ fi ...@@ -42,61 +61,54 @@ fi
if [ -z "$1" ] ; then if [ -z "$1" ] ; then
echo "eget - wget wrapper" >&2 echo "eget - wget wrapper" >&2
echo "Run with URL, like http://somesite.ru/dir/*.log" >&2 fatal "Run with URL, like http://somesite.ru/dir/*.log"
exit 1 fi
# do not support /
if echo "$1" | grep -q "/$" ; then
fatal "Use http://example.com/e/* to download all files in dir"
fi fi
# If ftp protocol, just download # If ftp protocol, just download
if echo "$1" | grep -q "^ftp://" ; then if echo "$1" | grep -q "^ftp://" ; then
[ -n "$LISTONLY" ] && fatal "Error: list files for ftp:// do not supported yet"
$WGET $WGET_OPTION_TARGET "$1" $WGET $WGET_OPTION_TARGET "$1"
exit exit
fi fi
# drop mask part (if has /$, not changed) # drop mask part
URL=$(echo "$1" | grep "/$" || dirname "$1") URL="$(dirname "$1")/"
# If have no wildcard symbol like asterisk and no / at the end, just download if echo "$URL" | grep -q "[*?]" ; then
if [ "$URL" != "$1" ] && echo "$1" | grep -qv "[*?]" ; then fatal "Error: there are globbing symbols (*?) in $URL"
$WGET $WGET_OPTION_TARGET "$1"
exit
fi fi
echo "Fall to http workaround"
# mask allowed only in last part of path # mask allowed only in last part of path
MASK=$(basename "$1") MASK=$(basename "$1")
# TODO: skip create_fake_files for full dir
# add * if full dir
#[ "$URL" != "$1" ] && MASK="*"
print_files() # 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 | \ $WGET -O- $URL | \
grep -o -E 'href="([^\*/"#]+)"' | cut -d'"' -f2 grep -o -E 'href="([^\*/"#]+)"' | cut -d'"' -f2
} }
create_fake_files() if [ -n "$LISTONLY" ] ; then
{ WGET="$WGET -q"
DIRALLFILES="$MYTMPDIR/files/" for fn in $(get_urls | filter_glob "$MASK") ; do
mkdir -p "$DIRALLFILES" echo "$(basename "$fn")"
print_files | while read -r line ; do
touch $DIRALLFILES/$(basename "$line")
done done
} exit
fi
download_files() ERROR=0
{ for fn in $(get_urls | filter_glob "$MASK") ; do
ERROR=0 $WGET "$URL/$(basename "$fn")" || ERROR=1
# TODO: test fix / at the end done
for line in $DIRALLFILES/$MASK ; do exit $ERROR
[ -r "$line" ] || { ERROR=1 ; break ; }
$WGET $URL/$(basename "$line") || ERROR=1
done
return $ERROR
}
MYTMPDIR="$(mktemp -d)"
create_fake_files
download_files || echo "There was some download errors" >&2
rm -rf "$MYTMPDIR"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment