Coverage for mindsdb / integrations / handlers / tripadvisor_handler / tripadvisor_api.py: 0%

73 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1import requests 

2from requests import Response 

3from enum import Enum 

4 

5 

6class TripAdvisorAPICall(Enum): 

7 """TripAdvisor API references""" 

8 

9 SEARCH_LOCATION = 1 

10 LOCATION_DETAILS = 2 

11 PHOTOS = 3 

12 REVIEWS = 4 

13 NEARBY_SEARCH = 5 

14 

15 

16class TripAdvisorAPI: 

17 """A class for checking the connection to the TripAdvisor Content API and making requests. 

18 

19 Attributes: 

20 api_key (str): The unique API key to access Tripadvisor content. 

21 """ 

22 

23 def __init__(self, api_key): 

24 self.api_key = api_key 

25 

26 def checkTripAdvisorConnection(self): 

27 """ 

28 Check the connection with TripAdvisor 

29 """ 

30 url = "https://api.content.tripadvisor.com/api/v1/location/search?language=en&key={api_key}&searchQuery={searchQuery}".format( 

31 api_key=self.api_key, searchQuery="London" 

32 ) 

33 

34 headers = {"accept": "application/json"} 

35 response = requests.get(url, headers=headers) 

36 status_code = response.status_code 

37 

38 if status_code >= 400 and status_code <= 499: 

39 raise Exception("Client error: " + response.text) 

40 

41 if status_code >= 500 and status_code <= 599: 

42 raise Exception("Server error: " + response.text) 

43 

44 def getResponse(self, url: str) -> Response: 

45 """ 

46 Getting a response from the API call 

47 """ 

48 headers = {"accept": "application/json"} 

49 response = requests.get(url, headers=headers) 

50 return response 

51 

52 def getURLQuery(self, url: str, params_dict: dict) -> str: 

53 """ 

54 Processing the query and adding parameters to the URL 

55 """ 

56 for idx, (queryParam, value) in enumerate(params_dict.items()): 

57 if value is not None or value != "": 

58 if value != "" and any( 

59 next_value != "" or value is not None 

60 for next_value in list(params_dict.values())[idx + 1:] 

61 ): 

62 url += "{queryParam}={value}&".format( 

63 queryParam=queryParam, value=value 

64 ) 

65 else: 

66 url += "{queryParam}={value}".format( 

67 queryParam=queryParam, value=value 

68 ) 

69 return url 

70 

71 def location_search( 

72 self, 

73 url: str, 

74 params_dict: dict, 

75 language: str = "en", 

76 ) -> Response: 

77 """ 

78 The Location Search request returns up to 10 locations found by the given search query. You can use category ("hotels", "attractions", "restaurants", "geos"), 

79 phone number, address, and latitude/longtitude to search with more accuracy. 

80 

81 Args: 

82 searchQuery (str): Text to use for searching based on the name of the location. 

83 category (str): Filters result set based on property type. Valid options are "hotels", "attractions", "restaurants", and "geos". 

84 phone (str): 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). 

85 address (str): Address to filter the search results by. 

86 latLong (str): Latitude/Longitude pair to scope down the search around a specifc point - eg. "42.3455,-71.10767". 

87 radius (int): Length of the radius from the provided latitude/longitude pair to filter results. 

88 radiusUnit (str): Unit for length of the radius. Valid options are "km", "mi", "m" (km=kilometers, mi=miles, m=meters. 

89 language (str): The language in which to return results (e.g. "en" for English or "es" for Spanish) from the list of our Supported Languages. 

90 

91 Returns: 

92 response: Response object with response data as application/json 

93 """ 

94 

95 url = url + "search?language={language}&key={api_key}&".format( 

96 api_key=self.api_key, language=language 

97 ) 

98 

99 url = self.getURLQuery(url, params_dict) 

100 response = self.getResponse(url) 

101 

102 return response 

103 

104 def location_details( 

105 self, url: str, params_dict: dict, locationId: str, language: str = "en" 

106 ) -> Response: 

107 """ 

108 A Location Details request returns comprehensive information about a location (hotel, restaurant, or an attraction) such as name, address, rating, and URLs for the listing 

109 on Tripadvisor. 

110 

111 Args: 

112 locationId (str): A unique identifier for a location on Tripadvisor. The location ID can be obtained using the Location Search. 

113 language (str): The language in which to return results (e.g. "en" for English or "es" for Spanish) from the list of our Supported Languages. 

114 currency (str): The currency code to use for request and response (should follow ISO 4217). 

115 

116 Returns: 

117 response (Response): Response object with response data as application/json 

118 """ 

