Esempio n. 1
0
        /// <summary>
        /// Maps a list of NodeResults potentially more than one hotel and returns a list of hotel objects mapping NodeResult properties to Hotel properties
        /// </summary>
        /// <param name="nodeResults">
        /// List of results from scraping a website
        /// </param>
        /// <param name="args">
        /// Register Args, stores checkin/out data used previously for registering pages with scraper
        /// </param>
        public static List <Hotel> Map(Dictionary <string, List <List <NodeResult> > > nodeResults, RequestArgs args)
        {
            List <Hotel>     hotels = new List <Hotel>();
            List <FieldInfo> fields = typeof(Hotel).GetFields().ToList();
            Dictionary <string, FieldInfo> fieldNames = new Dictionary <string, FieldInfo>();

            fields.ForEach((field) => { fieldNames.Add(field.Name, field); });

            foreach (KeyValuePair <string, List <List <NodeResult> > > nodeList in nodeResults) // Iterate each page
            {
                foreach (List <NodeResult> rawHotel in nodeList.Value)                          // Iterate each raw entry (List of NodeResults which should have all the properties for a Hotel)
                {
                    Hotel hotel = new Hotel();
                    hotel.ReservationData = new HotelReservations();
                    HotelReservation reservation = new HotelReservation();


                    foreach (NodeResult result in rawHotel)
                    {
                        if (fieldNames.ContainsKey(result.Property)) // Map each results property to the classes field
                        {
                            if (result.Attribute != null)
                            {
                                var node = result.Nodes.FirstOrDefault(); // maps attributes for example href to field values
                                if (node?.Attributes[result.Attribute] != null)
                                {
                                    fieldNames[result.Property].SetValue(hotel, node.Attributes[result.Attribute].Value);
                                }
                                else
                                {
                                    fieldNames[result.Property].SetValue(hotel, result.Nodes.First().InnerText.Trim(new char[] { '\r', '\n', '\t', ' ' }));
                                }
                            }
                            else
                            {
                                fieldNames[result.Property].SetValue(hotel, result.Nodes.First().InnerText.Trim(new char[] { '\r', '\n', '\t', ' ' }));
                            }
                        }
                    }

                    Program.App.Log($"[{DateTime.Now.ToShortTimeString()}] {hotel.ScrapeURL}: Found, {hotel.Name}, {hotel.Address}");

                    reservation.Price    = ParseProperty <string>("PriceL", rawHotel);
                    reservation.Currency = ParseProperty <string>("Currency", rawHotel);
                    reservation.CheckIn  = args.CheckIn;
                    reservation.CheckOut = args.CheckOut; // Reservation initialisation and data passing

                    hotel.ReservationData.AddDate(reservation);
                    hotel.DateGathered = DateTime.Now;
                    hotels.Add(hotel);
                }
            }
            return(hotels);
        }
Esempio n. 2
0
        public HotelReservation GetReservation(DateTime checkIn, DateTime checkOut)
        {
            HotelReservation searchReservation = null;

            foreach (HotelReservation reservation in reservations)
            {
                if (reservation.CheckIn.ToShortDateString() == checkIn.ToShortDateString() &&
                    reservation.CheckOut.ToShortDateString() == checkOut.ToShortDateString())
                {
                    searchReservation = reservation;
                    break;
                }
            }
            return(searchReservation);
        }
Esempio n. 3
0
        public HotelReservation GetReservation(int day, int month, int year)
        {
            HotelReservation searchReservation = null;

            foreach (HotelReservation reservation in reservations)
            {
                if ((reservation.CheckIn.Day == day && reservation.CheckIn.Month == month && reservation.CheckIn.Year == year) ||
                    (reservation.CheckOut.Day == day && reservation.CheckOut.Month == month && reservation.CheckOut.Year == year))
                {
                    searchReservation = reservation;
                }
            }

            return(searchReservation);
        }
Esempio n. 4
0
        /// <summary>
        /// Custom contains implementation, checks the data structure to see if any matching checkins are found.
        /// </summary>

        public int Contains(DateTime checkIn, DateTime checkOut)
        {
            int idx = -1;

            for (var i = 0; i < reservations.Count; i++)
            {
                HotelReservation reservation = reservations[i];
                if (reservation.CheckIn.ToShortDateString() == checkIn.ToShortDateString() &&
                    reservation.CheckOut.ToShortDateString() == checkOut.ToShortDateString())
                {
                    idx = i;
                    break;
                }
            }
            return(idx);
        }
Esempio n. 5
0
 public bool AddDate(HotelReservation reservation)
 {
     reservations.Add(reservation);
     return(true);
 }