ImageSearch.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. using Topsdk.Top;
  8. using Topsdk.Top.Defaultability;
  9. using Topsdk.Top.Defaultability.Domain;
  10. using Topsdk.Top.Defaultability.Request;
  11. using Topsdk.Top.Defaultability.Response;
  12. namespace molilian.core
  13. {
  14. public partial class AliyunPlus
  15. {
  16. private static string _end_point;
  17. private static int _timeout = 5000;
  18. static AliyunPlus()
  19. {
  20. _end_point = Environment.GetEnvironmentVariable("IMG_EndPoint");
  21. if (string.IsNullOrEmpty(_end_point))
  22. {
  23. #if DEBUG
  24. _end_point = "imagesearch.cn-shanghai.aliyuncs.com";
  25. #else
  26. //_end_point = "imagesearch-vpc.cn-shanghai.aliyuncs.com";
  27. _end_point = "imagesearch.cn-shanghai.aliyuncs.com";
  28. #endif
  29. }
  30. }
  31. public async Task<SearchByPicResponse> SearchByPicAsync(string base64String, string pid)
  32. {
  33. if (string.IsNullOrEmpty(base64String)) return null;
  34. if (string.IsNullOrEmpty(pid)) return null;
  35. try
  36. {
  37. byte[] bytes = Convert.FromBase64String(base64String);
  38. AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new();
  39. Config config = new()
  40. {
  41. AccessKeyId = _app_key,
  42. AccessKeySecret = _app_secret,
  43. Endpoint = _end_point,
  44. ConnectTimeout = _timeout,
  45. RegionId = "cn-shanghai"
  46. };
  47. Client client = new(config);
  48. SearchByPicAdvanceRequest request = new()
  49. {
  50. PicContentObject = new MemoryStream(bytes),
  51. Pid = pid,
  52. Crop = true,
  53. Num = 20,
  54. Fields = "itemId,Url,CouponShareUrl,DeeplinkUrl,DeeplinkCouponShareUrl,UserType,ShopTitle,Title,PicUrl,ZkFinalPrice,ReservePrice,PriceAfterCoupon,CouponAmount,CouponStartTime,CouponEndTime,Volume,Provcity"
  55. };
  56. // 选填,需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
  57. // request.Fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle";
  58. // 选填,图片类目
  59. //request.CategoryId = 88888888;
  60. // 选填,是否需要进行主体识别。默认true
  61. //request.Crop = false;
  62. // 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
  63. //request.Start = 0;
  64. // 选填,返回结果的数目。取值范围:1-20。默认值:10。
  65. //request.Num = 2;
  66. // 选填,渠道ID。用于淘宝联盟中的渠道区分。
  67. //request.relationId = 1145;
  68. var response = await client.SearchByPicAdvanceAsync(request, runtime);
  69. return response;
  70. }
  71. catch (TeaException error)
  72. {
  73. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  74. // 错误 message
  75. Console.WriteLine(error.Message);
  76. // 诊断地址
  77. Console.WriteLine(error.Data["Recommend"]);
  78. AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
  79. int.TryParse($"{error.DataResult["statusCode"]}", out int statusCode);
  80. SearchByPicResponse response = new();
  81. response.Body = new();
  82. response.Body.Success = false;
  83. response.Body.Code = statusCode;
  84. response.Body.Message = $"{error.DataResult["Code"]},{error.DataResult["Message"]}";
  85. LoggerLibrary log = new("SearchByPic", "tea");
  86. log.Info(error.Message);
  87. _ = log.SaveAsync();
  88. return response;
  89. }
  90. catch (Exception _error)
  91. {
  92. TeaException error = new TeaException(new Dictionary<string, object>
  93. {
  94. { "message", _error.Message }
  95. });
  96. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  97. // 错误 message
  98. Console.WriteLine(error.Message);
  99. // 诊断地址
  100. Console.WriteLine(error.Data["Recommend"]);
  101. AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
  102. LoggerLibrary log = new("SearchByPic", "error");
  103. log.Info(error.Message);
  104. log.Info(error.Data["Recommend"].Convert2Json());
  105. _ = log.SaveAsync();
  106. }
  107. return null;
  108. }
  109. public async Task<GetProductInfoByIdsResponse> GetProductInfoByIds(string itemIds, string pid)
  110. {
  111. if (string.IsNullOrEmpty(itemIds)) return null;
  112. if (string.IsNullOrEmpty(pid)) return null;
  113. try
  114. {
  115. AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new();
  116. Config config = new()
  117. {
  118. AccessKeyId = _app_key,
  119. AccessKeySecret = _app_secret,
  120. Endpoint = _end_point,
  121. ConnectTimeout = _timeout,
  122. RegionId = "cn-shanghai"
  123. };
  124. Client client = new(config);
  125. GetProductInfoByIdsRequest request = new()
  126. {
  127. ItemIds = itemIds,
  128. Pid = pid,
  129. Fields = "itemId,Title,Nick,SellerId,ShortTitle,ShopTitle,SubTitle,PicUrl,ReservePrice,ZkFinalPrice,PriceAfterCoupon,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,DeeplinkCouponShareUrl,Url,DeeplinkUrl"
  130. };
  131. var response = await client.GetProductInfoByIdsWithOptionsAsync(request, runtime);
  132. return response;
  133. }
  134. catch (TeaException error)
  135. {
  136. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  137. // 错误 message
  138. Console.WriteLine(error.Message);
  139. // 诊断地址
  140. Console.WriteLine(error.Data["Recommend"]);
  141. AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
  142. int.TryParse($"{error.DataResult["statusCode"]}", out int statusCode);
  143. GetProductInfoByIdsResponse response = new();
  144. response.Body = new();
  145. response.Body.Success = false;
  146. response.Body.Code = statusCode;
  147. response.Body.Message = $"{error.DataResult["Code"]},{error.DataResult["Message"]}";
  148. LoggerLibrary log = new("GetProductInfoByIds", "tea");
  149. log.Info(error.Message);
  150. _ = log.SaveAsync();
  151. return response;
  152. }
  153. catch (Exception _error)
  154. {
  155. TeaException error = new TeaException(new Dictionary<string, object>
  156. {
  157. { "message", _error.Message }
  158. });
  159. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  160. // 错误 message
  161. Console.WriteLine(error.Message);
  162. // 诊断地址
  163. Console.WriteLine(error.Data["Recommend"]);
  164. AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
  165. LoggerLibrary log = new("GetProductInfoByIds", "error");
  166. log.Info(error.Message);
  167. log.Info(error.Data["Recommend"].Convert2Json());
  168. _ = log.SaveAsync();
  169. }
  170. return null;
  171. }
  172. }
  173. }