https://rp5.ru/%D0%9F%D0%BE%D0%B3%D0%BE%D0%B4%D0%B...
www.realmeteo.ru/moscow/1/history/2017.json
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using ImportData.Code;
using ImportData.Model;
using Newtonsoft.Json;
namespace ImportData.Importer
{
public class RealMediaParser
{
// http://www.realmeteo.ru/moscow/1/history/2017.json
private const string _baseUrl = "http://www.realmeteo.ru";
public string GetJson(string slug)
{
var client = new HttpClient();
var uri = GetHistoricatUri(slug);
var data = client.GetStringAsync(uri);
data.Wait(5000);
return data.Result;
}
private string GetHistoricatUri(string slug)
{
return $"http://www.realmeteo.ru/{slug}/1/history/2017.json";
}
public Record[] ConvertJson2Records(string text)
{
var model = JsonConvert.DeserializeObject<WeatherRecordModel>(text);
model.Interval /= 1000;
model.Start /= 1000;
var start = model.Start;
var i = 0;
var outlist = new List<Record>();
var old = 0;
var hold = 100;
while (start < model.Stop)
{
float h=100,
t = 0;
try
{
if (model.Data.Humidity[i].HasValue)
h = (int)model.Data.Humidity[i].Value;
if (model.Data.Temperature[i].HasValue)
t = (int)model.Data.Temperature[i].Value;
var record = new Record();
record.Date = start.UnixTimestampDateTime();
record.Humidity = h;
if (t < -100)
t = old;
if (h < 0)
h = hold;
record.Temperature = old = (int) t;
record.Humidity = hold = (int)h;
outlist.Add(record);
}
catch (Exception ex)
{
Trace.WriteLine(ex);
}
i++;
start += model.Interval;
}
return outlist.ToArray();
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using ImportData.Code;
namespace ImportData.Importer
{
public class Rp5Parser
{
public Rp5Parser(string[] lines,
int skipLines = 6,
int dateColumn = 0,
int temperatureColumn = 1,
int humidity = 4)
{
SkipLines = skipLines;
DateColumn = dateColumn;
TemperatureColumn = temperatureColumn;
HumidityColumn = humidity;
float tpp = 0, upp = 100;
string[] headers = GetString(lines[skipLines]);
DateName = headers[0];
TemperatureName = headers[1];
HumidityName = headers[2];
var ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
ci.NumberFormat.CurrencyDecimalSeparator = ".";
var records = new List<Record>();
for (var i = SkipLines + 1; i < lines.Length; i++)
{
var line = lines[i];
if (string.IsNullOrWhiteSpace(line)) break;
var fields = GetString(line);
var d = fields[0];
var t = fields[1];
var u = fields[2];
var dp = DateTime.ParseExact(d, "dd.MM.yyyy HH:mm", ci.DateTimeFormat);
float tp, up;
if (!float.TryParse(t, NumberStyles.Any, ci, out tp))
{
tp = tpp;
}
if (!float.TryParse(u, NumberStyles.Any, ci, out up))
{
up = upp;
}
records.Add(new Record() { Date = dp, Temperature = tp, Humidity = up });
tpp = tp;
upp = up;
}
Records = records.ToArray();
}
private string[] GetString(string line)
{
var fields = line.Split(';');
var d = fields[DateColumn].Replace("\"", "");
var t = fields[TemperatureColumn].Replace("\"", "");
var h = fields[HumidityColumn].Replace("\"", "");
return new[] { d, t, h };
}
public int SkipLines { get; set; }
public int DateColumn { get; set; }
public string DateName { get; set; }
public int TemperatureColumn { get; set; }
public string TemperatureName { get; set; }
public Record[] Records { get; set; }
public int HumidityColumn { get; set; }
public string HumidityName { get; set; }
}
}