tools_eget 8.38 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)
3
# Use:
4
# eget http://ftp.altlinux.ru/pub/security/ssl/*
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5
#
6
# Copyright (C) 2014-2014, 2016, 2020, 2022  Etersoft
7
# Copyright (C) 2014 Daniil Mikhailov <danil@etersoft.ru>
8
# Copyright (C) 2016-2017, 2020, 2022 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/>.
#

24 25 26 27 28 29
fatal()
{
    echo "FATAL: $*" >&2
    exit 1
}

30 31
# TODO:
arch="$(uname -m)"
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

# copied from eepm project

# copied from /etc/init.d/outformat (ALT Linux)
isatty()
{
	# Set a sane TERM required for tput
	[ -n "$TERM" ] || TERM=dumb
	export TERM
	test -t 1
}

isatty2()
{
	# check stderr
	test -t 2
}


check_tty()
{
	isatty || return
	which tput >/dev/null 2>/dev/null || return
	# FreeBSD does not support tput -S
	echo | tput -S >/dev/null 2>/dev/null || return
	[ -z "$USETTY" ] || return
	export USETTY=1
}

: ${BLACK:=0} ${RED:=1} ${GREEN:=2} ${YELLOW:=3} ${BLUE:=4} ${MAGENTA:=5} ${CYAN:=6} ${WHITE:=7}

set_boldcolor()
{
	[ "$USETTY" = "1" ] || return
	{
		echo bold
		echo setaf $1
	} |tput -S
}

restore_color()
{
	[ "$USETTY" = "1" ] || return
	{
		echo op; # set Original color Pair.
		echo sgr0; # turn off all special graphics mode (bold in our case).
	} |tput -S
}

echover()
{
    [ -n "$verbose" ] || return
    echo "$*" >&2
}

# Print command line and run command line
showcmd()
{
	if [ -z "$quiet" ] ; then
		set_boldcolor $GREEN
		local PROMTSIG="\$"
		[ "$UID" = 0 ] && PROMTSIG="#"
		echo " $PROMTSIG $@"
		restore_color
	fi >&2
}

# Print command line and run command line
docmd()
{
	showcmd "$@"
	"$@"
}

check_tty

108 109
WGETNOSSLCHECK=''
CURLNOSSLCHECK=''
110 111
WGETUSERAGENT=''
CURLUSERAGENT=''
112 113
WGETQ='' #-q
CURLQ='' #-s
Vitaly Lipatov's avatar
Vitaly Lipatov committed
114 115
WGETNAMEOPTIONS='--content-disposition'
CURLNAMEOPTIONS='--remote-name --remote-header-name'
116 117 118 119 120 121

