TaokeOpenCore.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using dodohold.core;
  2. using Google.Protobuf.WellKnownTypes;
  3. using System.Text.Json;
  4. using System.Threading;
  5. using Top.Api.Response;
  6. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  7. namespace molilian.core
  8. {
  9. public partial class TaokeOpenCore
  10. {
  11. static string _test_base64String = string.Empty;
  12. public TkConfigDTO _config;
  13. private static string _end_point;
  14. static TaokeOpenCore()
  15. {
  16. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  17. }
  18. public TaokeOpenCore()
  19. {
  20. _config = TkConfigCore.Get();
  21. }
  22. public async Task<APIResult> PromotionQueryAsync(string img, string oaid = "", string ip = "", string scene = "", bool recommand = true, bool debug = false, int aid = 0)
  23. {
  24. PromotionQueryDTO result = new()
  25. {
  26. end_point = _end_point,
  27. ip = ip,
  28. oaid = oaid,
  29. scene = scene,
  30. img = img
  31. };
  32. if (string.IsNullOrEmpty(oaid))
  33. {
  34. result.success = false;
  35. result.message = "调用失败";
  36. result.reason = "无效OAID";
  37. _ = TkLogCore.PromotionImgLogAsync(result);
  38. return new APIResult(new
  39. {
  40. result.success,
  41. result.message,
  42. result.reason,
  43. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  44. {
  45. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  46. });
  47. }
  48. if (string.IsNullOrEmpty(img))
  49. {
  50. result.success = false;
  51. result.message = "调用失败";
  52. result.reason = "无效图片";
  53. _ = TkLogCore.PromotionImgLogAsync(result);
  54. return new APIResult(new
  55. {
  56. result.success,
  57. result.message,
  58. result.reason,
  59. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  60. {
  61. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  62. });
  63. }
  64. try
  65. {
  66. TkPoolDTO account;
  67. if (aid == 0)
  68. {
  69. account = TkPoolCore.GetOne(TkPoolCore.TkAction.promotionQuery);
  70. }
  71. else
  72. {
  73. account = TkPoolCore.GetOne(aid);
  74. }
  75. if (account == null)
  76. {
  77. result.success = false;
  78. result.message = "调用失败";
  79. result.reason = "没有匹配账号";
  80. _ = TkLogCore.PromotionImgLogAsync(result);
  81. return new APIResult(new
  82. {
  83. result.success,
  84. result.message,
  85. result.reason,
  86. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  87. {
  88. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  89. });
  90. }
  91. string nodeName = ProxyNodesCore.GetSelectOne(account.nodeName);
  92. var proxy = ProxyNodesCore.GetOne(nodeName);
  93. TaobaoOpenPlus taobao = new(account.appkey, account.appSecret, account.open_pid, debug, proxy);
  94. int timeout = _config.similar_timeout;
  95. #if DEBUG
  96. timeout = 50000;
  97. #endif
  98. result.proxy_nodename = nodeName;
  99. result.proxy_node = proxy?.Address?.Host;
  100. using var cts = new CancellationTokenSource();
  101. cts.CancelAfter(timeout);
  102. DateTime stime = DateTime.Now;
  103. if ("veapi".Equals(account.api) && account.api_id > 0)
  104. {
  105. result.proxy_nodename = null;
  106. result.proxy_node = null;
  107. var api = VeapiPoolCore.Get(account.api_id);
  108. if (account != null)
  109. {
  110. var core = new VeapiPlus(account.name, api.key, api.sessionKey);
  111. core.picSimilaritem(img, ref result);
  112. var ts2 = DateTime.Now - stime;
  113. result.elapsedTime2 = (int)ts2.TotalMilliseconds;
  114. _ = TkLogCore.PromotionImgLogAsync(result);
  115. return new APIResult(new
  116. {
  117. result.success,
  118. result.message,
  119. result.reason,
  120. promotionImg = result.PromotionImg
  121. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  122. {
  123. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  124. });
  125. }
  126. }
  127. var apiDelayTask = Task.Delay(500, cts.Token);
  128. var apiRequestTask = Task.Run(() =>
  129. {
  130. return taobao.PromotionQueryRequest(img, oaid);
  131. }, cts.Token);
  132. TbkThorMfrsPromotionQueryResponse data;
  133. var apiCompletedTask = await Task.WhenAny(apiRequestTask, apiDelayTask);
  134. if (apiCompletedTask == apiRequestTask)
  135. {
  136. data = await apiRequestTask;
  137. if (data.IsError)
  138. {
  139. result.success = false;
  140. result.message = "调用失败";
  141. result.reason = data.ErrMsg;
  142. _ = TkLogCore.PromotionImgLogAsync(result);
  143. return new APIResult(new
  144. {
  145. result.success,
  146. result.message,
  147. result.reason,
  148. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  149. {
  150. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  151. });
  152. }
  153. if (!"0".Equals(data.Status))
  154. {
  155. result.success = false;
  156. result.message = "调用失败";
  157. result.reason = data.Desc;
  158. _ = TkLogCore.PromotionImgLogAsync(result);
  159. return new APIResult(new
  160. {
  161. result.success,
  162. result.message,
  163. result.reason,
  164. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  165. {
  166. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  167. });
  168. }
  169. }
  170. else
  171. {
  172. }
  173. //500ms后再提交一次
  174. _ = Task.Run(() =>
  175. {
  176. return taobao.PromotionQueryRequest(img, oaid);
  177. });
  178. var ts = DateTime.Now - stime;
  179. result.elapsedTime2 = (int)ts.TotalMilliseconds;
  180. //result.url = data.Url;
  181. result.url = $"https://mo.m.taobao.com/etao/search_pic_detail?appKey={account.appkey}&oaid={oaid}&pid={account.open_pid}";
  182. string cookies = string.Empty;
  183. var requestTask = Task.Run(async () =>
  184. {
  185. if (recommand)
  186. {
  187. //DateTime stime = DateTime.Now;
  188. //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  189. //var ts = DateTime.Now - stime;
  190. //result.elapsedTime4 = (int)ts.TotalMilliseconds;
  191. var similarPromotionTask = taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies, cts.Token);
  192. var promotionImgTask = taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  193. await Task.WhenAll(similarPromotionTask, promotionImgTask);
  194. (result.SimilarPromotion, cookies, result.elapsedTime3) = await similarPromotionTask;
  195. (result.PromotionImg, cookies, result.elapsedTime4) = await promotionImgTask;
  196. }
  197. else
  198. {
  199. result.elapsedTime3 = 1;
  200. (result.PromotionImg, cookies, result.elapsedTime4) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  201. }
  202. result.similarPromotion = result.SimilarPromotion.Convert2Json();
  203. result.promotionImg = result.PromotionImg.Convert2Json();
  204. return result;
  205. }, cts.Token);
  206. var delayTask = Task.Delay(timeout, cts.Token);
  207. var completedTask = await Task.WhenAny(requestTask, delayTask);
  208. if (completedTask == requestTask)
  209. {
  210. result = await requestTask;
  211. }
  212. else
  213. {
  214. //============================== 放弃转链-请求超时 ==============================
  215. cts.Cancel();
  216. result.success = false;
  217. result.message = "调用失败";
  218. result.reason = "请求超时";
  219. if (!recommand) result.elapsedTime3 = 1;
  220. _ = TkLogCore.PromotionImgLogAsync(result);
  221. return new APIResult(new
  222. {
  223. result.success,
  224. result.message,
  225. result.reason,
  226. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  227. {
  228. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  229. });
  230. }
  231. //(result.SimilarPromotion, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies);
  232. //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies);
  233. //result.similarPromotion = result.SimilarPromotion.Convert2Json();
  234. //result.promotionImg = result.PromotionImg.Convert2Json();
  235. result.success = true;
  236. result.message = "调用成功";
  237. _ = TkLogCore.PromotionImgLogAsync(result);
  238. return new APIResult(new
  239. {
  240. result.success,
  241. result.message,
  242. result.reason,
  243. similarPromotion = result.SimilarPromotion,
  244. promotionImg = result.PromotionImg,
  245. result.url,
  246. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  247. {
  248. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  249. });
  250. }
  251. catch (Exception ex)
  252. {
  253. result.success = false;
  254. result.message = "调用失败";
  255. result.reason = ex.Message;
  256. _ = TkLogCore.PromotionImgLogAsync(result);
  257. return new APIResult(new
  258. {
  259. result.success,
  260. result.message,
  261. result.reason,
  262. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  263. {
  264. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  265. });
  266. }
  267. }
  268. }
  269. }