1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/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, 2020 Etersoft
# Copyright (C) 2014 Daniil Mikhailov <danil@etersoft.ru>
# Copyright (C) 2016-2017, 2020 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/>.
#
fatal()
{
echo "FATAL: $*" >&2
exit 1
}
WGETQ='' #-q
CURLQ='' #-s
set_quiet()
{
WGETQ='-q'
CURLQ='-s'
}
# TODO: passthrou all wget options
if [ "$1" = "-q" ] ; then
set_quiet
shift
fi
WGET="$(which wget 2>/dev/null)"
if [ -n "$WGET" ] ; then
# put remote content to stdout
scat()
{
$WGET $WGETQ -O- "$1"
}
# download to default name of to $2
sget()
{
if [ -n "$2" ] ; then
$WGET $WGETQ -O "$2" "$1"
else
$WGET $WGETQ "$1"
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"
# put remote content to stdout
scat()
{
$CURL -L $CURLQ "$1"
}
# download to default name of to $2
sget()
{
if [ -n "$2" ] ; then
$CURL -L $CURLQ --output "$2" "$1"
else
$CURL -L $CURLQ -O "$1"
fi
}
fi
LISTONLY=''
if [ "$1" = "--list" ] ; then
LISTONLY="$1"
set_quiet
shift
fi
LATEST=''
if [ "$1" = "--latest" ] ; then
LATEST="$1"
shift
fi
fatal()
{
echo "$*" >&2
exit 1
}
# check man glob
filter_glob()
{
[ -z "$1" ] && cat && return
# translate glob to regexp
grep "$(echo "$1" | sed -e "s|\*|.*|g" -e "s|?|.|g")$"
}
filter_order()
{
[ -z "$LATEST" ] && cat && return
sort | tail -n1
}
# download to this file
TARGETFILE=''
if [ "$1" = "-O" ] ; then
TARGETFILE="$2"
shift 2
fi
# TODO:
# -P support
if [ -z "$1" ] ; then
echo "eget - wget like downloader" >&2
fatal "Run $0 --help to get help"
fi
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo "eget - wget like downloader with wildcard support in filename part of URL"
echo "Usage: eget [-q] [-O target file] [--list] http://somesite.ru/dir/na*.log"
echo
echo "Options:"
echo " -q - quiet mode"
echo " -O file - download to this file (use filename from server if missed)"
echo " --list - print files from url with mask"
echo " --latest - print only latest version of file"
echo
echo "eget supports --list and download for https://github.com/owner/project urls"
echo
# echo "See $ wget --help for wget options you can use here"
exit
fi
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"
local URL="https://api.github.com/repos/$owner/$project/releases"
scat $URL | \
grep -i -o -E '"browser_download_url": "https://.*"' | cut -d'"' -f4
}
if echo "$1" | grep -q "^https://github.com/" && \
echo "$1" | grep -q -v "/download/" && [ -n "$2" ] ; then
MASK="$2"
if [ -n "$LISTONLY" ] ; then
get_github_urls "$1" | filter_glob "$MASK" | filter_order
exit
fi
for fn in $(get_github_urls "$1" | filter_glob "$MASK" | filter_order) ; do
sget "$fn" || ERROR=1
done
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 "TODO: list files for ftp:// do not supported yet"
sget "$1" "$TARGETFILE"
exit
fi
# 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
if echo "$URL" | grep -q "[*?]" ; then
fatal "Error: there are globbing symbols (*?) in $URL"
fi
# If have no wildcard symbol like asterisk, just download
if echo "$MASK" | grep -qv "[*?]" || echo "$MASK" | grep -q "[?].*="; then
sget "$1" "$TARGETFILE"
exit
fi
is_url()
{
echo "$1" | grep -q "://"
}
get_urls()
{
scat $URL/ | \
grep -i -o -E 'href="(.+)"' | cut -d'"' -f2
}
if [ -n "$LISTONLY" ] ; then
for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do
is_url "$fn" && echo $fn && continue
fn="$(echo "$fn" | sed -e 's|^./||' -e 's|^/+||')"
echo "$URL/$fn"
done
exit
fi
ERROR=0
for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do
sget "$URL/$(basename "$fn")" || ERROR=1
done
exit $ERROR