TaokeOpenCore.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 = await TkPoolCore.GetOneAsync(TkPoolCore.TkAction.promotionQuery, false, string.Empty, 0);
  70. }
  71. else
  72. {
  73. account = await TkPoolCore.GetOneAsync(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.accountId = account.id;
  99. result.accountName = account.name;
  100. result.proxy_nodename = nodeName;
  101. result.proxy_node = proxy?.Address?.Host;
  102. using var cts = new CancellationTokenSource();
  103. cts.CancelAfter(timeout);
  104. DateTime stime = DateTime.Now;
  105. #if DEBUG
  106. account.api_id = 17;
  107. account.api = "aliyun";
  108. account.refpid = "mm_6695915495_3161300068_115818650332";
  109. #endif
  110. switch (account.api)
  111. {
  112. case "veapi":
  113. case "ddx":
  114. case "other_api":
  115. if (account.api_id > 0)
  116. {
  117. result.proxy_nodename = null;
  118. result.proxy_node = null;
  119. var api = OtherApiPoolCore.Get(account.api_id);
  120. var core = new OtherApiPlus(account.name, api.key, api.sessionKey, account.api, api.api_url);
  121. core.picSimilaritem(img, ref result);
  122. var ts2 = DateTime.Now - stime;
  123. result.elapsedTime2 = (int)ts2.TotalMilliseconds;
  124. _ = TkLogCore.PromotionImgLogAsync(result);
  125. return new APIResult(new
  126. {
  127. result.success,
  128. result.message,
  129. result.reason,
  130. promotionImg = result.PromotionImg
  131. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  132. {
  133. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  134. });
  135. }
  136. break;
  137. case "aliyun":
  138. {
  139. result.proxy_nodename = null;
  140. result.proxy_node = null;
  141. AliyunCore api;
  142. if (account.api_id == 0)
  143. {
  144. api = new AliyunCore(account.appkey, account.appSecret);
  145. }
  146. else
  147. {
  148. api = new AliyunCore(account.api_id);
  149. }
  150. var aliDelayTask = Task.Delay(1500, cts.Token);
  151. var aliRequestTask = api.picSimilaritemAsync(img, account.open_pid, result);
  152. var aliCompletedTask = await Task.WhenAny(aliDelayTask, aliRequestTask);
  153. if (aliCompletedTask == aliRequestTask)
  154. {
  155. result = await aliRequestTask;
  156. }
  157. else
  158. {
  159. result.success = false;
  160. result.message = "调用失败";
  161. result.reason = "请求超时";
  162. }
  163. var ts2 = DateTime.Now - stime;
  164. result.elapsedTime2 = (int)ts2.TotalMilliseconds;
  165. _ = TkLogCore.PromotionImgLogAsync(result);
  166. return new APIResult(new
  167. {
  168. result.success,
  169. result.message,
  170. result.reason,
  171. promotionImg = result.PromotionImg
  172. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  173. {
  174. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  175. });
  176. }
  177. }
  178. var apiDelayTask = Task.Delay(500, cts.Token);
  179. var apiRequestTask = Task.Run(() =>
  180. {
  181. return taobao.PromotionQueryRequest(img, oaid);
  182. }, cts.Token);
  183. TbkThorMfrsPromotionQueryResponse data;
  184. var apiCompletedTask = await Task.WhenAny(apiRequestTask, apiDelayTask);
  185. if (apiCompletedTask == apiRequestTask)
  186. {
  187. data = await apiRequestTask;
  188. if (data.IsError)
  189. {
  190. result.success = false;
  191. result.message = "调用失败";
  192. result.reason = data.ErrMsg;
  193. _ = TkLogCore.PromotionImgLogAsync(result);
  194. return new APIResult(new
  195. {
  196. result.success,
  197. result.message,
  198. result.reason,
  199. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  200. {
  201. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  202. });
  203. }
  204. if (!"0".Equals(data.Status))
  205. {
  206. result.success = false;
  207. result.message = "调用失败";
  208. result.reason = data.Desc;
  209. _ = TkLogCore.PromotionImgLogAsync(result);
  210. return new APIResult(new
  211. {
  212. result.success,
  213. result.message,
  214. result.reason,
  215. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  216. {
  217. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  218. });
  219. }
  220. }
  221. else
  222. {
  223. }
  224. //500ms后再提交一次
  225. _ = Task.Run(() =>
  226. {
  227. return taobao.PromotionQueryRequest(img, oaid);
  228. });
  229. var ts = DateTime.Now - stime;
  230. result.elapsedTime2 = (int)ts.TotalMilliseconds;
  231. //result.url = data.Url;
  232. result.url = $"https://mo.m.taobao.com/etao/search_pic_detail?appKey={account.appkey}&oaid={oaid}&pid={account.open_pid}";
  233. string cookies = string.Empty;
  234. var requestTask = Task.Run(async () =>
  235. {
  236. if (recommand)
  237. {
  238. //DateTime stime = DateTime.Now;
  239. //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  240. //var ts = DateTime.Now - stime;
  241. //result.elapsedTime4 = (int)ts.TotalMilliseconds;
  242. var similarPromotionTask = taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies, cts.Token);
  243. var promotionImgTask = taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  244. await Task.WhenAll(similarPromotionTask, promotionImgTask);
  245. (result.SimilarPromotion, cookies, result.elapsedTime3) = await similarPromotionTask;
  246. (result.PromotionImg, cookies, result.elapsedTime4) = await promotionImgTask;
  247. }
  248. else
  249. {
  250. result.elapsedTime3 = 1;
  251. (result.PromotionImg, cookies, result.elapsedTime4) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  252. }
  253. result.similarPromotion = result.SimilarPromotion.Convert2Json();
  254. result.promotionImg = result.PromotionImg.Convert2Json();
  255. return result;
  256. }, cts.Token);
  257. var delayTask = Task.Delay(timeout, cts.Token);
  258. var completedTask = await Task.WhenAny(requestTask, delayTask);
  259. if (completedTask == requestTask)
  260. {
  261. result = await requestTask;
  262. }
  263. else
  264. {
  265. //============================== 放弃转链-请求超时 ==============================
  266. cts.Cancel();
  267. result.success = false;
  268. result.message = "调用失败";
  269. result.reason = "请求超时";
  270. if (!recommand) result.elapsedTime3 = 1;
  271. _ = TkLogCore.PromotionImgLogAsync(result);
  272. return new APIResult(new
  273. {
  274. result.success,
  275. result.message,
  276. result.reason,
  277. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  278. {
  279. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  280. });
  281. }
  282. //(result.SimilarPromotion, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies);
  283. //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies);
  284. //result.similarPromotion = result.SimilarPromotion.Convert2Json();
  285. //result.promotionImg = result.PromotionImg.Convert2Json();
  286. result.success = true;
  287. result.message = "调用成功";
  288. _ = TkLogCore.PromotionImgLogAsync(result);
  289. return new APIResult(new
  290. {
  291. result.success,
  292. result.message,
  293. result.reason,
  294. similarPromotion = result.SimilarPromotion,
  295. promotionImg = result.PromotionImg,
  296. result.url,
  297. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  298. {
  299. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  300. });
  301. }
  302. catch (Exception ex)
  303. {
  304. result.success = false;
  305. result.message = "调用失败";
  306. result.reason = ex.Message;
  307. _ = TkLogCore.PromotionImgLogAsync(result);
  308. return new APIResult(new
  309. {
  310. result.success,
  311. result.message,
  312. result.reason,
  313. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  314. {
  315. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  316. });
  317. }
  318. }
  319. }
  320. }