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.
Merge pull request #31 from fornit1917/27-Grab-images-from-free-Stock
#27 grab images from free stock
- Loading branch information
Showing
14 changed files
with
359 additions
and
2 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,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(); | ||
} | ||
|
||
} | ||
} |
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,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); | ||
} | ||
} |
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.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; } | ||
} | ||
} |
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,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> |
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,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(); | ||
} | ||
} | ||
} |
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,7 @@ | ||
{ | ||
"profiles": { | ||
"ImageBase.GrabbingImages": { | ||
"commandName": "Project" | ||
} | ||
} | ||
} |
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,7 @@ | ||
{ | ||
"dependencies": { | ||
"secrets1": { | ||
"type": "secrets" | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
ImageBase.GrabbingImages/Properties/serviceDependencies.local.json
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,7 @@ | ||
{ | ||
"dependencies": { | ||
"secrets1": { | ||
"type": "secrets.user" | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
ImageBase.GrabbingImages/Services/Implementations/GrabberFactory.cs
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,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
61
ImageBase.GrabbingImages/Services/Implementations/PexelsGrabber.cs
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,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(); | ||
|
||
} | ||
} | ||
} |
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,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
13
ImageBase.GrabbingImages/Services/Interfaces/IGrabberFactory.cs
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,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(); | ||
} | ||
} |
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,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) |
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