From db1fb4c14f4183811cb765396804c543e2e8bf09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=91=D0=BE?= =?UTF-8?q?=D0=B3=D0=B4=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Wed, 9 Dec 2020 18:52:52 +0300 Subject: [PATCH] #27 Changed grabbing setup method and added console parameters --- ImageBase.GrabbingImages/Converter/CSVBase.cs | 27 ---------- ImageBase.GrabbingImages/Dtos/ImageDto.cs | 5 +- ImageBase.GrabbingImages/Grabber.cs | 4 +- .../ImageBase.GrabbingImages.csproj | 1 + ImageBase.GrabbingImages/Program.cs | 54 +++++++++++++++---- .../Properties/launchSettings.json | 7 +++ README.md | 27 +++++++--- 7 files changed, 76 insertions(+), 49 deletions(-) delete mode 100644 ImageBase.GrabbingImages/Converter/CSVBase.cs create mode 100644 ImageBase.GrabbingImages/Properties/launchSettings.json diff --git a/ImageBase.GrabbingImages/Converter/CSVBase.cs b/ImageBase.GrabbingImages/Converter/CSVBase.cs deleted file mode 100644 index 1ce8963..0000000 --- a/ImageBase.GrabbingImages/Converter/CSVBase.cs +++ /dev/null @@ -1,27 +0,0 @@ -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 index 47952fb..13757ad 100644 --- a/ImageBase.GrabbingImages/Dtos/ImageDto.cs +++ b/ImageBase.GrabbingImages/Dtos/ImageDto.cs @@ -1,11 +1,10 @@ -using ImageBase.GrabbingImages.Converter; -using System; +using System; using System.Collections.Generic; using System.Text; namespace ImageBase.GrabbingImages.Dtos { - public class ImageDto : CSVBase + public class ImageDto { public int id { get; set; } diff --git a/ImageBase.GrabbingImages/Grabber.cs b/ImageBase.GrabbingImages/Grabber.cs index 242476c..ad6f232 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(string tema = "Nature", int pagestart=1,int count = 1) + public async Task SearchPhotosAsync(string theme = "Nature", int pagestart=1,int count = 1) { - PhotoPage photoPage = await pexelsClient.SearchPhotosAsync(tema, "ru-RU", pagestart, count); + PhotoPage photoPage = await pexelsClient.SearchPhotosAsync(theme, "ru-RU", pagestart, count); return photoPage; } } diff --git a/ImageBase.GrabbingImages/ImageBase.GrabbingImages.csproj b/ImageBase.GrabbingImages/ImageBase.GrabbingImages.csproj index 8e97f80..a96823b 100644 --- a/ImageBase.GrabbingImages/ImageBase.GrabbingImages.csproj +++ b/ImageBase.GrabbingImages/ImageBase.GrabbingImages.csproj @@ -9,6 +9,7 @@ + diff --git a/ImageBase.GrabbingImages/Program.cs b/ImageBase.GrabbingImages/Program.cs index 64b14fc..54a5874 100644 --- a/ImageBase.GrabbingImages/Program.cs +++ b/ImageBase.GrabbingImages/Program.cs @@ -4,21 +4,60 @@ using System.Threading.Tasks; using System.Collections.Generic; using System.IO; +using ServiceStack.Text; +using System; namespace ImageBase.GrabbingImages { public class Program { private static List _listImageDtos; - private static string KeyWord; + static async Task Main(string[] args) + { + if (args.Length == 0) + { + await GrabbingFromPexels(); + } + else + { + switch (args.Length) + { + case 1: + if (args[0] == "Pexels") + { + await GrabbingFromPexels(); + } + break; + case 2: + if (args[0] == "Pexels") + { + await GrabbingFromPexels(args[1]); + } + break; + case 3: + if (args[0] == "Pexels") + { + await GrabbingFromPexels(args[1],Convert.ToInt32(args[2])); + } + break; + case 4: + if (args[0] == "Pexels") + { + await GrabbingFromPexels(args[1], Convert.ToInt32(args[2]),args[3]); + } + break; + default: + break; + } + } + } + static async Task GrabbingFromPexels(string theme="Nature",int countImages=5,string outputfile= "AllImages.csv") { Grabber grabber = InicializeGrabber(); - KeyWord = "Nature"; - PhotoPage photoPage = await grabber.SearchPhotosAsync(KeyWord, 1,5); - + PhotoPage photoPage = await grabber.SearchPhotosAsync(theme, 1, countImages); _listImageDtos = CreateListImages(photoPage); - ConvertToCSVAndSaveInFile(_listImageDtos,"AllImages.csv"); + ConvertToCSVAndSaveInFile(_listImageDtos, outputfile); } static Grabber InicializeGrabber() { @@ -56,10 +95,7 @@ static void ConvertToCSVAndSaveInFile(List imageDtos,string destinatio { using (var sw = new StreamWriter(destination)) { - foreach (var item in imageDtos) - { - sw.WriteLine(item.ToCsv()); - } + sw.WriteCsv(imageDtos); } } } diff --git a/ImageBase.GrabbingImages/Properties/launchSettings.json b/ImageBase.GrabbingImages/Properties/launchSettings.json new file mode 100644 index 0000000..361b690 --- /dev/null +++ b/ImageBase.GrabbingImages/Properties/launchSettings.json @@ -0,0 +1,7 @@ +{ + "profiles": { + "ImageBase.GrabbingImages": { + "commandName": "Project" + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index 7b10e61..6b9a539 100644 --- a/README.md +++ b/README.md @@ -31,15 +31,26 @@ _ImageBase.ImageHash_ - this project calculates hashes images _ImageBase.WebApp_ - this project contains rest API requests to manage your images collection ## Usage ImageBase.GrabbingImages + +If you are using the Pexels open service set parameters then you need to enter the command line arguments. + For example + +```bash +ImageBase.GrabbingImages.exe Pexels Sun 3 Out.csv +``` +It configures parameters for you request +1) Pexels - name services +2) Sun - chosen name topic +3) 3 - count required images +4) Out.csv - name of the output file with csv data ```ccharp -static async Task Main(string[] args) -{ - Grabber grabber = InicializeGrabber(); - KeyWord = "Nature"; - PhotoPage photoPage = await grabber.SearchPhotosAsync(KeyWord, 1,5); //Set 3 parameters: tema, start page and count image - _listImageDtos = CreateListImages(photoPage); - ConvertToCSVAndSaveInFile(_listImageDtos,"AllImages.csv"); //Set 2 parameters: list with your images and output saving file -} +static async Task GrabbingFromPexels(string theme="Nature",int countImages=5,string outputfile= "AllImages.csv") + { + Grabber grabber = InicializeGrabber(); + PhotoPage photoPage = await grabber.SearchPhotosAsync(theme, 1, countImages); + _listImageDtos = CreateListImages(photoPage); + ConvertToCSVAndSaveInFile(_listImageDtos, outputfile); + } ``` ## Contributing Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.