Api/Netina.Infrastructure/Services/Scrapers/DigikalaScraper.cs

102 lines
4.4 KiB
C#
Raw Permalink Normal View History

2024-04-16 20:01:34 +03:30
using MediatR;
using Microsoft.EntityFrameworkCore;
2024-04-16 20:01:34 +03:30
using Netina.Domain.CommandQueries.Commands;
using Netina.Domain.Dtos.ScraperDtos.Response;
using Netina.Domain.Dtos.SmallDtos;
using Netina.Domain.Entities.Brands;
using Netina.Domain.Entities.ProductCategories;
using Netina.Repository.Repositories.Base.Contracts;
2024-04-16 20:01:34 +03:30
namespace Netina.Infrastructure.Services.Scrapers;
2024-08-09 21:55:16 +03:30
public class DigikalaScraper(
IRestApiWrapper apiWrapper,
IRepositoryWrapper repositoryWrapper,
IMediator mediator,
IUploadFileService uploadFileService)
: IDigikalaScraper
{
public async Task<List<ScraperProductDto>> GetProductsByNameAsync(string productName)
{
2024-08-09 21:55:16 +03:30
var products = await apiWrapper.DigikalaRestApi.SearchProductAsync(productName);
return products.data.products.Select(s => new ScraperProductDto
{
PersianName = s.title_fa,
EnglishName = s.title_en,
MainImage = s.images.main.url.First(),
//Cost = long.TryParse(s.default_variant.price.rrp_price,out long result) ? result : 0,
ScraperId = s.id.ToString(),
ScraperUrl = $"https://digikala.com/{s.url.uri}"
}).ToList();
}
public async Task<bool> AddProductToShopAsync(string productId, string productName, CancellationToken cancellationToken = default)
{
2024-08-09 21:55:16 +03:30
var response = await apiWrapper.DigikalaRestApi.GetProductAsync(productId);
var digiProduct = response.data;
var newSummery = digiProduct.seo.description.Replace("فروشگاه اینترنتی دیجی\u200cکالا", "فروشگاه اینترنتی وسمه");
var specifications = new List<SpecificationSDto>();
foreach (var specification in digiProduct.product.specifications)
{
foreach (var attribute in specification.attributes)
{
specifications.Add(new SpecificationSDto { Value = string.Join(",", attribute.values), Title = attribute.title });
}
}
using var httClient = new HttpClient();
var imageBytes = await httClient.GetByteArrayAsync(digiProduct.product.images.main.url.FirstOrDefault(), cancellationToken);
var imageBase64 = Convert.ToBase64String(imageBytes);
var uploadFile = new FileUploadRequest
{
StringBaseFile = imageBase64,
FileName = digiProduct.product.title_fa.Replace(" ", "_") + ".jpg",
FileUploadType = FileUploadType.Image,
ContentType = "image/jpeg"
};
2024-08-09 21:55:16 +03:30
var uploadResponse = await uploadFileService.UploadImageAsync(uploadFile);
var files = new List<StorageFileSDto>
{
new StorageFileSDto
{
FileLocation = uploadResponse.FileLocation,
FileName = uploadResponse.FileName,
IsPrimary = true,
}
};
2024-08-09 21:55:16 +03:30
var nonBrand = await repositoryWrapper.SetRepository<Brand>()
.TableNoTracking
.FirstOrDefaultAsync(b => b.PersianName == "بدون برند", cancellationToken);
if (nonBrand == null)
{
nonBrand = Brand.Create("بدون برند" , "NoBrand","محصولات بدون برند",false,string.Empty);
2024-08-09 21:55:16 +03:30
repositoryWrapper.SetRepository<Brand>()
.Add(nonBrand);
2024-08-09 21:55:16 +03:30
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
2024-08-09 21:55:16 +03:30
var nonCat = await repositoryWrapper.SetRepository<ProductCategory>()
.TableNoTracking
.FirstOrDefaultAsync(b => b.Name == "دسته بندی نشده", cancellationToken);
if (nonCat == null)
{
nonCat = ProductCategory.Create("دسته بندی نشده", "محصولات بدون دسته بندی", false);
2024-08-09 21:55:16 +03:30
repositoryWrapper.SetRepository<ProductCategory>()
.Add(nonCat);
2024-08-09 21:55:16 +03:30
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
if (digiProduct.product.title_en.IsNullOrEmpty())
digiProduct.product.title_en = digiProduct.product.title_fa;
var request = new CreateProductCommand(digiProduct.product.title_fa, digiProduct.product.title_en,
newSummery,
string.Empty, string.Empty, string.Empty, true, 0,
0, 0, false
, 5, false, nonBrand.Id, nonCat.Id, new DiscountSDto(), specifications, files,new Dictionary<string, string>(),new Dictionary<string, string>());
2024-08-09 21:55:16 +03:30
await mediator.Send(request, cancellationToken);
return true;
}
}