|
Revision 2129, 1.3 KB
(checked in by Chuck.Esterbrook, 3 years ago)
|
|
Take out is shared on def main in the how-to's.
|
-
Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 | """ |
|---|
| 2 | download.cobra |
|---|
| 3 | |
|---|
| 4 | Downloads a file specified by a URL to the local file system. |
|---|
| 5 | |
|---|
| 6 | Windows: |
|---|
| 7 | > cd \path\to\directory\of\download.cobra |
|---|
| 8 | > cobra -c download.cobra |
|---|
| 9 | > download http://www.google.com/images/logo_sm.gif |
|---|
| 10 | Downloading http://www.google.com/images/logo_sm.gif to logo_sm.gif |
|---|
| 11 | Done. |
|---|
| 12 | |
|---|
| 13 | Unix-like: |
|---|
| 14 | > cd /path/to/directory/of/download.cobra |
|---|
| 15 | > cobra -c Download.cobra |
|---|
| 16 | > mono download.exe http://www.google.com/images/logo_sm.gif |
|---|
| 17 | Downloading http://www.google.com/images/logo_sm.gif to logo_sm.gif |
|---|
| 18 | Done. |
|---|
| 19 | |
|---|
| 20 | You can specify the local filename: |
|---|
| 21 | > download http://www.google.com/images/logo_sm.gif GoogleLogo.gif |
|---|
| 22 | Downloading http://www.google.com/images/logo_sm.gif to GoogleLogo.gif |
|---|
| 23 | Done. |
|---|
| 24 | |
|---|
| 25 | """ |
|---|
| 26 | |
|---|
| 27 | use System.Net |
|---|
| 28 | |
|---|
| 29 | class DownloadFile |
|---|
| 30 | |
|---|
| 31 | def main |
|---|
| 32 | args = CobraCore.commandLineArgs |
|---|
| 33 | if args.count < 2 |
|---|
| 34 | print 'usage: download URL \[LOCALFILENAME]' |
|---|
| 35 | return |
|---|
| 36 | url = args[1] |
|---|
| 37 | |
|---|
| 38 | # localFileName is derived from the url if missing: |
|---|
| 39 | if args.count > 2 |
|---|
| 40 | localFileName = args[2] |
|---|
| 41 | else |
|---|
| 42 | s = url |
|---|
| 43 | if s.endsWith('/'), s = s[:-1] |
|---|
| 44 | i = s.lastIndexOf('/') |
|---|
| 45 | if i <> -1, localFileName = s[i+1:] |
|---|
| 46 | else, print 'Invalid URL.' |
|---|
| 47 | |
|---|
| 48 | print 'Downloading [url] to [localFileName]' |
|---|
| 49 | Console.out.flush |
|---|
| 50 | |
|---|
| 51 | try |
|---|
| 52 | WebClient().downloadFile(url, localFileName) |
|---|
| 53 | catch e as Exception |
|---|
| 54 | print e |
|---|
| 55 | |
|---|
| 56 | print 'Done.' |
|---|