set_quiet()
{
    WGETQ='-q'
    CURLQ='-s'
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
122

123 124
# TODO: parse options in a good way

125
# TODO: passthrou all wget options
Vitaly Lipatov's avatar
Vitaly Lipatov committed
126
if [ "$1" = "-q" ] ; then
127
    set_quiet
Vitaly Lipatov's avatar
Vitaly Lipatov committed
128 129 130
    shift
fi

131 132 133 134 135 136
if [ "$1" = "-k" ] || [ "$1" = "--no-check-certificate" ] ; then
    WGETNOSSLCHECK='--no-check-certificate'
    CURLNOSSLCHECK='-k'
    shift
fi

137
if [ "$1" = "-U" ] || [ "$1" = "-A" ] || [ "$1" = "--user-agent" ] ; then
138 139 140
    user_agent="Mozilla/5.0 (X11; Linux $arch)"
    WGETUSERAGENT="-U '$user_agent'"
    CURLUSERAGENT="-A '$user_agent'"
141 142 143
    shift
fi

144 145 146 147

WGET="$(which wget 2>/dev/null)"

if [ -n "$WGET" ] ; then
148 149 150 151 152 153 154 155
__wget()
{
    if [ -n "$WGETUSERAGENT" ] ; then
        docmd $WGET $WGETQ $WGETNOSSLCHECK "$WGETUSERAGENT" "$@"
    else
        docmd $WGET $WGETQ $WGETNOSSLCHECK "$@"
    fi
}
156 157 158
# put remote content to stdout
scat()
{
159
    __wget -O- "$1"
160 161 162 163
}
# download to default name of to $2
sget()
{
164 165 166
    if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then
       scat "$1"
    elif [ -n "$2" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
167
       docmd __wget -O "$2" "$1"
168
    else
169 170 171 172
# TODO: поддержка rsync для известных хостов?
# Не качать, если одинаковый размер и дата
# -nc
# TODO: overwrite always
Vitaly Lipatov's avatar
Vitaly Lipatov committed
173
       docmd __wget $WGETNAMEOPTIONS "$1"
174 175 176 177 178 179
    fi
}

else
CURL="$(which curl 2>/dev/null)"
[ -n "$CURL" ] || fatal "There are no wget nor curl in the system. Install it with $ epm install curl"
180 181 182 183 184 185 186 187
__curl()
{
    if [ -n "$CURLUSERAGENT" ] ; then
        docmd $CURL -L $CURLQ "$CURLUSERAGENT" $CURLNOSSLCHECK "$@"
    else
        docmd $CURL -L $CURLQ $CURLNOSSLCHECK "$@"
    fi
}
188 189 190
# put remote content to stdout
scat()
{
191
    __curl "$1"
192 193 194 195
}
# download to default name of to $2
sget()
{
196 197 198
    if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then
       scat "$1"
    elif [ -n "$2" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
199
       __curl --output "$2" "$1"
200
    else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
201
       __curl $CURLNAMEOPTIONS "$1"
202 203 204 205
    fi
}
fi

206
LISTONLY=''
207 208
if [ "$1" = "--list" ] ; then
    LISTONLY="$1"
209
    set_quiet
210 211 212
    shift
fi

213 214 215 216 217 218
LATEST=''
if [ "$1" = "--latest" ] ; then
    LATEST="$1"
    shift
fi

219 220 221 222 223 224
fatal()
{
    echo "$*" >&2
    exit 1
}

225 226 227
# check man glob
filter_glob()
{
228
	[ -z "$1" ] && cat && return
229
	# translate glob to regexp
230
	grep "$(echo "$1" | sed -e "s|\*|.*|g" -e "s|?|.|g")$"
231 232
}

233 234 235
filter_order()
{
    [ -z "$LATEST" ] && cat && return
236
    sort -V | tail -n1
237
}
238

239
# download to this file
240
TARGETFILE=''
241 242 243
if [ "$1" = "-O" ] ; then
    TARGETFILE="$2"
    shift 2
244 245 246
elif [ "$1" = "-O-" ] ; then
    TARGETFILE="-"
    shift 1
247 248
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
249 250
# TODO:
# -P support
Vitaly Lipatov's avatar
Vitaly Lipatov committed
251

252
if [ -z "$1" ] ; then
253
    echo "eget - wget like downloader wrapper with wildcard support" >&2
254
    fatal "Run $0 --help to get help"
255 256
fi

257
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
258
    echo "eget - wget like downloader wrapper with wildcard support in filename part of URL"
259
    echo "Usage: eget [-q] [-k] [-U] [-O target file] [--list] http://somesite.ru/dir/na*.log"
260
    echo
261
    echo "Options:"
262
    echo "    -q       - quiet mode"
263
    echo "    -k|--no-check-certificate - skip SSL certificate chain support"
264
    echo "    -U|-A|--user-agent - send browser like UserAgent"
265 266
    echo "    -O file  - download to this file (use filename from server if missed)"
    echo "    --list   - print files from url with mask"
267
    echo "    --latest - print only latest version of a file"
268
    echo
269 270
    echo "eget supports --list and download for https://github.com/owner/project urls"
    echo
271 272 273 274 275
    echo "Examples:"
    echo "  $ eget --list http://ftp.somesite.ru/package-*.tar"
    echo "  $ eget http://ftp.somesite.ru/package-*.x64.tar"
    echo "  $ eget --list http://download.somesite.ru 'package-*.tar.xz'"
    echo "  $ eget --list --latest https://github.com/telegramdesktop/tdesktop/releases 'tsetup.*.tar.xz'"
276
#    echo "See $ wget --help for wget options you can use here"
277 278 279
    exit
fi

280 281 282 283 284 285 286
get_github_urls()
{
    # https://github.com/OWNER/PROJECT
    local owner="$(echo "$1" | sed -e "s|^https://github.com/||" -e "s|/.*||")" #"
    local project="$(echo "$1" | sed -e "s|^https://github.com/$owner/||" -e "s|/.*||")" #"
    [ -n "$owner" ] || fatal "Can't get owner from $1"
    [ -n "$project" ] || fatal "Can't get project from $1"
287
    local URL="https://api.github.com/repos/$owner/$project/releases"
288
    scat $URL | \
289 290 291
        grep -i -o -E '"browser_download_url": "https://.*"' | cut -d'"' -f4
}

292 293
if echo "$1" | grep -q "^https://github.com/" && \
   echo "$1" | grep -q -v "/download/" && [ -n "$2" ] ; then
294 295 296
    MASK="$2"

    if [ -n "$LISTONLY" ] ; then
297
        get_github_urls "$1" | filter_glob "$MASK" | filter_order
298 299 300 301
        exit
    fi

    for fn in $(get_github_urls "$1" | filter_glob "$MASK" | filter_order) ; do
302
        sget "$fn" || ERROR=1
303
    done
304
    exit
305 306 307
fi


308
# do not support /
309
if echo "$1" | grep -q "/$" && [ -z "$2" ] ; then
310
    fatal "Use http://example.com/e/* to download all files in dir"
311 312
fi

313
# TODO: curl?
314 315
# If ftp protocol, just download
if echo "$1" | grep -q "^ftp://" ; then
316 317
    [ -n "$LISTONLY" ] && fatal "TODO: list files for ftp:// do not supported yet"
    sget "$1" "$TARGETFILE"
318 319 320
    exit
fi

321 322 323 324 325 326 327 328 329 330 331
# mask allowed only in the last part of path
MASK=$(basename "$1")

# if mask are second arg
if [ -n "$2" ] ; then
    URL="$1"
    MASK="$2"
else
    # drop mask part
    URL="$(dirname "$1")"
fi
332

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

337
# If have no wildcard symbol like asterisk, just download
338
if echo "$MASK" | grep -qv "[*?]" || echo "$MASK" | grep -q "[?].*="; then
339
    sget "$1" "$TARGETFILE"
340 341 342
    exit
fi

343 344 345 346 347
is_url()
{
    echo "$1" | grep -q "://"
}

348
get_urls()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
349
{
350 351
    # cat html, divide to lines by tags and cut off hrefs only
    scat $URL | sed -e 's|<|<\n|g' | \
352
         grep -i -o -E 'href="(.+)"' | cut -d'"' -f2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
353 354
}

355
if [ -n "$LISTONLY" ] ; then
356
    for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do
357 358 359
        is_url "$fn" && echo $fn && continue
        fn="$(echo "$fn" | sed -e 's|^./||' -e 's|^/+||')"
        echo "$URL/$fn"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
360
    done
361 362
    exit
fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
363

364
ERROR=0
365
for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do
366 367
    is_url "$fn" || fn="$URL/$(basename "$fn")"
    sget "$fn" || ERROR=1
368 369
done
exit $ERROR
Vitaly Lipatov's avatar
Vitaly Lipatov committed
370