diff --git a/ImageBase.GrabbingImages/Converter/CSVBase.cs b/ImageBase.GrabbingImages/Converter/CSVBase.cs new file mode 100644 index 0000000..1ce8963 --- /dev/null +++ b/ImageBase.GrabbingImages/Converter/CSVBase.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ImageBase.GrabbingImages.Converter +{ + public abstract class CSVBase + { + public virtual string ToCsv() + { + string output = ""; + + var properties = GetType().GetProperties(); + + for (var i = 0; i < properties.Length; i++) + { + output += properties[i].GetValue(this).ToString(); + if (i != properties.Length - 1) + { + output += ","; + } + } + + return output; + } + } +} diff --git a/ImageBase.GrabbingImages/Dtos/ImageDto.cs b/ImageBase.GrabbingImages/Dtos/ImageDto.cs new file mode 100644 index 0000000..47952fb --- /dev/null +++ b/ImageBase.GrabbingImages/Dtos/ImageDto.cs @@ -0,0 +1,30 @@ +using ImageBase.GrabbingImages.Converter; +using System; +using System.Collections.Generic; +using System.Text; + +namespace ImageBase.GrabbingImages.Dtos +{ + public class ImageDto : CSVBase + { + public int id { get; set; } + + public int width { get; set; } + + public int height { get; set; } + + public string url { get; set; } + + public string photographer { get; set; } + + public string original { get; set; } + + public string large { get; set; } + + public string medium { get; set; } + + public string small { get; set; } + + public string landscape { get; set; } + } +} diff --git a/ImageBase.GrabbingImages/Grabber.cs b/ImageBase.GrabbingImages/Grabber.cs index 7e084ac..242476c 100644 --- a/ImageBase.GrabbingImages/Grabber.cs +++ b/ImageBase.GrabbingImages/Grabber.cs @@ -18,9 +18,9 @@ public Grabber(IConfiguration configuration) } private PexelsClient pexelsClient; - public async Task SearchPhotosAsync() + public async Task SearchPhotosAsync(string tema = "Nature", int pagestart=1,int count = 1) { - PhotoPage photoPage = await pexelsClient.SearchPhotosAsync("Nature","ru-RU",1,5); + PhotoPage photoPage = await pexelsClient.SearchPhotosAsync(tema, "ru-RU", pagestart, count); return photoPage; } } diff --git a/ImageBase.GrabbingImages/Program.cs b/ImageBase.GrabbingImages/Program.cs index 295486b..64b14fc 100644 --- a/ImageBase.GrabbingImages/Program.cs +++ b/ImageBase.GrabbingImages/Program.cs @@ -1,18 +1,66 @@ using Microsoft.Extensions.Configuration; using PexelsDotNetSDK.Models; -using System; +using ImageBase.GrabbingImages.Dtos; +using System.Threading.Tasks; +using System.Collections.Generic; +using System.IO; namespace ImageBase.GrabbingImages { - class Program + public class Program { - static async System.Threading.Tasks.Task Main(string[] args) + private static List _listImageDtos; + private static string KeyWord; + static async Task Main(string[] args) + { + Grabber grabber = InicializeGrabber(); + KeyWord = "Nature"; + PhotoPage photoPage = await grabber.SearchPhotosAsync(KeyWord, 1,5); + + _listImageDtos = CreateListImages(photoPage); + ConvertToCSVAndSaveInFile(_listImageDtos,"AllImages.csv"); + } + static Grabber InicializeGrabber() { IConfiguration config = new ConfigurationBuilder() .AddUserSecrets(typeof(Program).Assembly) .Build(); Grabber grabber = new Grabber(config); - PhotoPage photoPage = await grabber.SearchPhotosAsync(); + return grabber; + } + static List CreateListImages(PhotoPage photoPage) + { + List listImageDtos = new List(); + + for (int i = 0; i < photoPage.photos.Count; i++) + { + ImageDto image = new ImageDto() + { + id = photoPage.photos[i].id, + width = photoPage.photos[i].width, + height = photoPage.photos[i].height, + url = photoPage.photos[i].url, + photographer = photoPage.photos[i].photographer, + original = photoPage.photos[i].source.original, + large = photoPage.photos[i].source.large, + medium = photoPage.photos[i].source.medium, + small = photoPage.photos[i].source.small, + landscape = photoPage.photos[i].source.landscape + }; + listImageDtos.Add(image); + } + return listImageDtos; + } + + static void ConvertToCSVAndSaveInFile(List imageDtos,string destination) + { + using (var sw = new StreamWriter(destination)) + { + foreach (var item in imageDtos) + { + sw.WriteLine(item.ToCsv()); + } + } } } }