Exemplo n.º 1
0
        public static void ProcessaRicercheTransavia(List <RicercheVoli> queries)
        {
            try
            {
                List <Voli> Voli   = new List <Voli>();
                WebClient   client = new WebClient();
                foreach (var url in queries)
                {
                    try
                    {
                        string html = client.DownloadString(url.Url);

                        //Recupero From -> To
                        string htmlDa = html.Remove(0, html.IndexOf("<div id=\"toflight"));
                        htmlDa = htmlDa.Remove(htmlDa.IndexOf("<div class=\"right\">"));
                        htmlDa = htmlDa.Remove(0, htmlDa.LastIndexOf("alt="));
                        htmlDa = System.Text.RegularExpressions.Regex.Replace(htmlDa, "<[^>]*>", " ");
                        htmlDa = System.Text.RegularExpressions.Regex.Replace(htmlDa, @"\s+", " ");
                        htmlDa = htmlDa.Remove(0, htmlDa.IndexOf("\"") + 1);
                        htmlDa = htmlDa.Remove(htmlDa.IndexOf("\""));
                        string fromAirport = htmlDa.Split('-')[0].Trim();
                        string toAirport   = htmlDa.Split('-')[1].Trim();

                        //Recupero Calendar From
                        var htmlCalendar = html.Remove(0, html.IndexOf("<table class=\"calenderview\">"));
                        htmlCalendar = htmlCalendar.Remove(htmlCalendar.IndexOf("</table>"));
                        var         calElements = htmlCalendar.Replace("<label for=", "§").Split('§');
                        List <Voli> Andate      = new List <Voli>();
                        List <Voli> Ritorni     = new List <Voli>();
                        foreach (var e in calElements)
                        {
                            if (!e.Contains("calenderview"))
                            {
                                var price = e.Remove(0, e.IndexOf("€Â")).Replace("€Â", "").Trim();
                                price = price.Remove(price.IndexOf("</span>")).Replace("</span>", "");
                                Andate.Add(new Voli
                                {
                                    From           = fromAirport,
                                    To             = toAirport,
                                    Price          = Convert.ToDouble(price),
                                    Dal            = Convert.ToDateTime(e.Substring(1, 10)),
                                    DataRicerca    = DateTime.Now,
                                    IsPriceChanged = false,
                                    FromCode       = url.FromCode,
                                    Vettore        = url.Vettore
                                });
                            }
                        }



                        //Recupero Calendar To
                        var htmlCalendarTo = html.Remove(0, html.LastIndexOf("<table class=\"calenderview\">"));
                        htmlCalendarTo = htmlCalendarTo.Remove(htmlCalendarTo.IndexOf("</table>"));
                        var calElementsTo = htmlCalendarTo.Replace("<label for=", "§").Split('§');

                        foreach (var e in calElementsTo)
                        {
                            if (!e.Contains("calenderview"))
                            {
                                var price = e.Remove(0, e.IndexOf("€Â")).Replace("€Â", "").Trim();
                                price = price.Remove(price.IndexOf("</span>")).Replace("</span>", "");
                                Ritorni.Add(new Voli
                                {
                                    From           = fromAirport,
                                    To             = toAirport,
                                    Price          = Convert.ToDouble(price),
                                    Al             = Convert.ToDateTime(e.Substring(1, 10)),
                                    DataRicerca    = DateTime.Now,
                                    IsPriceChanged = false,
                                    FromCode       = url.FromCode,
                                    Vettore        = url.Vettore
                                });
                            }
                        }



                        foreach (var a in Andate)
                        {
                            foreach (var r in Ritorni)
                            {
                                if (r.Al > a.Dal)
                                {
                                    Voli.Add(new Voli
                                    {
                                        From           = a.From,
                                        To             = a.To,
                                        DataRicerca    = a.DataRicerca,
                                        IsPriceChanged = false,
                                        FromCode       = a.FromCode,
                                        Vettore        = a.Vettore,
                                        Dal            = a.Dal,
                                        Al             = r.Al,
                                        Price          = a.Price + r.Price
                                    });
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }

                using (var context = new FodEntities())
                {
                    int price = Convert.ToInt32(context.Settings.Where(x => x.Key == "Price").FirstOrDefault());
                    foreach (var q in queries)
                    {
                        context.RicercheVolis.Where(x => x.Id == q.Id).FirstOrDefault().Processato = true;
                    }

                    foreach (var v in Voli)
                    {
                        var old = context.Voli.Where(x => x.Dal == v.Dal && x.Al == v.Al && x.From == v.From && x.To == v.To).FirstOrDefault();
                        if (old != null)
                        {
                            if (v.Price > old.Price)
                            {
                                v.IsPriceChanged = true;
                                v.OldPrice       = old.Price;
                            }
                        }
                        else
                        {
                            if (v.Price <= price)
                            {
                                context.AddToVoli(v);
                            }
                        }
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
            }
        }
Exemplo n.º 2
0
        public static void ProcessaRicerche(List <RicercheVoli> queries)
        {
            List <Voli> Voli = new List <Voli>();

            foreach (var url in queries)
            {
                if (url.Vettore == "Ryanair")
                {
                    WebClient client = new WebClient();
                    //url.Url = "http://www.ryanair.com/it/voli-economici/?price=80&limit=500&offset=0&from=BLQ&to=&out-date-start=2014-09-24&out-date-end=2015-05-22&in-date-start=2014-09-24&in-date-end=2015-05-22&roundtrip=1&min-trip=3&max-trip=3&view=list";
                    string html       = client.DownloadString(url.Url);
                    string htmloffers = html.Remove(0, html.IndexOf("<div data-view=\"list\" class=\"view view-page list list-fares float-l pn-r\" id=\"list-fares\">"));
                    htmloffers = htmloffers.Remove(htmloffers.IndexOf("<script type=\"text/javascript\">"));
                    htmloffers = htmloffers.Replace("<a title=", "§");
                    string[] rows = htmloffers.Split('§');


                    foreach (string s in rows)
                    {
                        if (s.Contains("no-results"))
                        {
                            continue;
                        }
                        if (s.Contains("<span class=\"airport d-inblock float-l fs16\">"))
                        {
                            string offertRow = s.Remove(0, s.IndexOf(">") + 1);
                            offertRow = System.Text.RegularExpressions.Regex.Replace(offertRow, "<[^>]*>", " ");
                            offertRow = System.Text.RegularExpressions.Regex.Replace(offertRow, @"\s+", " ");
                            string[] elements = offertRow.Split(' ');
                            string   dateIn   = s.Remove(0, s.IndexOf("in-date=") + "in-date=".Length);
                            dateIn = dateIn.Remove(dateIn.IndexOf("&out-date="));
                            string dateOut = s.Remove(0, s.IndexOf("&out-date=") + "&out-date=".Length);
                            dateOut = dateOut.Remove(dateOut.IndexOf(">") - 1);

                            Voli.Add(new Voli
                            {
                                From           = elements[1],
                                To             = elements[2].Contains("(")? elements[3] : elements[2],
                                Price          = searchPrice(elements),
                                Dal            = Convert.ToDateTime(dateOut),
                                Al             = Convert.ToDateTime(dateIn),
                                DataRicerca    = DateTime.Now,
                                IsPriceChanged = false,
                                FromCode       = url.FromCode,
                                Vettore        = url.Vettore,
                                Star           = false
                            });
                        }
                    }
                }
            }

            using (var context = new FodEntities())
            {
                foreach (var q in queries)
                {
                    context.RicercheVolis.Where(x => x.Id == q.Id).FirstOrDefault().Processato = true;
                }

                foreach (var v in Voli)
                {
                    var old = context.Voli.Where(x => x.Dal == v.Dal && x.Al == v.Al && x.From == v.From && x.To == v.To).FirstOrDefault();
                    if (old != null)
                    {
                        if (v.Price > old.Price)
                        {
                            v.IsPriceChanged = true;
                            v.OldPrice       = old.Price;
                        }
                    }
                    else
                    {
                        context.AddToVoli(v);
                    }


                    if (!context.Cities.Any(x => x.Name.ToLower() == v.From.ToLower()))
                    {
                        context.Cities.AddObject(new City
                        {
                            Name    = v.From,
                            Visible = true,
                            Type    = 0
                        });
                    }
                    if (!context.Cities.Any(x => x.Name.ToLower() == v.To.ToLower()))
                    {
                        context.Cities.AddObject(new City
                        {
                            Name    = v.To,
                            Visible = true,
                            Type    = 0
                        });
                    }
                }
                context.SaveChanges();
            }
        }