/// <summary> /// Write files to disk by downloading from NASA API. /// </summary> /// <param name="roverPhotos"></param> private void WriteFilesToDisk(RoverPhotosResponse roverPhotos) { // Setup directory structure CreateMissingDirectories(roverPhotos); using (var client = new WebClient()) { foreach (var roverPhoto in roverPhotos.photos) { var roverFileFolder = GetRoverPhotoFolderName(roverPhoto); var downloadUrl = roverPhoto.img_src + "?api_key=" + apiKey; var localFileName = System.IO.Path.GetFileName(roverPhoto.img_src); var localFileFullPath = roverFileFolder + localFileName; if (!File.Exists(localFileFullPath)) { client.DownloadFile(downloadUrl, localFileFullPath); roverPhoto.isDownloaded = true; } else { roverPhoto.isExists = true; } } } }
/// <summary> /// Build directory stucture. This is done before image files are saved. /// </summary> /// <param name="roverPhotos"></param> private void CreateMissingDirectories(RoverPhotosResponse roverPhotos) { foreach (var roverPhoto in roverPhotos.photos) { var roverFileFolder = GetRoverPhotoFolderName(roverPhoto); if (!System.IO.Directory.Exists(roverFileFolder)) { System.IO.Directory.CreateDirectory(roverFileFolder); } } }
/// <summary> /// Download metadata for a rover for a specified date. /// </summary> /// <param name="rover"></param> /// <param name="formattedDate"></param> /// <returns></returns> private RoverPhotosResponse DownloadRoverDataForDate(string rover, string formattedDate) { var url = apiUrl + $"/mars-photos/api/v1/rovers/{rover}/photos?earth_date={formattedDate}&api_key={apiKey}"; var t = Task.Run(() => RestHelper.ProcessGetRequestAsync(url)); t.Wait(); RestResponse webResponse = t.Result; RoverPhotosResponse roverPhotos = JsonConvert.DeserializeObject <RoverPhotosResponse>(webResponse.data); return(roverPhotos); }