Created
December 10, 2015 01:39
-
-
Save aaronksaunders/fdd8459388ee7babad49 to your computer and use it in GitHub Desktop.
Meteor sample code to scrape data from a remote website
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
/** | |
* | |
* @param _type | |
* @param _url | |
* @param _data | |
* @param _callback | |
*/ | |
function getInfoFromURL(_type, _url, _data, _callback) { | |
Meteor.call("callServer", _url, _type, _data, function (error, results) { | |
if (error) { | |
_callback({ | |
error: error | |
}); | |
return; | |
} | |
console.log(results.content); //results.data should be a JSON object | |
var div = document.createElement('div'); | |
div.innerHTML = results.content; | |
var result = { | |
title: $(div).find('title').text(), | |
description: $(div).find("[title='Quick View']"), | |
data: [] | |
}; | |
_.each(result.description, function (_elm) { | |
var nameText = $(_elm).text(); | |
result.data.push({ | |
download: $(_elm).attr("href"), | |
name: nameText, | |
location: ($(_elm).parent().text()).split(nameText)[1] | |
}); | |
}); | |
console.log(result); | |
_callback(result); | |
}); | |
} | |
getInfoFromURL("POST", "http://www.aapdc.org/prx/", { | |
search_submitted: "true", | |
zip: "20011", | |
range: "4" | |
}, function (_response) { | |
console.log(_response); | |
}); |
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
Meteor.methods({ | |
callServer: function (_url, _type, _data) { | |
console.log("_url: " + _url); | |
console.log("_type: " + _type); | |
console.log("_data: " + JSON.stringify(_data)); | |
this.unblock(); | |
return Meteor.http.call("POST", _url, { | |
params: _data, | |
headers: { | |
"Content-Type": "application/x-www-form-urlencoded" | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment