private static string CreateRequest(DateTime beginDate, DateTime endDate, IEnumerable<WeatherChar> charList, IEnumerable<int> postCodeList, WeatherSource source) { string forecastType = source == WeatherSource.ENS ? "F50" : "F"; StringBuilder objListSb = new StringBuilder(); foreach (var ch in charList) { foreach (var postCode in postCodeList) { objListSb.Append(String.Format(objStrTemplate, source.ToString(), ch.ToString(), postCode.ToString(), forecastType)); objListSb.Append(','); } } objListSb.Length--;//Убираем последнюю запятую return String.Format(requestStrTemplate, beginDate.ToString(dateStrTemplate), endDate.ToString(dateStrTemplate), objListSb.ToString()); }
/// <summary> /// Returns weather data /// </summary> /// <param name="beginDate">Begin date</param> /// <param name="endDate">End date</param> /// <param name="charList">List of weather chars</param> /// <param name="postCodeList">List of weather post codes</param> /// <param name="source">Source for fetch</param> /// <returns></returns> public static IDictionary<WeatherChar, IDictionary<int, IDictionary<DateTime, float?>>> GetWeatherData( DateTime beginDate, DateTime endDate, IEnumerable<WeatherChar> charList, IEnumerable<int> postCodeList, WeatherSource source) { string response = ""; using (WebClient wc = new WebClient()) { string request = CreateRequest(beginDate, endDate, charList, postCodeList, source); response = wc.DownloadString(request); } return ParseResponse(response, beginDate, endDate, charList, postCodeList, source); }
static void Main(string[] args) { if (args.Length > 0) { _folder = args[0]; } if (args.Length > 1) { _type = (WeatherSource)int.Parse(args[1]); } bool nocheck = false; if (args.Length > 2) { nocheck = true; } bool nokill = false; if (args.Length > 3) { nokill = true; } if (!nocheck) { // check if mother app is running Process[] pl = Process.GetProcesses(); bool running = false; foreach (var p in pl) { if (p.ProcessName.Equals("PictureSlideshowScreensaver", StringComparison.OrdinalIgnoreCase)) { running = true; break; } } if (!running) { return; } } // nokill = true; if (!nokill) { string browsername = "chrome"; string drivername = "chromedriver"; if (_type == WeatherSource.NI || _type == WeatherSource.YI) { browsername = "iexplore"; drivername = "IEDriverServer"; } else if (_type == WeatherSource.NE || _type == WeatherSource.YE) { browsername = "edge"; drivername = "msedgeDriver"; } Process[] pl = Process.GetProcesses(); foreach (var p in pl) { string mwt = p.MainWindowTitle; string pn = p.ProcessName; if (p.ProcessName.Equals(browsername, StringComparison.OrdinalIgnoreCase)) { //p.CloseMainWindow(); try { p.Kill(); } catch (Exception) { } } } foreach (var p in pl) { string mwt = p.MainWindowTitle; string pn = p.ProcessName; if (p.ProcessName.Equals(drivername, StringComparison.OrdinalIgnoreCase)) { p.Kill(); } } } IWeatherWriter writer = null; IWeatherReader reader = null; if (_type == WeatherSource.NI || _type == WeatherSource.NC || _type == WeatherSource.NE) { reader = new NGSSeleniumReader(_type); writer = new NGSFileReaderWriter(_type); } else if (_type == WeatherSource.YI || _type == WeatherSource.YC || _type == WeatherSource.YE) { reader = new YandexSeleniumReader(_type); writer = new YandexFileReaderWriter(_type); } string temp = "", current = "", forecast = "", except = ""; try { temp = reader.temperature(); } catch (Exception e) { except += "\n\n\n ======================= \n" + e.Message; } try { current = reader.current(); } catch (Exception e) { except += "\n\n\n ======================= \n" + e.Message; } try { forecast = reader.forecast(); } catch (Exception e) { except += "\n\n\n ======================= \n" + e.Message; } writer.writeinfo(temp, current, forecast, except); reader.close(); }
public YandexFileReaderWriter(WeatherSource type, string folder = null) : base(folder) { _foldername = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); _filename = "yandex_weather_info.txt"; _execparams = $". {(int)type}"; }
internal static void UpDateForecasts(IEnumerable <ForecastFromSource> latestForecastsFromSource, IEnumerable <DailyIcon> dailyIcons) { DateTime fullHours = DateTime.Today.AddHours(DateTime.Now.Hour); try { using (WeatherContext db = new WeatherContext()) { WeatherSource[] source = latestForecastsFromSource.Select(x => x.source).Distinct().ToArray(); foreach (var s in source) { db.WeatherSources.Attach(s); } // Group ratings by the farness of the forecast var result = from f in db.Ratings group f by DbFunctions.DiffHours(f.ForecastDate, f.ForecastFor).HasValue ? DbFunctions.DiffHours(f.ForecastDate, f.ForecastFor).Value : 0 into g//farness select g; result.ToList(); //select best ratings by farness var topOfRatings = (from g in result select new { airTemperature = (from p in g group p.AirTemperature by p.Source into gr select new { val = gr.Sum(), source = gr.Key }).OrderByDescending(x => x.val).FirstOrDefault().source, iconDescription = (from p in g group p.WeatherDescription by p.Source into gr select new { val = gr.Sum(), source = gr.Key }).OrderByDescending(x => x.val).FirstOrDefault().source, pressure = (from p in g group p.Pressure by p.Source into gr select new { val = gr.Sum(), source = gr.Key }).OrderByDescending(x => x.val).FirstOrDefault().source, humidity = (from p in g group p.Humidity by p.Source into gr select new { val = gr.Sum(), source = gr.Key }).OrderByDescending(x => x.val).FirstOrDefault().source, WindSpeed = (from p in g group p.WindSpeed by p.Source into gr select new { val = gr.Sum(), source = gr.Key }).OrderByDescending(x => x.val).FirstOrDefault().source, WindDirection = (from p in g group p.WindDirection by p.Source into gr select new { val = gr.Sum(), source = gr.Key }).OrderByDescending(x => x.val).FirstOrDefault().source, Fallout = (from p in g group p.Fallout by p.Source into gr select new { val = gr.Sum(), source = gr.Key }).OrderByDescending(x => x.val).FirstOrDefault().source, Snow = (from p in g group p.Snow by p.Source into gr select new { val = gr.Sum(), source = gr.Key }).OrderByDescending(x => x.val).FirstOrDefault().source, Cloud = (from p in g group p.Cloud by p.Source into gr select new { val = gr.Sum(), source = gr.Key }).OrderByDescending(x => x.val).FirstOrDefault().source, Farness = g.Key }).AsEnumerable(); // database query ends here, the rest is a query in memory var resultForecast = (from rat in topOfRatings// select forecasts by best ratings select new { rating = rat, forecastse = (from lf in latestForecastsFromSource where fullHours.AddHours(rat.Farness) == lf.date select lf).ToList() }).AsEnumerable(); IEnumerable <WeatherCondition> res = new List <WeatherCondition>(); try { res = from obj in resultForecast where obj.forecastse.Count > 0 select new WeatherCondition { date = fullHours.AddHours(obj.rating.Farness), airTemperature = obj.forecastse.Where(x => x.source == obj.rating.airTemperature).FirstOrDefault().airTemperature, isDay = obj.forecastse.Where(x => x.source == obj.rating.iconDescription).FirstOrDefault().isDay, description = obj.forecastse.Where(x => x.source == obj.rating.iconDescription).FirstOrDefault().description, pressure = obj.forecastse.Where(x => x.source == obj.rating.pressure).FirstOrDefault().pressure, humidity = obj.forecastse.Where(x => x.source == obj.rating.humidity).FirstOrDefault().humidity, windSpeed = obj.forecastse.Where(x => x.source == obj.rating.WindSpeed).FirstOrDefault().windSpeed, windDirection = obj.forecastse.Where(x => x.source == obj.rating.WindDirection).FirstOrDefault().windDirection, rain = obj.forecastse.Where(x => x.source == obj.rating.Fallout).FirstOrDefault().rain, snow = obj.forecastse.Where(x => x.source == obj.rating.Snow).FirstOrDefault().snow, cloud = obj.forecastse.Where(x => x.source == obj.rating.Cloud).FirstOrDefault().cloud }; } catch (Exception ex) { Logger.Log.Error("", ex); } ///////////////////////////////////////////////////////////////////// code for description//////////////////////////////////////////////////// var TopOfDescriptionRating = from g in topOfRatings select new { date = fullHours.AddHours(g.Farness).Date, sourse = g.iconDescription }; var SourcesDescriptionRating = from g in TopOfDescriptionRating group g by g.date into gr select new { date = gr.Key, sourses = gr.GroupBy(x => x.sourse).OrderByDescending(x => x.Count()) }; var SourceDescriptionRating = from g in SourcesDescriptionRating select new { date = g.date, source = g.sourses.FirstOrDefault().Key }; ///////////////////////////////////////////////////////////////////// end code for description//////////////////////////////////////////////////// lock (LatestForecasts) { LatestForecasts.Clear(); // breakdown by day var resGroupByDay = from forecast in res group forecast by forecast.date.Date into forecastOnDay select forecastOnDay; foreach (var forecastsOnDay in resGroupByDay.OrderBy(x => x.Key)) { OneDayForecast f = new OneDayForecast(); f.date = forecastsOnDay.Key; f.astro = Astro.GetAstroByDate(forecastsOnDay.Key); WeatherSource oneDaySource = SourceDescriptionRating.Where(x => x.date == forecastsOnDay.Key).FirstOrDefault().source; f.description = dailyIcons.Where(x => x.forecastFor == forecastsOnDay.Key && x.source == oneDaySource).FirstOrDefault().description; f.Forecasts.AddRange(forecastsOnDay.OrderBy(x => x.date)); LatestForecasts.Add(f); } LatestForecastUpdate = DateTime.Now; } //delete old data from the table OneDayAnalyticForecast end WeatherCondition try { foreach (var forecast in db.OneDayAnalyticForecast.Include("Forecasts")) { db.WeatherConditions.RemoveRange(forecast.Forecasts); db.OneDayAnalyticForecast.Remove(forecast); } db.SaveChanges(); } catch (Exception ex) { Logger.Log.Error("An error occurred while trying to delete OneDayAnalyticForecast", ex); } try { var descriptions = LatestForecasts.Select(f => f.description).Distinct(); var icons = LatestForecasts.Select(f => f.description.Icon).Distinct(); var astros = LatestForecasts.Select(f => f.astro).Distinct(); foreach (var astro in astros) { if (astro != null) { if (db.Entry(astro).State == EntityState.Detached) { db.AstroByDays.Attach(astro); } } } foreach (var forecast in LatestForecasts) { foreach (var condition in forecast.Forecasts) { if (!descriptions.Contains(condition.description)) { if (db.Entry(condition.description).State == EntityState.Detached) { db.WeatherDescriptions.Attach(condition.description); } } if (!icons.Contains(condition.description.Icon)) { if (db.Entry(condition.description.Icon).State == EntityState.Detached) { db.WeatherIcons.Attach(condition.description.Icon); } } } foreach (var descript in descriptions) { if (db.Entry(descript).State == EntityState.Detached) { db.WeatherDescriptions.Attach(descript); } } foreach (var icon in icons) { if (db.Entry(icon).State == EntityState.Detached) { db.WeatherIcons.Attach(icon); } } } foreach (var forecast in LatestForecasts) { db.OneDayAnalyticForecast.Add(forecast); } db.SaveChanges(); } catch (Exception ex) { Logger.Log.Error("An error occurred while trying to save analytic forecasts in AnalyticForecastFactory", ex); } } } catch (Exception ex) { Logger.Log.Error("An error occurred while trying to create analytic forecasts", ex); } }
private static IDictionary<WeatherChar, IDictionary<int, IDictionary<DateTime, float?>>> ParseResponse( string jsonData, DateTime beginDate, DateTime endDate, IEnumerable<WeatherChar> charList, IEnumerable<int> postCodeList, WeatherSource source) { JObject jResponse = JObject.Parse(jsonData); IList<JArray> jData = jResponse.Value<JArray>("data").ToObject<List<JArray>>(); var result = new Dictionary<WeatherChar, IDictionary<int, IDictionary<DateTime, float?>>>(); IDictionary<WeatherChar, IDictionary<int, int>> indexes = GetIndexes(jResponse.Value<JArray>("headers"), charList, postCodeList); foreach (WeatherChar ch in charList) { var chResult = new Dictionary<int, IDictionary<DateTime, float?>>(); foreach (int postCode in postCodeList) { var postResult = new Dictionary<DateTime, float?>(); for (DateTime date = beginDate; date < endDate; date = date.AddDays(1)) { postResult.Add(date, GetValue(jData, date, indexes[ch][postCode])); } chResult.Add(postCode, postResult); } result.Add(ch, chResult); } return result; }
public NGSSeleniumReader(WeatherSource type) : base(type) { _weather_url = "https://pogoda.ngs.ru/academgorodok/"; _weather_forecast_url = _weather_url; }
public WeatherReader(WeatherSource weatherSource) { _weatherSource = weatherSource; }
// for Novosibirsk by default public YandexSeleniumReader(WeatherSource type, double lat = 54.85194397, double lon = 83.10189056) : base(type) { SetLocation(lat, lon); }
public WeatherSeleniumReader(WeatherSource type) { _type = type; _driver = create_driver(); }