119 url = url + "{locationId}/details?language={language}&key={api_key}&".format( 

120 locationId=locationId, api_key=self.api_key, language=language 

121 ) 

122 url = self.getURLQuery(url, params_dict) 

123 response = self.getResponse(url) 

124 return response 

125 

126 def location_reviews( 

127 self, url: str, locationId: str, language: str = "en" 

128 ) -> Response: 

129 """ 

130 The Location Reviews request returns up to 5 of the most recent reviews for a specific location. Please note that the limits are different for the beta subscribers. 

131 

132 Args: 

133 locationId (str): A unique identifier for a location on Tripadvisor. The location ID can be obtained using the Location Search. 

134 language (str): The language in which to return results (e.g. "en" for English or "es" for Spanish) from the list of our Supported Languages. 

135 

136 Returns: 

137 response: Response object with response data as application/json 

138 """ 

139 url = url + "{locationId}/reviews?language={language}&key={api_key}&".format( 

140 locationId=locationId, api_key=self.api_key, language=language 

141 ) 

142 

143 response = self.getResponse(url) 

144 return response 

145 

146 def location_photos(self, url: str, locationId: str, language: str = "en") -> Response: 

147 """ 

148 The Location Photos request returns up to 5 high-quality photos for a specific location. Please note that the limits are different for the beta subscribers. 

149 You need to upgrade to get the higher limits mentioned here. The photos are ordered by recency. 

150 

151 Args: 

152 locationId (str): A unique identifier for a location on Tripadvisor. The location ID can be obtained using the Location Search. 

153 language (str): The language in which to return results (e.g. "en" for English or "es" for Spanish) from the list of our Supported Languages. 

154 

155 Returns: 

156 response: Response object with response data as application/json 

157 """ 

158 url = url + "{locationId}/photos?language={language}&key={api_key}".format(locationId=locationId, language=language, api_key=self.api_key) 

159 response = self.getResponse(url) 

160 return response 

161 

162 def location_nearby_search(self, url: str, params_dict: dict, language: str = "en") -> Response: 

163 """ 

164 The Nearby Location Search request returns up to 10 locations found near the given latitude/longtitude. 

165 You can use category ("hotels", "attractions", "restaurants", "geos"), phone number, address to search with more accuracy. 

166 

167 Args: 

168 latLong (str): Latitude/Longitude pair to scope down the search around a specifc point - eg. "42.3455,-71.10767". 

169 category (str): Filters result set based on property type. Valid options are "hotels", "attractions", "restaurants", and "geos". 

170 phone (str): 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). 

171 address (str): Address to filter the search results by. 

172 radius (int): Length of the radius from the provided latitude/longitude pair to filter results. 

173 radiusUnit (str): Unit for length of the radius. Valid options are "km", "mi", "m" (km=kilometers, mi=miles, m=meters. 

174 language (str): The language in which to return results (e.g. "en" for English or "es" for Spanish) from the list of our Supported Languages. 

175 

176 Returns: 

177 response: Response object with response data as application/json 

178 """ 

179 

180 url = url + "nearby_search?language={language}&key={api_key}&".format( 

181 api_key=self.api_key, language=language 

182 ) 

183 

184 url = self.getURLQuery(url, params_dict) 

185 response = self.getResponse(url) 

186 return response 

187 

188 def getTripAdvisorData(self, apiCall, **params): 

189 """ 

190 Making a request based on the query and receive data from TripAdvisor. 

191 """ 

192 url = "https://api.content.tripadvisor.com/api/v1/location/" 

193 params_dict = params 

194 

195 if apiCall == TripAdvisorAPICall.SEARCH_LOCATION: 

196 response = self.location_search(url, params_dict) 

197 return response.json()["data"] 

198 

199 elif apiCall == TripAdvisorAPICall.LOCATION_DETAILS: 

200 response = self.location_details( 

201 url, params_dict, params_dict["locationId"] 

202 ) 

203 return response.json() 

204 

205 elif apiCall == TripAdvisorAPICall.REVIEWS: 

206 response = self.location_reviews(url, params_dict["locationId"]) 

207 return response.json()["data"] 

208 

209 elif apiCall == TripAdvisorAPICall.PHOTOS: 

210 response = self.location_photos(url, params_dict["locationId"]) 

211 return response.json()["data"] 

212 

213 elif apiCall == TripAdvisorAPICall.NEARBY_SEARCH: 

214 response = self.location_nearby_search(url, params_dict) 

215 return response.json()["data"]