Commit 860d06ac authored by Vitaly Lipatov's avatar Vitaly Lipatov

rewrite eget

parent 38ab6652
#!/bin/sh #!/bin/sh
# eget - simply shell on wget for loading directories over http # eget - simply shell on wget for loading directories over http (wget does not support wildcard for http)
# Example use: # Example use:
# eget ftp://ftp.altlinux.ru/pub/security/ssl/* # eget http://ftp.altlinux.ru/pub/security/ssl/*
# #
# Copyright (C) 2014-2014, 2016 Etersoft # Copyright (C) 2014-2014, 2016 Etersoft
# Copyright (C) 2014 Daniil Mikhailov <danil@etersoft.ru> # Copyright (C) 2014 Daniil Mikhailov <danil@etersoft.ru>
...@@ -42,33 +42,37 @@ fi ...@@ -42,33 +42,37 @@ fi
if [ -z "$1" ] ; then if [ -z "$1" ] ; then
echo "eget - wget wrapper" >&2 echo "eget - wget wrapper" >&2
echo "Run with URL, like ftp://somesite.ru/dir/*.log" >&2 echo "Run with URL, like http://somesite.ru/dir/*.log" >&2
exit 1 exit 1
fi fi
# If ftp protocol or have no asterisk, just download # If ftp protocol, just download
# TODO: use has() if echo "$1" | grep -q "^ftp://" ; then
if echo "$1" | grep -q "^ftp://" || echo "$1" | grep -qv "[*?]" ; then $WGET $WGET_OPTION_TARGET "$1"
exit
fi
# drop mask part (if has /$, not changed)
URL=$(echo "$1" | grep "/$" || dirname "$1")
# If have no wildcard symbol like asterisk and no / at the end, just download
if [ "$URL" != "$1" ] && echo "$1" | grep -qv "[*?]" ; then
$WGET $WGET_OPTION_TARGET "$1" $WGET $WGET_OPTION_TARGET "$1"
exit exit
fi fi
echo "Fall to http workaround" echo "Fall to http workaround"
URL=$(echo "$1" | grep "/$" || dirname "$1")
# 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
get_index() # add * if full dir
{ #[ "$URL" != "$1" ] && MASK="*"
MYTMPDIR="$(mktemp -d)"
INDEX=$MYTMPDIR/index
$WGET $URL -O $INDEX
}
print_files() print_files()
{ {
cat $INDEX | grep -o -E 'href="([^\*/"#]+)"' | cut -d'"' -f2 $WGET -O- $URL | \
grep -o -E 'href="([^\*/"#]+)"' | cut -d'"' -f2
} }
create_fake_files() create_fake_files()
...@@ -79,19 +83,20 @@ create_fake_files() ...@@ -79,19 +83,20 @@ create_fake_files()
print_files | while read -r line ; do print_files | while read -r line ; do
touch $DIRALLFILES/$(basename "$line") touch $DIRALLFILES/$(basename "$line")
done done
rm -f $INDEX
} }
download_files() download_files()
{ {
ERROR=0 ERROR=0
# TODO: test fix / at the end
for line in $DIRALLFILES/$MASK ; do for line in $DIRALLFILES/$MASK ; do
[ -r "$line" ] || { ERROR=1 ; break ; }
$WGET $URL/$(basename "$line") || ERROR=1 $WGET $URL/$(basename "$line") || ERROR=1
done done
return $ERROR return $ERROR
} }
get_index || { rm -rf "$MYTMPDIR" ; exit ; } MYTMPDIR="$(mktemp -d)"
create_fake_files create_fake_files
download_files || echo "There was some download errors" >&2 download_files || echo "There was some download errors" >&2
rm -rf "$MYTMPDIR" 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