public static void SendReminder() { List <Voli> Voli = new List <Voli>(); var pattern = "<div style='padding: 5px;font-size:12px;clear:both;'><div style='line-height:15px'><div style='width:60px;float:left;font-weight:bold;padding-right: 10px;'>€ {0}</div> <div style='float:left;width:80px'>{1}</div><div style='float:left; padding-right:10px'><></div><div style='float:left;width:80px;'>{2}</div><div style='float:left;padding-right:10px;'>dal {3} </div> <div style='float:left;padding-right:10px;'>al {4} </div><div style='float:left;width:70px;'>[{6} {7} ]</div><div style='float:left'> -> {5} </div></div></div>"; string body = "<meta charset=\"utf-8\"/><div style=' font-size: 12px; font-family:\"verdana\"; color: #22356b'><h1 style='line-height: 30px; font-size:30px;font-weight:lighter'>Flight Offers Daily</h1><hr/>"; List <Airports> listAirportSelected = Helpers.AirportHelper.GetAirportsFromList(); string MailingList; using (var context = new FodEntities()) { var bestFly = context.Voli.OrderBy(x => x.Price).FirstOrDefault(); if (bestFly != null) { var we = bestFly.Weekend == null ? false : true; body += "<h2 style='line-height: 20px; font-size:18px;font-weight:lighter; border-bottom: 1px solid #eeeee'>Best Fly: € <b>" + bestFly.Price + "</b></h2>"; body += string.Format(pattern, bestFly.Price, bestFly.From, bestFly.To, bestFly.Dal.Value.ToString("dd-MM-yyyy"), bestFly.Al.Value.ToString("dd-MM-yyyy"), bestFly.Vettore, (bool)we ? " WE /" : "", bestFly.Days.ToString()); } body += "<div style='clear:both;'></div>"; var voli = VoloHelper.GetVoli(); if (voli.Count > 0) { body += "<h2 style='line-height: 15px; font-size:18px;font-weight:lighter; border-bottom: 1px solid #eeeee'>Migliori offerte per destinazione</h2>"; foreach (var v in voli) { var we = v.Weekend == null ? false : true; body += string.Format(pattern, v.Price, v.From, v.To, v.Dal.Value.ToString("dd-MM-yyyy"), v.Al.Value.ToString("dd-MM-yyyy"), v.Vettore, (bool)we? " WE /" : "", v.Days.ToString()); } } MailingList = context.Settings.Where(x => x.Key == "MailingList").FirstOrDefault().Value; } var mail = new MailMessage("*****@*****.**", MailingList.ToString(), "Flight Offers Daily", body += "</div>"); mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net"); smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "rugiaint11*!"); smtp.Port = 25; smtp.EnableSsl = false; smtp.Send(mail); using (var context = new FodEntities()) { context.Settings.Where(x => x.Key == "LastMailingData").FirstOrDefault().Value = DateTime.Now.Date.ToShortDateString(); context.SaveChanges(); } }
/// <summary> /// Elimina i voli piu vecchi di oggi /// </summary> public static void ClearOldFly() { using (var context = new FodEntities()) { foreach (var old in context.Voli.Where(x => x.Dal <= DateTime.Now).ToList()) { context.Voli.DeleteObject(old); } context.SaveChanges(); } }
public static void ClearRicerche() { using (var context = new FodEntities()) { foreach (var rv in context.RicercheVolis.Where(x => x.Processato == true).ToList()) { context.DeleteObject(rv); } context.SaveChanges(); } }
public static void PopulateAirports() { List <Settings> settings = SettingsHelper.GetSettings(); var refresh = settings.Where(x => x.Key == "RefreshAirports").FirstOrDefault(); if (refresh != null && Convert.ToBoolean(refresh.Value)) { List <Airports> airportsList = new List <Airports>(); AirportInfo.airportSoapClient ai = new AirportInfo.airportSoapClient(); var countries = settings.Where(x => x.Key == "SearchCountries").FirstOrDefault(); if (countries != null) { foreach (var country in countries.Value.Split(',')) { string airports = ai.GetAirportInformationByCountry(country); XDocument dox = XDocument.Parse(airports); var AirportCodes = dox.Descendants().Where(x => x.Name == "AirportCode").Distinct().ToList(); var AirportNames = dox.Descendants().Where(x => x.Name == "CityOrAirportName").Distinct().ToList(); foreach (var air in AirportCodes) { if (!airportsList.Any(x => x.Id == air.Value)) { var index = AirportCodes.IndexOf(air); airportsList.Add(new Airports { Country = country, Id = air.Value, Name = AirportNames.ElementAt(index).Value }); } } } } using (var context = new FodEntities()) { foreach (var acode in airportsList) { context.Airports.AddObject(acode); } context.SaveChanges(); } } }
public static bool IsSearchEnabled() { var res = false; var datesearch = DateTime.Now.Date; using (var context = new FodEntities()) { if (!context.RicercheVolis.Any(x => x.DataRicerca > datesearch)) { res = true; } if (Convert.ToBoolean(Helpers.SettingsHelper.GetSettings().Where(X => X.Key == "ForceSearch").FirstOrDefault().Value)) { context.Settings.Where(x => x.Key == "ForceSearch").FirstOrDefault().Value = "False"; context.SaveChanges(); res = true; } return(res); } }
public ActionResult Settings(Models.SettingsModel model) { using (var context = new FodEntities()) { context.Settings.Where(x => x.Key == "Airports").FirstOrDefault().Value = model.Airports; context.Settings.Where(x => x.Key == "DateMin").FirstOrDefault().Value = model.DateMin; context.Settings.Where(x => x.Key == "DateRange").FirstOrDefault().Value = model.DateRange; context.Settings.Where(x => x.Key == "ForceSearch").FirstOrDefault().Value = model.ForceSearch; context.Settings.Where(x => x.Key == "MaxTrip").FirstOrDefault().Value = model.MaxTrip; context.Settings.Where(x => x.Key == "MinTrip").FirstOrDefault().Value = model.MinTrip; context.Settings.Where(x => x.Key == "Price").FirstOrDefault().Value = model.Price; context.Settings.Where(x => x.Key == "RefreshAirports").FirstOrDefault().Value = model.RefreshAirports; context.Settings.Where(x => x.Key == "SearchCountries").FirstOrDefault().Value = model.SearchCountries; context.Settings.Where(x => x.Key == "SendDailyReport").FirstOrDefault().Value = model.SendDailyReport; context.Settings.Where(x => x.Key == "StepProcess").FirstOrDefault().Value = model.StepProcess; context.Settings.Where(x => x.Key == "gpl").FirstOrDefault().Value = model.gpl; context.SaveChanges(); } return(RedirectToAction("settings", "home")); }
public static void ComponiRicercaTransavia() { var searchFroms = "http://www.transavia.com/hv/it-IT/home"; WebClient client = new WebClient(); string html = client.DownloadString(searchFroms); //Parse aereoporti var froms = html.Remove(0, html.IndexOf("<div id=\"ffrom\">")); froms = froms.Remove(froms.IndexOf("</select></div>")); froms = froms.Remove(0, froms.IndexOf("<option value=\"ZZZ\"/>")).Replace("<option value=\"ZZZ\"/>", "").Replace("</option>", "§"); var airports = froms.Split('§'); var fromItalianAirports = new List <Airports>(); var toAirports = new List <Airports>(); List <Airports> listAirportSelected = FOD.Helpers.AirportHelper.GetAirportsFromList(); foreach (var a in airports) { if (a.Length >= 15) { var air = a.Substring(15, 3); if (listAirportSelected.Any(x => x.Id == air)) { fromItalianAirports.Add(new Airports { Id = air }); } else { toAirports.Add(new Airports { Id = air }); } } } var searchPattern = "http://www.transavia.com/hv/main/nav/processflightqry?toDay={1}&toMonth={3}&lang=it&adults=1&from={4}&fromMonth={2}&to={5}&country=IT&infants=0&children=0&fromDay={0}&opensearchform=true&tab=cal"; List <RicercheVoli> ParametriRicerca = new List <RicercheVoli>(); List <Settings> settings = SettingsHelper.GetSettings(); string price = settings.Where(x => x.Key == "Price").FirstOrDefault().Value.ToString(); int dateRange = Convert.ToInt32(settings.Where(x => x.Key == "DateRange").FirstOrDefault().Value.ToString()); int minTrip = Convert.ToInt32(settings.Where(x => x.Key == "MinTrip").FirstOrDefault().Value.ToString()); int maxTrip = Convert.ToInt32(settings.Where(x => x.Key == "MaxTrip").FirstOrDefault().Value.ToString()); string dateMin = settings.Where(x => x.Key == "DateMin").FirstOrDefault().Value.ToString(); foreach (var air in fromItalianAirports) { DateTime dateOut = string.IsNullOrEmpty(dateMin) ? DateTime.Now : Convert.ToDateTime(dateMin); DateTime dateOutCalculed; DateTime dateInCalculed; DateTime dateIn = dateOut; string DateInString = string.Empty; string DateOutString = string.Empty; dateIn = dateOut.AddDays(dateRange); int months = dateRange / 30 + 1; foreach (var toDestination in toAirports) { bool noroute = false; for (int i = 0; i < months; i++) { if (!noroute) { dateOutCalculed = dateOut.AddDays(30 * i); dateInCalculed = dateOutCalculed.AddDays(30 * i); DateOutString = dateOutCalculed.ToString("yyyy-MM"); DateInString = dateInCalculed.ToString("yyyy-MM"); string result = string.Empty; bool error = false; string url = string.Format(searchPattern, dateOut.Day.ToString(), dateIn.Day.ToString(), DateOutString, DateInString, air.Id, toDestination.Id); try { result = client.DownloadString(url); } catch (Exception ex) { noroute = true; error = true; } if (!result.Contains("Durante la sua prenotazione si è verificato un errore.") && !error) { ParametriRicerca.Add(new RicercheVoli { Url = url, FromCode = air.Id, Vettore = "Transavia" }); } } } } } using (var context = new FodEntities()) { foreach (var p in ParametriRicerca) { if (!context.RicercheVolis.Any(x => x.Url == p.Url)) { //Verifica Link context.RicercheVolis.AddObject(new RicercheVoli { Url = p.Url, Processato = false, DataRicerca = DateTime.Now, FromCode = p.FromCode, Vettore = p.Vettore }); } } context.SaveChanges(); } }
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) { } }
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(); } }
public static void ComponiRicerca() { string searchPattern = "http://www.ryanair.com/it/voli-economici/?price={0}&limit=150&offset=0&from={1}&to=&out-date-start={2}&out-date-end={3}&in-date-start={4}&in-date-end={5}&roundtrip=1&min-trip={6}&max-trip={7}&view=list"; List <RicercheVoli> ParametriRicerca = new List <RicercheVoli>(); List <Settings> settings = SettingsHelper.GetSettings(); string price = settings.Where(x => x.Key == "Price").FirstOrDefault().Value.ToString(); int dateRange = Convert.ToInt32(settings.Where(x => x.Key == "DateRange").FirstOrDefault().Value.ToString()); int minTrip = Convert.ToInt32(settings.Where(x => x.Key == "MinTrip").FirstOrDefault().Value.ToString()); int maxTrip = Convert.ToInt32(settings.Where(x => x.Key == "MaxTrip").FirstOrDefault().Value.ToString()); string dateMin = settings.Where(x => x.Key == "DateMin").FirstOrDefault().Value.ToString(); List <Airports> listAirportSelected = FOD.Helpers.AirportHelper.GetAirportsFromList(); DateTime dateOut = string.IsNullOrEmpty(dateMin) ? DateTime.Now : Convert.ToDateTime(dateMin); TimeSpan ts = new TimeSpan(dateRange, 0, 0, 0); DateTime dateIn = dateOut.Add(ts); string DateOutString = dateOut.ToString("yyyy-MM-dd"); string DateInString = dateIn.ToString("yyyy-MM-dd"); foreach (var air in listAirportSelected) { //DateTime dateOut = string.IsNullOrEmpty(dateMin) ? DateTime.Now : Convert.ToDateTime(dateMin); //DateTime dateOutCalculed; //DateTime dateInCalculed; //DateTime dateIn = dateOut; //string DateInString = string.Empty; //string DateOutString = string.Empty; ////for (int i = 0; i < dateRange; i++) ////{ //dateOutCalculed = dateOut.AddDays(dateRange); for (int j = minTrip; j < maxTrip; j++) { //dateInCalculed = dateOutCalculed.AddDays(j); //DateOutString = dateOutCalculed.ToString("yyyy-MM-dd"); //DateInString = dateInCalculed.ToString("yyyy-MM-dd"); string url = string.Format(searchPattern, price, air.Id, DateOutString, DateInString, DateOutString, DateInString, j.ToString(), j.ToString()); ParametriRicerca.Add(new RicercheVoli { Url = url, FromCode = air.Id, Vettore = "Ryanair" }); } //} } using (var context = new FodEntities()) { foreach (var p in ParametriRicerca) { if (!context.RicercheVolis.Any(x => x.Url == p.Url)) { context.RicercheVolis.AddObject(new RicercheVoli { Url = p.Url, Processato = false, DataRicerca = DateTime.Now, FromCode = p.FromCode, Vettore = p.Vettore }); } context.SaveChanges(); } } }