Friday, 22 February 2019

Sample code to fetch Trip adviser Rating and Number of Review




Name Space Used 

using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;

Serializable class


    [Serializable]
    public class TripLocation
    {
        [JsonProperty("name")]
        public string POIName { get; set; }

        [JsonProperty("rating")]
        public string POIRating { get; set; }

        [JsonProperty("ratingImageUrl")]
        public string RatingImageUrl_v1 { get; set; }

        [JsonProperty("rating_image_url")]
        public string RatingImageUrl { get; set; }

        [JsonProperty("num_reviews")]
        public string NumberOfReviews { get; set; }

        [JsonProperty("write_review")]
        public string WriteReviewUrl { get; set; }

        [JsonProperty("web_url")]
        public string WebUrl { get; set; }
    }



Code to getting Trip Advisor Rating 


        public static TripLocation  GetTripAdvsiorRating (string locationid)
        {
            TripLocation location = new TripLocation();

            string data = string.Empty;


            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(GetLocationURL(locationid));
            httpWebRequest.Method = "GET";
            httpWebRequest.ContentType = "application/json";

            using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
            {
                using (Stream stream = httpWebResponse.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        data = streamReader.ReadToEnd();
                    }
                }
            }

            location = JsonConvert.DeserializeObject<TripLocation>(data);

            Console.WriteLine(location.NumberOfReviews);
            Console.WriteLine(location.RatingImageUrl);

            return location;
        }

        public static string GetLocationURL(string id)
        {
            const string tripadvisorApiKey = "Your API KEY";
            return String.Format("{0}/{1}/?key={2}", "http://api.tripadvisor.com/api/partner/2.0/location", id, tripadvisorApiKey);
        }



1 comment: