Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

Commit

Permalink
#27 Add converter to csv and save file
Browse files Browse the repository at this point in the history
  • Loading branch information
BoDmiSer committed Dec 6, 2020
1 parent a93175e commit 1f0140b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
27 changes: 27 additions & 0 deletions ImageBase.GrabbingImages/Converter/CSVBase.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
30 changes: 30 additions & 0 deletions ImageBase.GrabbingImages/Dtos/ImageDto.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
4 changes: 2 additions & 2 deletions ImageBase.GrabbingImages/Grabber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public Grabber(IConfiguration configuration)
}
private PexelsClient pexelsClient;

public async Task<PhotoPage> SearchPhotosAsync()
public async Task<PhotoPage> 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;
}
}
Expand Down
56 changes: 52 additions & 4 deletions ImageBase.GrabbingImages/Program.cs
Original file line number Diff line number Diff line change
@@ -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<ImageDto> _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<ImageDto> CreateListImages(PhotoPage photoPage)
{
List<ImageDto> listImageDtos = new List<ImageDto>();

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<ImageDto> imageDtos,string destination)
{
using (var sw = new StreamWriter(destination))
{
foreach (var item in imageDtos)
{
sw.WriteLine(item.ToCsv());
}
}
}
}
}

0 comments on commit 1f0140b

Please sign in to comment.