Created
April 28, 2012 08:00
-
-
Save greenido/2517000 to your computer and use it in GitHub Desktop.
Dart Crawler (server side example)
This file contains hidden or 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
#import('dart:io'); | |
#import('dart:uri'); | |
#import('dart:json'); | |
// Dart Hackathon TLV 2012 | |
// | |
// A simple example to fetch JSON and parse it on the server side | |
// This is a good start for a crawler and/or any other web service | |
// we wish to create using dart on the server side. | |
// | |
// Author: Ido Green | greenido.wordpress.com | |
// Date: 28/4/2012 | |
// | |
void main() { | |
HttpClient client = new HttpClient(); | |
// lets create a simple pipe that bring us the 'future' IPOs that are | |
// going to take place in the US. | |
Uri pipeUrl = new Uri.fromString("http://pipes.yahoo.com/pipes/pipe.run?_id=c4fb5175c000f1e19e244bba36aca1e8&_render=json"); | |
// open a GET connection to fetch this data | |
var conn = client.getUrl(pipeUrl); | |
conn.onRequest = (HttpClientRequest request) { | |
request.outputStream.close(); | |
}; | |
conn.onResponse = (HttpClientResponse response) { | |
print("status code:" + response.statusCode); | |
var output = new File('ourData.json').openOutputStream(); | |
response.inputStream.pipe(output); | |
// In case you want to print the data to your console: | |
// response.inputStream.pipe(stdout); | |
}; | |
} // end of class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment