diff --git a/airbnb.js b/airbnb.js index cd7cf30..6bf046c 100644 --- a/airbnb.js +++ b/airbnb.js @@ -8,6 +8,7 @@ * - getCalendar() - Get availability for a hosting * - getEstIncome() - Get estimate income a hosting generates, by month * - getReviews() - Get reviews for a given user, as host or guest + * - getListingReviews() - Get reviews for a given listing id */ var search = require('./api/search'), @@ -15,6 +16,7 @@ var search = require('./api/search'), getCalendar = require('./api/getCalendar'), getReviews = require('./api/getReviews'), getEstIncome = require('./api/getEstIncome'), + getListingReviews = require('./api/getListingReviews'), login = require('./api/login'); var USERNAME = 'username', @@ -30,6 +32,7 @@ var AirbnbClient = function(optionalConfigs) { getCalendar: getCalendar, getReviews: getReviews, getEstIncome: getEstIncome, + getListingReviews: getListingReviews, login: login }; }; diff --git a/api/getListingReviews.js b/api/getListingReviews.js new file mode 100644 index 0000000..38b96e9 --- /dev/null +++ b/api/getListingReviews.js @@ -0,0 +1,57 @@ +var request = require('request'), + _ = require('lodash'), + configs = require('../configs/configs'), + serialize = require('../helpers/serialize'), + Promise = require('bluebird'); + +/** + * Get a list of reviews for a given listing id, as all. Details: http://airbnbapi.org/#get-reviews + * @param {Number, String} listingId + * @param {Object} options - Options to specify _limit, _offset, etc. + * @return {Promise} - contains list of reviews + * + * options = { + * _limit: {Number}, default 50 (max 50) + * _offset: {Number}, default 0 + * locale: {String}, default 'en' + * _order: {String}, default 'language' + * role: {String}, default 'all' + * } + */ +function getListingReviews(listingId, options) { + options.listing_id = listingId; + var DEFAULT_LISTING_REVIEWS_PARAMS = _.assign({}, configs.DEFAULT_REQUEST_PARAMS, { + _limit: 50, + _offset: 0, + locale: 'en', + _order: 'language', + role: 'all' + }), + searchOptions = _.assign({}, DEFAULT_LISTING_REVIEWS_PARAMS, options), + requestConfigs = _.assign({}, configs.DEFAULT_REQUEST_CONFIGS, { + url: configs.LISTING_REVIEWS_URL + '?' + serialize(searchOptions) + }), + reviews = [], + $; + + return new Promise(function(resolve, reject) { + if (!listingId) { + reject('Invalid listing ID'); + } + + request(requestConfigs, function(err, res, body) { + body = JSON.parse(body); + if (!err && res.statusCode == 200) { + if (body.error_code){ + return reject(body.error_details); + } + resolve(body.reviews); + } else if (err) { + reject(err); + } + }); + }); + +} + +module.exports = getListingReviews; diff --git a/configs/configs.js b/configs/configs.js index d8ea2a8..c821ad0 100644 --- a/configs/configs.js +++ b/configs/configs.js @@ -2,9 +2,11 @@ var secrets = require('./_secrets'), _ = require('lodash'); var AIRBNB_PREFIX = 'https://www.airbnb.com', + AIRBNB_API_PREFIX = 'https://api.airbnb.com', SEARCH_URL = AIRBNB_PREFIX + '/search/search_results', AVAILABILITY_URL = AIRBNB_PREFIX + '/api/v2/calendar_months', HOSTING_INFO_URL = AIRBNB_PREFIX + '/api/v1/listings', + LISTING_REVIEWS_URL = AIRBNB_API_PREFIX + '/v2/reviews', USER_REVIEWS_URL = AIRBNB_PREFIX + '/users/review_page', LOGIN_URL = AIRBNB_PREFIX + '/api/v1/authorize', DEFAULT_REQUEST_CONFIGS = { @@ -18,9 +20,11 @@ var AIRBNB_PREFIX = 'https://www.airbnb.com', module.exports = { AIRBNB_PREFIX: AIRBNB_PREFIX, + AIRBNB_API_PREFIX: AIRBNB_API_PREFIX, SEARCH_URL: SEARCH_URL, AVAILABILITY_URL: AVAILABILITY_URL, HOSTING_INFO_URL: HOSTING_INFO_URL, + LISTING_REVIEWS_URL: LISTING_REVIEWS_URL, USER_REVIEWS_URL: USER_REVIEWS_URL, LOGIN_URL: LOGIN_URL, DEFAULT_REQUEST_CONFIGS: DEFAULT_REQUEST_CONFIGS,