public async Task SavaApodInDataBase(APOD apod) { APOD check = await _context.APOD.AsNoTracking().FirstOrDefaultAsync(t => t.Date.Equals(apod.Date)); if (check is null && apod.MediaType.Equals("image")) { await _context.APOD.AddAsync(apod); await _context.SaveChangesAsync(); } //remove old ones List <APOD> APODs = await _context.APOD.ToListAsync(); if (APODs.Count <= 7) { return; } for (int i = 0; i < APODs.Count - 10;) { if (APODs[i].MediaType.Equals("image")) { _context.APOD.Remove(APODs[i]); i++; } else { continue; } } await _context.SaveChangesAsync(); }
private async Task getFile(APOD apod) { //TODO catch exceptions using (var file = await client.GetStreamAsync(apod.hdurl).ConfigureAwait(false)) using (var fileStream = File.Create(background)) { await file.CopyToAsync(fileStream); await fileStream.FlushAsync(); } }
async static Task Main(string[] args) { try { //Download the json containing the APOD. HttpResponseMessage response = await client.GetAsync("https://api.nasa.gov/planetary/apod?api_key=bqNHUv2ah1REfaNSpeW934ewvSx4iPCkB4sm9bpX"); response.EnsureSuccessStatusCode(); //Deserialize the JSON string responseBody = await response.Content.ReadAsStringAsync(); APOD apod = JsonConvert.DeserializeObject <APOD>(responseBody); Console.WriteLine("Astronomy Picture Of The Day | Program Created by Weston McNamara\n"); //After displaying that, download the background, and set the desktop background with the new image. using (var client = new WebClient()) { Console.WriteLine("Downloading Background..."); client.DownloadFile(apod.hdurl, "bg.jpg"); SetDesktopBackground(Path.GetFullPath("bg.jpg")); Console.WriteLine("Background Updated!\n"); } //Ouput the title, date, and an image explanation Console.WriteLine(apod.title); Console.WriteLine(apod.date + "\n\n"); Console.WriteLine(apod.explanation + "\n"); } catch (Exception e) { //Console.WriteLine("\nException Caught!"); //Console.WriteLine("Message :{0} ", e.Message); Console.WriteLine("\nThe APOD today is not a format that can be set to a desktop background, such as a video. Please visit the official APOD website to view todays APOD. \n\nhttps://apod.nasa.gov/apod/astropix.html"); } Console.WriteLine("\nPress Any Key To Close"); Console.ReadKey(); }
public async Task GetTodayApodData(string data) { if (data is null) { return; } JObject jObject = JObject.Parse(data); APOD apod = new APOD() { Author = _jsonTools.GetValue <string>(jObject, "copyright"), Date = _jsonTools.GetValue <string>(jObject, "date"), Description = _jsonTools.GetValue <string>(jObject, "explanation"), MediaType = _jsonTools.GetValue <string>(jObject, "media_type"), Title = _jsonTools.GetValue <string>(jObject, "title"), Url = _jsonTools.GetValue <string>(jObject, "url"), UrlHd = _jsonTools.GetValue <string>(jObject, "hdurl") }; await _repository.SavaApodInDataBase(apod); }
public virtual void InsertAPOD(string collectionaName, APOD APODToSave) { var _collection = _database.GetCollection <APOD>(collectionaName); _collection.InsertOne(APODToSave); }