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

Commit

Permalink
Merge pull request #31 from fornit1917/27-Grab-images-from-free-Stock
Browse files Browse the repository at this point in the history
#27 grab images from free stock
  • Loading branch information
fornit1917 authored Dec 17, 2020
2 parents 469c450 + 450744b commit b6ac7e7
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 2 deletions.
33 changes: 33 additions & 0 deletions ImageBase.GrabbingImages/Converters/CSVConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using ImageBase.GrabbingImages.Dtos;
using ServiceStack;
using ServiceStack.Text;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace ImageBase.GrabbingImages.Converters
{
public class CSVConverter : IConvertToSave
{
public StreamWriter CreateStreamWriter(string OutputFile)
{
StreamWr = new StreamWriter(OutputFile, true);
return StreamWr;
}
public static string OutputFile { get; set; }
private static StreamWriter StreamWr { get; set; }

public void SaveToFile(ImageDto imageDto)
{
StreamWr.WriteCsv(imageDto.InList());
}

public void Dispose()
{
StreamWr?.Dispose();
}

}
}
12 changes: 12 additions & 0 deletions ImageBase.GrabbingImages/Converters/IConvertToSave.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using ImageBase.GrabbingImages.Dtos;
using System;
using System.IO;

namespace ImageBase.GrabbingImages.Converters
{
public interface IConvertToSave: IDisposable
{
public void SaveToFile(ImageDto imageDto);
public StreamWriter CreateStreamWriter(string outputfile);
}
}
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.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;

namespace ImageBase.GrabbingImages.Dtos
{
public class ImageDto
{
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; }
}
}
15 changes: 15 additions & 0 deletions ImageBase.GrabbingImages/ImageBase.GrabbingImages.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>c986862e-528e-4391-9ff1-fbc6cc7c06aa</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.1.0" />
<PackageReference Include="PexelsDotNetSDK" Version="1.0.5" />
<PackageReference Include="ServiceStack.Text" Version="5.10.2" />
</ItemGroup>

</Project>
65 changes: 65 additions & 0 deletions ImageBase.GrabbingImages/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Microsoft.Extensions.Configuration;
using PexelsDotNetSDK.Models;
using ImageBase.GrabbingImages.Dtos;
using ImageBase.GrabbingImages.Services.Implementations;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using ServiceStack.Text;
using System;
using ImageBase.GrabbingImages.Services.Interfaces;
using ImageBase.GrabbingImages.Converters;

namespace ImageBase.GrabbingImages
{
public class Program
{
static async Task Main(string[] args)
{
if (args.Length == 0)
{
await CreateFactoryAsync(new GrabberFactory());
}
else
{
switch (args.Length)
{
case 1:
if (args[0] == "Pexels")
{
await CreateFactoryAsync(new GrabberFactory());
}
break;
case 2:
if (args[0] == "Pexels")
{
await CreateFactoryAsync(new GrabberFactory(),args[1]);
}
break;
case 3:
if (args[0] == "Pexels")
{
await CreateFactoryAsync(new GrabberFactory(), args[1],Convert.ToInt32(args[2]));
}
break;
case 4:
if (args[0] == "Pexels")
{
await CreateFactoryAsync(new GrabberFactory(), args[1], Convert.ToInt32(args[2]),args[3]);
}
break;
default:
break;
}
}
}

public static async Task CreateFactoryAsync(IGrabberFactory factory, string theme = "Nature", int countImages = 2, string outputfile = "AllImages.csv")
{
var pexelsgrabber = factory.CreatePexelsGrabber();
pexelsgrabber.InitializeConverterStream(new CSVConverter(),outputfile);
await pexelsgrabber.SearchPhotosAsync(theme,1,countImages);
pexelsgrabber.DisposeStream();
}
}
}
7 changes: 7 additions & 0 deletions ImageBase.GrabbingImages/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"ImageBase.GrabbingImages": {
"commandName": "Project"
}
}
}
7 changes: 7 additions & 0 deletions ImageBase.GrabbingImages/Properties/serviceDependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"secrets1": {
"type": "secrets"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"secrets1": {
"type": "secrets.user"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using ImageBase.GrabbingImages.Dtos;
using ImageBase.GrabbingImages.Services.Interfaces;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace ImageBase.GrabbingImages.Services.Implementations
{
public class GrabberFactory : IGrabberFactory
{
public GrabberFactory()
{
IConfiguration config = new ConfigurationBuilder()
.AddUserSecrets(typeof(Program).Assembly)
.Build();
Configuration = config;
}
public IConfiguration Configuration { get; }
public IGrabber CreatePexelsGrabber()
{
return new PexelsGrabber(Configuration);
}
}
}
61 changes: 61 additions & 0 deletions ImageBase.GrabbingImages/Services/Implementations/PexelsGrabber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using ImageBase.GrabbingImages.Converters;
using ImageBase.GrabbingImages.Dtos;
using ImageBase.GrabbingImages.Services.Interfaces;
using Microsoft.Extensions.Configuration;
using PexelsDotNetSDK.Api;
using PexelsDotNetSDK.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ImageBase.GrabbingImages.Services.Implementations
{
public class PexelsGrabber : IGrabber
{
public IConfiguration Configuration { get; }
private IConvertToSave Converter { get; set; }
public PexelsGrabber(IConfiguration configuration)
{
Configuration = configuration;
pexelsClient = new PexelsClient(Configuration.GetConnectionString("MyAPIKey"));
}
private PexelsClient pexelsClient;

public async Task SearchPhotosAsync(string theme, int pagestart, int count)
{
PhotoPage photoPage = await pexelsClient.SearchPhotosAsync(theme, "ru-RU", pagestart, count);
CreateListImages(photoPage);
}
private void CreateListImages(PhotoPage photoPage)
{
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
};
Converter.SaveToFile(image);
}
}
public void InitializeConverterStream(IConvertToSave converter, string outputfile)
{
Converter = converter;
Converter.CreateStreamWriter(outputfile);
}
public void DisposeStream()
{
Converter?.Dispose();

}
}
}
17 changes: 17 additions & 0 deletions ImageBase.GrabbingImages/Services/Interfaces/IGrabber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ImageBase.GrabbingImages.Converters;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ImageBase.GrabbingImages.Services.Interfaces
{
public interface IGrabber
{
public IConfiguration Configuration { get; }
public Task SearchPhotosAsync(string theme, int pagestart, int count);
public void InitializeConverterStream(IConvertToSave convertToSave,string outputfile);
public void DisposeStream();
}
}
13 changes: 13 additions & 0 deletions ImageBase.GrabbingImages/Services/Interfaces/IGrabberFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace ImageBase.GrabbingImages.Services.Interfaces
{
public interface IGrabberFactory
{
public IConfiguration Configuration { get; }
public IGrabber CreatePexelsGrabber();
}
}
63 changes: 61 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,61 @@
# imagebase
Web app for storing and searching images
# ImageBase

ImageBase is a .NET project that wrote with C# for managing your images collection and compared it to other images. This will allow you to check the image for originality.

## Installation

Use the link [https](https://github.com/fornit1917/imagebase.git) to install ImageBase.
Write code to create repository clone

```bash
gh repo clone fornit1917/imagebase
```
## Structure project
```bash
├── imageBase
│ ├── ImageBase.Common
│ ├── ImageBase.GrabbingImages
│ ├── ImageBase.HashBase
│ ├── ImageBase.ImageHash
│ ├── ImageBase.WebApp
│ ├── ImageBase.Common.UnitTests
│ ├── ImageBase.HashBase.UnitTests
│ ├── ImageBase.ImageHash.Tests
│ └── ImageBase.WebApp.UnitTests
└──
```
_ImageBase.Common_ - this project contains calculate Hamming distance for image
_ImageBase.GrabbingImages_ - This project contains populating your database with free Pexels images
_ImageBase.HashBase_ - this project contains generate VP Tree and compares hashes images
_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 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.

Please make sure to update tests as appropriate.

## License
[GPL-3.0 License](https://github.com/fornit1917/imagebase/blob/main/LICENSE)
6 changes: 6 additions & 0 deletions imagebase.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageBase.ImageHash.UnitTes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageBase.HashBase.UnitTests", "ImageBase.HashBase.UnitTests\ImageBase.HashBase.UnitTests.csproj", "{34EC7522-24B2-4206-B3C1-DA33B9485F2B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageBase.GrabbingImages", "ImageBase.GrabbingImages\ImageBase.GrabbingImages.csproj", "{175BA793-13FE-4320-9A16-24FAC844C771}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,6 +59,10 @@ Global
{34EC7522-24B2-4206-B3C1-DA33B9485F2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34EC7522-24B2-4206-B3C1-DA33B9485F2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34EC7522-24B2-4206-B3C1-DA33B9485F2B}.Release|Any CPU.Build.0 = Release|Any CPU
{175BA793-13FE-4320-9A16-24FAC844C771}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{175BA793-13FE-4320-9A16-24FAC844C771}.Debug|Any CPU.Build.0 = Debug|Any CPU
{175BA793-13FE-4320-9A16-24FAC844C771}.Release|Any CPU.ActiveCfg = Release|Any CPU
{175BA793-13FE-4320-9A16-24FAC844C771}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit b6ac7e7

Please sign in to comment.