Skip to content

Instantly share code, notes, and snippets.

@jakubbalada
Created February 5, 2018 16:27
Show Gist options
  • Save jakubbalada/42206ec16ead2c3f62cb9f1c72ce0fc0 to your computer and use it in GitHub Desktop.
Save jakubbalada/42206ec16ead2c3f62cb9f1c72ce0fc0 to your computer and use it in GitHub Desktop.
function pageFunction(context) {
// called on every page the crawler visits, use it to extract data from it
var $ = context.jQuery;
var result = [];
var content = $('meta[id="_bootstrap-layout-init"]').attr("content");
var api_key = JSON.parse(content).api_config.key;
var listId = context.request.url.match(/[^\/]+$/g);
var getReviewData = function(offset) {
var reviewsAPI = "https://www.airbnb.com/api/v2/reviews?key=" + api_key + "&listing_id=" + listId + "&role=guest&_format=for_p3&_limit=50&_offset=" + offset;
$.ajax({
url: reviewsAPI,
type: 'GET',
dataType: 'json'
})
.done(function(data, textStatus, jqXHR) {
console.log("Review data fetched");
data.reviews.forEach(function(review) {
result.push(review);
});
var reviewsCount = data.metadata.reviews_count;
offset += 50;
if (offset > reviewsCount) {
context.finish(result);
} else
getReviewData(offset);
})
.fail(function(xhr, textStatus, errorThrown) {
console.log("Review API call failed");
context.finish(result);
})
.always(function() {
console.log("Review API call finished");
});
};
context.willFinishLater();
getReviewData(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment