|
| 1 | +function filename = download(URL, output_dir, verbose) |
| 2 | + % |
| 3 | + % USAGE:: |
| 4 | + % |
| 5 | + % filename = download(URL, output_dir, verbose) |
| 6 | + % |
| 7 | + % (C) Copyright 2021 Remi Gau |
| 8 | + if nargin < 2 |
| 9 | + output_dir = pwd; |
| 10 | + end |
| 11 | + |
| 12 | + msg = sprintf('Downloading dataset from:\n %s\n\n', URL); |
| 13 | + print_to_screen(msg, verbose); |
| 14 | + |
| 15 | + tokens = regexp(URL, '/', 'split'); |
| 16 | + protocol = tokens{1}; |
| 17 | + |
| 18 | + filename = tokens{end}; |
| 19 | + |
| 20 | + if exist(filename, 'file') |
| 21 | + delete(filename); |
| 22 | + end |
| 23 | + |
| 24 | + if strcmp(protocol, 'http:') |
| 25 | + |
| 26 | + if isunix() |
| 27 | + if verbose |
| 28 | + system(sprintf('wget %s', URL)); |
| 29 | + else |
| 30 | + system(sprintf('wget -q %s', URL)); |
| 31 | + end |
| 32 | + else |
| 33 | + urlwrite(URL, filename); |
| 34 | + end |
| 35 | + |
| 36 | + % move file in case it was not downloaded in the root dir |
| 37 | + if ~exist(fullfile(output_dir, filename), 'file') |
| 38 | + print_to_screen([filename ' --> ' output_dir], verbose); |
| 39 | + movefile(filename, fullfile(output_dir, filename)); |
| 40 | + end |
| 41 | + filename = fullfile(output_dir, filename); |
| 42 | + |
| 43 | + else |
| 44 | + |
| 45 | + ftp_server = tokens{3}; |
| 46 | + ftpobj = ftp(ftp_server); |
| 47 | + |
| 48 | + filename = strjoin(tokens(4:end), '/'); |
| 49 | + filename = mget(ftpobj, filename, output_dir); |
| 50 | + |
| 51 | + end |
| 52 | + |
| 53 | + if iscell(filename) |
| 54 | + filename = filename{1}; |
| 55 | + end |
| 56 | + |
| 57 | + print_to_screen(' Done\n\n', verbose); |
| 58 | + |
| 59 | +end |
| 60 | + |
| 61 | +function print_to_screen(msg, verbose) |
| 62 | + if verbose |
| 63 | + fprintf(1, msg); |
| 64 | + end |
| 65 | +end |
0 commit comments