tripadvisor_api_module_v1
Version:
TripAdvisor API Module
214 lines • 9.79 kB
JavaScript
export class TripadvisorAPI {
TRIPADVISOR_API_KEY;
constructor(key) {
this.TRIPADVISOR_API_KEY = key;
}
/**
* Nearby Search
* API Document: https://tripadvisor-content-api.readme.io/reference/searchfornearbylocations
* API URL https://api.content.tripadvisor.com/api/v1/location/nearby_search
* @param latLong string (required) Latitude/Longitude pair to scope down the search around a specifc point - eg. "42.3455,-71.10767"
* @param category string (optional) Filters result set based on property type. Valid options are "hotels", "attractions", "restaurants", and "geos"
* @param phone string (optional) Phone number to filter the search results by (this can be in any format with spaces and dashes but without the "+" sign at the beginning)
* @param address string (optional) Address to filter the search results by
* @param radius string (optional) Length of the radius from the provided latitude/longitude pair to filter results.
* @param radiusUnit string (optional) Unit for length of the radius. Valid options are "km", "mi", "m" (km=kilometers, mi=miles, m=meters)
* @param language string (optional) The language in which to return results (e.g. "en" for English or "es" for Spanish) from the list of our Supported Languages.
* @returns
*/
async searchNearby(latLong, category, phone, address, radius, radiusUnit, language) {
let url = `https://api.content.tripadvisor.com/api/v1/location/nearby_search?key=${this.TRIPADVISOR_API_KEY}&latLong=${latLong}`;
if (category) {
url += `&category=${category}`;
}
if (phone) {
url += `&phone=${phone}`;
}
if (address) {
url += `&address=${address}`;
}
if (radius && radiusUnit) {
url += `&radius=${radius}&radiusUnit=${radiusUnit}`;
}
if (language) {
url += `&language=${language}`;
}
console.log("searchNearby", url);
try {
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const result = await response.json();
return result;
}
catch (error) {
console.error(error);
return error;
}
}
/**
* Text(Query) Search
* API Document: https://tripadvisor-content-api.readme.io/reference/searchforlocations
* API URL https://api.content.tripadvisor.com/api/v1/location/search
* @param searchQuery string (required) Text to use for searching based on the name of the location
* @param category string (optional) Filters result set based on property type. Valid options are "hotels", "attractions", "restaurants", and "geos"
* @param phone string (optional) Phone number to filter the search results by (this can be in any format with spaces and dashes but without the "+" sign at the beginning)
* @param address string (optional) Address to filter the search results by
* @param latLong string (optional) Latitude/Longitude pair to scope down the search around a specifc point - eg. "42.3455,-71.10767"
* @param radius number (optional) > 0 Length of the radius from the provided latitude/longitude pair to filter results.
* @param radiusUnit string (optional) Unit for length of the radius. Valid options are "km", "mi", "m" (km=kilometers, mi=miles, m=meters)
* @param language string (optional) The language in which to return results (e.g. "en" for English or "es" for Spanish) from the list of our Supported Languages.
* @returns
*/
async searchByText(searchQuery, category, phone, address, latLong, radius, radiusUnit, language) {
let url = `https://api.content.tripadvisor.com/api/v1/location/search?key=${this.TRIPADVISOR_API_KEY}&searchQuery=${searchQuery}`;
if (category) {
url += `&category=${category}`;
}
if (phone) {
url += `&phone=${phone}`;
}
if (address) {
url += `&address=${address}`;
}
if (latLong) {
url += `&latLong=${latLong}`;
}
if (radius && radiusUnit) {
url += `&radius=${radius}&radiusUnit=${radiusUnit}`;
}
if (language) {
url += `&language=${language}`;
}
console.log("searchByText", url);
try {
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const result = await response.json();
return result;
}
catch (error) {
console.error(error);
return error;
}
}
/**
* Get Location Details
* API Document: https://tripadvisor-content-api.readme.io/reference/getlocationdetails
* API URL https://api.content.tripadvisor.com/api/v1/location/{locationId}/details
* @param locationId string (required) A unique identifier for a location on Tripadvisor. The location ID can be obtained using the Location Search.
* @param language string (optional) The language in which to return results (e.g. "en" for English or "es" for Spanish) from the list of our Supported Languages.
* @param currency string (optional) The currency code to use for request and response (should follow ISO 4217).
* @returns
*/
async getLocationDetails(locationId, language, currency) {
let url = `https://api.content.tripadvisor.com/api/v1/location/${locationId}/details?key=${this.TRIPADVISOR_API_KEY}`;
if (language) {
url += `&language=${language}`;
}
if (currency) {
url += `¤cy=${currency}`;
}
console.log("getLocationDetails", url);
try {
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const result = await response.json();
return result;
}
catch (error) {
console.error(error);
return error;
}
}
/**
* Get Location Photos
* API Document: https://tripadvisor-content-api.readme.io/reference/getlocationphotos
* API URL https://api.content.tripadvisor.com/api/v1/location/{locationId}/photos
* @param locationId string (required) A unique identifier for a location on Tripadvisor. The location ID can be obtained using the Location Search.
* @param language string (optional) The language in which to return results (e.g. "en" for English or "es" for Spanish) from the list of our Supported Languages.
* @param limit number (optional) The number of results to return
* @param offset number (optional) The index of the first result
* @param source string (optional) A comma-separated list of allowed photo sources. Allowed values are 'Expert', 'Management', 'Traveler'. If not specified, allow photos from all sources.
* @returns
*/
async getLocationPhotos(locationId, language, limit, offset, source) {
let url = `https://api.content.tripadvisor.com/api/v1/location/${locationId}/photos?key=${this.TRIPADVISOR_API_KEY}`;
if (language) {
url += `&language=${language}`;
}
if (limit) {
url += `&limit=${limit}`;
}
if (offset) {
url += `&offset=${offset}`;
}
if (source) {
url += `&source=${source}`;
}
console.log("getLocationPhotos", url);
try {
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const result = await response.json();
return result;
}
catch (error) {
console.error(error);
return error;
}
}
/**
* Get Location Reviews
* API Document: https://tripadvisor-content-api.readme.io/reference/getlocationreviews
* API URL https://api.content.tripadvisor.com/api/v1/location/{locationId}/reviews
* @param locationId string (required) A unique identifier for a location on Tripadvisor. The location ID can be obtained using the Location Search.
* @param language string (optional) The language in which to return results (e.g. "en" for English or "es" for Spanish) from the list of our Supported Languages.
* @param limit number (optional) The number of results to return
* @param offset number (optional) The index of the first result
* @returns
*/
async getLocationReviews(locationId, language, limit, offset) {
let url = `https://api.content.tripadvisor.com/api/v1/location/${locationId}/reviews?key=${this.TRIPADVISOR_API_KEY}`;
if (language) {
url += `&language=${language}`;
}
if (limit) {
url += `&limit=${limit}`;
}
if (offset) {
url += `&offset=${offset}`;
}
console.log("getLocationReviews", url);
try {
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const result = await response.json();
return result;
}
catch (error) {
console.error(error);
return error;
}
}
}
//# sourceMappingURL=tripadvisor-api.js.map