using BaiDuSdk;
|
using CommonUtil;
|
using HtmlAgilityPack;
|
using Operater.DAL;
|
using Operater.DbModel;
|
using Operater.DTO.System;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using YouZanSDKStandard.Api;
|
using YouZanSDKStandard.Api.Request;
|
|
namespace ProductDistribution.Handle
|
{
|
public class ProductCheckHandle
|
{
|
public static string _errFileName = "检测关键词error";
|
public static void MainMethod(ProductDistributionMq msg)
|
{
|
try
|
{
|
var appSet = (new AppsetDAL().ListGet(t => t.Id != null))[0];
|
var proList = new DistributionRelationDAL().ListGet(t => t.ReStatus == 1);
|
int totalCount = 0;
|
// var proList = new DistributionRelationDAL().ListPageGet(t => t.ReStatus == 1, 1, 100, ref totalCount);
|
//获取过滤关键词
|
var filter = new ProductFilterDAL().ListGet(t => t.Id != null);
|
int index = 0;
|
foreach (var pro in proList)
|
{
|
index++;
|
Console.WriteLine($"{index}/{proList.Count}");
|
try
|
{
|
////获取有赞商品详情
|
YouZanProductDetailGetRequest detailReq = new YouZanProductDetailGetRequest()
|
{
|
item_id = long.Parse(pro.YzProduct)
|
};
|
var detailRs = YouZanClient.Execute(detailReq, appSet.YzToken);
|
if (detailRs.IsNull() || !detailRs.success)
|
{
|
Console.WriteLine("获取有赞商品详情失败");
|
LogUtil.Info("获取有赞商品详情失败:" + detailRs.message, "错误");
|
continue;
|
}
|
string checkResult = "";
|
//检测title
|
string titleErr = FilterMsg(filter, detailRs.data.title);
|
checkResult += titleErr.IsNullOrEmpty() ? "" : ($"标题:[{titleErr}];");
|
//检测详情
|
string descErr = FilterMsg(filter, detailRs.data.desc);
|
checkResult += descErr.IsNullOrEmpty() ? "" : ($"详情文字:[{descErr}];");
|
//检测主图
|
//只检第一张
|
string imgErr = FilterImg(filter, detailRs.data.images[0].image_url);
|
checkResult += imgErr.IsNullOrEmpty() ? "" : ($"主图:[{imgErr}];");
|
//StringBuilder imgErr = new StringBuilder();
|
//foreach (var img in detailRs.data.images)
|
//{
|
// string err = FilterImg(filter, img.image_url);
|
// if (!err.IsNullOrEmpty())
|
// {
|
// imgErr.Append(err + ",");
|
// }
|
//}
|
//if (!imgErr.IsNull() && imgErr.Length > 0)
|
//{
|
// checkResult += ($"主图:[{imgErr.ToString()}];");
|
//}
|
//检测sku图片
|
//StringBuilder skuErr = new StringBuilder();
|
//foreach (var sku in detailRs.data.sku_value_props)
|
//{
|
// if (sku.img_url.IsNullOrEmpty())
|
// {
|
// continue;
|
// }
|
// string err = FilterImg(filter, sku.img_url);
|
// if (!err.IsNullOrEmpty())
|
// {
|
// skuErr.Append(err + ",");
|
// }
|
//}
|
//if (!skuErr.IsNull() && skuErr.Length > 0)
|
//{
|
// checkResult += ($"sku图片:[{skuErr.ToString()}];");
|
//}
|
//检测商品详情图片
|
//string descImgErr = FilterDesc(filter, detailRs.data.desc);
|
//checkResult += descImgErr.IsNullOrEmpty() ? "" : ($"详情图片:[{descImgErr}];");
|
if (checkResult.IsNullOrEmpty())
|
{
|
continue;
|
}
|
ProductCheckRecord productCheckRecord = new ProductCheckRecord()
|
{
|
Id = Guid.NewGuid().ToString(),
|
YzProductId = pro.YzProduct,
|
AliProductId = pro.SourceProduct,
|
CheckTime = DateTime.Now,
|
TaskId = msg.TaskId,
|
Memo = checkResult,
|
YzAlias = detailRs.data.alias,
|
YzImg = detailRs.data.images[0].image_url,
|
YzTitle = detailRs.data.title
|
};
|
new ProductCheckRecordDAL().Insert(productCheckRecord);
|
}
|
catch (Exception e)
|
{
|
LogUtil.Info("循环错误:" + e.ToString(), "检测关键词error");
|
continue;
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
LogUtil.Info("错误:" + e.ToString(), "主线程错误");
|
}
|
}
|
|
#region 检测关键词
|
public static string FilterMsg(List<ProductFilter> filter, string msg)
|
{
|
StringBuilder errMsg = new StringBuilder();
|
try
|
{
|
foreach (var fi in filter)
|
{
|
if (msg.ToLower().Contains(fi.KeyWord.ToLower()))
|
{
|
errMsg.Append(fi.KeyWord + ",");
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
LogUtil.Info(e.ToString(), "过滤关键词error");
|
}
|
if (!errMsg.IsNull() && errMsg.Length > 0)
|
{
|
return errMsg.ToString();
|
}
|
return "";
|
}
|
|
#endregion
|
|
#region 图片关键词过滤
|
public static string FilterImg(List<ProductFilter> filter, string imgUrl)
|
{
|
try
|
{
|
StringBuilder keyError = new StringBuilder();
|
//百度ai图片识别
|
var res = BaiDuClient.WebImageUrl(imgUrl);
|
if (!res.IsSuccess)
|
{
|
LogUtil.Info($"百度ai错误:{res.error_msg}--图片:{imgUrl}", "过滤关键词error");
|
}
|
if (res.words_result_num <= 0)
|
{
|
return null;
|
}
|
var msgList = res.words_result.Select(t => t.words).ToList();
|
string msg = string.Join(',', msgList);
|
var filterKey = filter.Where(t => msg.ToLower().Contains(t.KeyWord.ToLower())).ToList();
|
if (!filterKey.IsNull() && filterKey.Count > 0)
|
{
|
var filterList = filterKey.Select(t => t.KeyWord).ToList();
|
return string.Join(',', filterList);
|
}
|
}
|
catch (Exception e)
|
{
|
LogUtil.Info(e.ToString(), "过滤关键词error");
|
}
|
return null;
|
}
|
#endregion
|
|
#region 检测商品详情图片
|
public static string FilterDesc(List<ProductFilter> filter, string desc)
|
{
|
StringBuilder errMsg = new StringBuilder();
|
HtmlDocument doc = new HtmlDocument();
|
doc.LoadHtml(desc);
|
var imgNode = doc.DocumentNode.SelectNodes("//img");
|
foreach (var imgItem in imgNode)
|
{
|
var imgsrc = imgItem.Attributes["src"].Value;
|
string imgError = FilterImg(filter, imgsrc);
|
if (!imgError.IsNullOrEmpty())
|
{
|
errMsg.Append(imgError + ",");
|
}
|
}
|
if (!errMsg.IsNull() && errMsg.Length > 0)
|
{
|
return errMsg.ToString();
|
}
|
return "";
|
}
|
|
#endregion
|
}
|
}
|