ImageSearch.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using AlibabaCloud.OpenApiClient.Models;
  2. using AlibabaCloud.SDK.ImageSearch20210501.Models;
  3. using AlibabaCloud.SDK.ImageSearch20210501;
  4. using Tea;
  5. using dodohold.core;
  6. using Environment = System.Environment;
  7. namespace molilian.core
  8. {
  9. public partial class AliyunPlus
  10. {
  11. private static string _end_point;
  12. private static int _timeout = 1500;
  13. static AliyunPlus()
  14. {
  15. _end_point = Environment.GetEnvironmentVariable("IMG_EndPoint");
  16. if (string.IsNullOrEmpty(_end_point))
  17. {
  18. #if DEBUG
  19. _end_point = "imagesearch.cn-shanghai.aliyuncs.com";
  20. #else
  21. _end_point = "imagesearch-vpc.cn-shanghai.aliyuncs.com";
  22. #endif
  23. }
  24. }
  25. public async Task<SearchByPicResponse> SearchByPicAsync(string base64String, string pid)
  26. {
  27. if (string.IsNullOrEmpty(base64String)) return null;
  28. if (string.IsNullOrEmpty(pid)) return null;
  29. try
  30. {
  31. byte[] bytes = Convert.FromBase64String(base64String);
  32. AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new();
  33. Config config = new()
  34. {
  35. AccessKeyId = _app_key,
  36. AccessKeySecret = _app_secret,
  37. Endpoint = _end_point,
  38. ConnectTimeout = _timeout,
  39. RegionId = "cn-shanghai"
  40. };
  41. Client client = new(config);
  42. SearchByPicAdvanceRequest request = new()
  43. {
  44. PicContentObject = new MemoryStream(bytes),
  45. Pid = pid,
  46. Crop = true,
  47. Num = 20,
  48. Fields = "itemId,Url,CouponShareUrl,DeeplinkUrl,DeeplinkCouponShareUrl,UserType,ShopTitle,Title,PicUrl,ReservePrice,PriceAfterCoupon,CouponAmount,CouponStartTime,CouponEndTime,Volume,Provcity"
  49. };
  50. // 选填,需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
  51. // request.Fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle";
  52. // 选填,图片类目
  53. //request.CategoryId = 88888888;
  54. // 选填,是否需要进行主体识别。默认true
  55. //request.Crop = false;
  56. // 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
  57. //request.Start = 0;
  58. // 选填,返回结果的数目。取值范围:1-20。默认值:10。
  59. //request.Num = 2;
  60. // 选填,渠道ID。用于淘宝联盟中的渠道区分。
  61. //request.relationId = 1145;
  62. var response = await client.SearchByPicAdvanceAsync(request, runtime);
  63. return response;
  64. }
  65. catch (TeaException error)
  66. {
  67. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  68. // 错误 message
  69. Console.WriteLine(error.Message);
  70. // 诊断地址
  71. Console.WriteLine(error.Data["Recommend"]);
  72. AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
  73. int.TryParse($"{error.DataResult["statusCode"]}", out int statusCode);
  74. SearchByPicResponse response = new();
  75. response.Body = new();
  76. response.Body.Success = false;
  77. response.Body.Code = statusCode;
  78. response.Body.Message = $"{error.DataResult["Code"]},{error.DataResult["Message"]}";
  79. LoggerLibrary log = new("SearchByPic", "tea");
  80. log.Info(error.Message);
  81. _ = log.SaveAsync();
  82. return response;
  83. }
  84. catch (Exception _error)
  85. {
  86. TeaException error = new TeaException(new Dictionary<string, object>
  87. {
  88. { "message", _error.Message }
  89. });
  90. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  91. // 错误 message
  92. Console.WriteLine(error.Message);
  93. // 诊断地址
  94. Console.WriteLine(error.Data["Recommend"]);
  95. AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
  96. LoggerLibrary log = new("SearchByPic", "error");
  97. log.Info(error.Message);
  98. log.Info(error.Data["Recommend"].Convert2Json());
  99. _ = log.SaveAsync();
  100. }
  101. return null;
  102. }
  103. }
  104. }