Last active
December 5, 2017 04:31
-
-
Save mjohnsullivan/47aa935a6902baac8c2e9113d2b31148 to your computer and use it in GitHub Desktop.
Simple HTTP downloader script in Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2017 Matt Sullivan | |
// Governed by the 2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause | |
import 'dart:io'; | |
import 'package:http/http.dart' as http; | |
import 'package:args/args.dart'; | |
void main(List<String> args) { | |
var parsedArgs = parseArgs(args); | |
// download(parsedArgs[0], parsedArgs[1]); | |
progressiveDownload(parsedArgs[0], parsedArgs[1]); | |
} | |
/// Parse the command line args | |
List<String> parseArgs(List<String> args) { | |
var parser = new ArgParser() | |
..addOption('url', abbr: 'u', help: 'The url to download', valueHelp: 'url') | |
..addOption('filename', | |
abbr: 'f', help: 'save filename', valueHelp: 'filename') | |
..addFlag('help', abbr: 'h', help: 'help info'); | |
var parsedArgs = parser.parse(args); | |
if (parsedArgs['help']) { | |
print(parser.usage); | |
exit(0); | |
} | |
String url; | |
if (parsedArgs['url'] == null) { | |
stderr.writeln('A url must be provided'); | |
exit(1); | |
} else { | |
url = parsedArgs['url']; | |
} | |
var filename = parsedArgs['filename']; | |
if (filename == null) { | |
var uri = Uri.parse(url); | |
filename = | |
uri.pathSegments.isNotEmpty ? uri.pathSegments.last : 'output.html'; | |
} | |
return [url, filename]; | |
} | |
/// Downloads and saves the specified URI | |
void download(String url, String filename) { | |
http.get(Uri.parse(url)).then((res) { | |
switch (res.statusCode) { | |
case 200: | |
print('Downloading content of length: ${res.contentLength}'); | |
new File(filename).writeAsBytes(res.bodyBytes); | |
break; | |
default: | |
stderr.writeln('Unexpected response code : ${res.statusCode}'); | |
} | |
}); | |
} | |
/// Progressively downloads and saves the specified URI | |
void progressiveDownload(String url, String filename) { | |
var req = new http.Request('GET', Uri.parse(url)); | |
req.send().then((res) { | |
if (res.statusCode == 200) { | |
print('Downloading content of length: ${res.contentLength}'); | |
res.stream.map((chunk) { | |
print('Chunk size: ${chunk.length}'); | |
return chunk; | |
}).pipe(new File(filename).openWrite()); | |
} else { | |
stderr.writeln('Unexpected response code : ${res.statusCode}'); | |
res.stream.drain(); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: downloader | |
version: 0.0.1 | |
description: > | |
Simple command line download script | |
dependencies: | |
http: ^0.11.3 | |
args: ^1.0.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment