This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#27 Add converter to csv and save file
- Loading branch information
Showing
4 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} | ||
} |