public void Download(GrabberInfo info, DateTime dt) { DateTime = dt; using WebClient client = new WebClient(); try { Bytes = client.DownloadData(info.ImageUrl); if (Bytes is null || Bytes.Length == 0) { throw new WebException("Image URL contains no data"); } if (Bytes[0] == '<') { throw new WebException("Image URL contains HTML (not an image)"); } using MemoryStream msIn = new MemoryStream(Bytes); using Image originalImage = Bitmap.FromStream(msIn); ImageWidth = originalImage.Width; ImageHeight = originalImage.Height; string timestamp = $"{dt.Year:D2}.{dt.Month:D2}.{dt.Day:D2}.{dt.Hour:D2}.{dt.Minute:D2}.{dt.Second:D2}"; Filename = $"{info.ID} {timestamp} {ImageWidth}x{ImageHeight} " + Path.GetExtension(info.ImageUrl); Hash = GetHash(Bytes); Response = "success"; } catch (WebException ex) { Response = ex.Message; } }
/// <summary> /// Try to parse a CSV line and return a grabber (or null if it cannot be parsed) /// </summary> private static Grabber GrabberFromCsvLine(string line) { line = line.Trim(); if (line.StartsWith("#")) { return(null); } string[] parts = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))").Split(line); parts = parts.Select(s => s.Trim(new char[] { '\'', '"', ' ' })).ToArray(); if (parts.Length != 7) { return(null); } var info = new GrabberInfo() { ID = parts[0], Callsign = parts[1], Title = parts[2], Name = parts[3], Location = parts[4], SiteUrl = parts[5], ImageUrl = parts[6], }; return(new Grabber(info)); }
public static Grabber[] GrabbersFromJson(string json) { const string EXPECTED_VERSION = "1.0"; using JsonDocument document = JsonDocument.Parse(json); string version = document.RootElement.GetProperty("version").GetString(); if (version != EXPECTED_VERSION) { throw new InvalidOperationException("invalid JSON version"); } List <Grabber> grabbers = new List <Grabber>(); foreach (var grabber in document.RootElement.GetProperty("grabbers").EnumerateObject()) { GrabberInfo info = new GrabberInfo() { ID = grabber.Value.GetProperty("id").GetString(), Name = grabber.Value.GetProperty("name").GetString(), Callsign = grabber.Value.GetProperty("callsign").GetString(), Location = grabber.Value.GetProperty("location").GetString(), ImageUrl = grabber.Value.GetProperty("imageUrl").GetString(), SiteUrl = grabber.Value.GetProperty("siteUrl").GetString(), }; GrabberHistory history = new GrabberHistory() { LastUniqueHash = grabber.Value.GetProperty("lastUniqueHash").GetString(), LastUniqueDateTime = grabber.Value.GetProperty("lastUniqueDateTime").GetDateTime(), LastUniqueAgeMinutes = grabber.Value.GetProperty("lastUniqueAgeMinutes").GetInt32(), URLs = grabber.Value.GetProperty("urls").EnumerateArray().Select(x => x.GetString()).ToArray() }; grabbers.Add(new Grabber(info, history)); } return(grabbers.ToArray()); }
public Grabber(GrabberInfo info = null, GrabberHistory history = null) { Info = info ?? new GrabberInfo(); History = history ?? new GrabberHistory(); Data = new GrabberData(); }