| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- using dodohold.core;
- using Google.Protobuf.WellKnownTypes;
- using System.Text.Json;
- using System.Threading;
- using Top.Api.Response;
- using static dodohold.core.ZTOExpress.CreateOrderArgs;
- namespace molilian.core
- {
- public partial class TaokeOpenCore
- {
- static string _test_base64String = string.Empty;
- public TkConfigDTO _config;
- private static string _end_point;
- static TaokeOpenCore()
- {
- _end_point = Environment.GetEnvironmentVariable("EndPoint");
- }
- public TaokeOpenCore()
- {
- _config = TkConfigCore.Get();
- }
- public async Task<APIResult> PromotionQueryAsync(string img, string oaid = "", string ip = "", string scene = "", bool recommand = true, bool debug = false, int aid = 0)
- {
- PromotionQueryDTO result = new()
- {
- end_point = _end_point,
- ip = ip,
- oaid = oaid,
- scene = scene,
- img = img
- };
- if (string.IsNullOrEmpty(oaid))
- {
- result.success = false;
- result.message = "调用失败";
- result.reason = "无效OAID";
- _ = TkLogCore.PromotionImgLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- }, APIResultCodeEnum.OK, new JsonSerializerOptions
- {
- Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- }
- if (string.IsNullOrEmpty(img))
- {
- result.success = false;
- result.message = "调用失败";
- result.reason = "无效图片";
- _ = TkLogCore.PromotionImgLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- }, APIResultCodeEnum.OK, new JsonSerializerOptions
- {
- Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- }
- try
- {
- TkPoolDTO account;
- if (aid == 0)
- {
- account = await TkPoolCore.GetOneAsync(TkPoolCore.TkAction.promotionQuery, false, string.Empty);
- }
- else
- {
- account = await TkPoolCore.GetOneAsync(aid);
- }
- if (account == null)
- {
- result.success = false;
- result.message = "调用失败";
- result.reason = "没有匹配账号";
- _ = TkLogCore.PromotionImgLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- }, APIResultCodeEnum.OK, new JsonSerializerOptions
- {
- Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- }
- string nodeName = ProxyNodesCore.GetSelectOne(account.nodeName);
- var proxy = ProxyNodesCore.GetOne(nodeName);
- TaobaoOpenPlus taobao = new(account.appkey, account.appSecret, account.open_pid, debug, proxy);
- int timeout = _config.similar_timeout;
- #if DEBUG
- timeout = 50000;
- #endif
- result.accountId = account.id;
- result.accountName = account.name;
- result.proxy_nodename = nodeName;
- result.proxy_node = proxy?.Address?.Host;
- using var cts = new CancellationTokenSource();
- cts.CancelAfter(timeout);
- DateTime stime = DateTime.Now;
- #if DEBUG
- account.api_id = 17;
- account.api = "aliyun";
- account.refpid = "mm_6695915495_3161300068_115818650332";
- #endif
- switch (account.api)
- {
- case "veapi":
- case "ddx":
- case "other_api":
- if (account.api_id > 0)
- {
- result.proxy_nodename = null;
- result.proxy_node = null;
- var api = OtherApiPoolCore.Get(account.api_id);
- var core = new OtherApiPlus(account.name, api.key, api.sessionKey, account.api, api.api_url);
- core.picSimilaritem(img, ref result);
- var ts2 = DateTime.Now - stime;
- result.elapsedTime2 = (int)ts2.TotalMilliseconds;
- _ = TkLogCore.PromotionImgLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- promotionImg = result.PromotionImg
- }, APIResultCodeEnum.OK, new JsonSerializerOptions
- {
- Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- }
- break;
- case "aliyun":
- {
- result.proxy_nodename = null;
- result.proxy_node = null;
- AliyunCore api;
- if (account.api_id == 0)
- {
- api = new AliyunCore(account.appkey, account.appSecret);
- }
- else
- {
- api = new AliyunCore(account.api_id);
- }
- var aliDelayTask = Task.Delay(1500, cts.Token);
- var aliRequestTask = api.picSimilaritemAsync(img, account.open_pid, result);
- var aliCompletedTask = await Task.WhenAny(aliDelayTask, aliRequestTask);
- if (aliCompletedTask == aliRequestTask)
- {
- result = await aliRequestTask;
- }
- else
- {
- result.success = false;
- result.message = "调用失败";
- result.reason = "请求超时";
- }
- var ts2 = DateTime.Now - stime;
- result.elapsedTime2 = (int)ts2.TotalMilliseconds;
- _ = TkLogCore.PromotionImgLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- promotionImg = result.PromotionImg
- }, APIResultCodeEnum.OK, new JsonSerializerOptions
- {
- Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- }
- }
- var apiDelayTask = Task.Delay(500, cts.Token);
- var apiRequestTask = Task.Run(() =>
- {
- return taobao.PromotionQueryRequest(img, oaid);
- }, cts.Token);
- TbkThorMfrsPromotionQueryResponse data;
- var apiCompletedTask = await Task.WhenAny(apiRequestTask, apiDelayTask);
- if (apiCompletedTask == apiRequestTask)
- {
- data = await apiRequestTask;
- if (data.IsError)
- {
- result.success = false;
- result.message = "调用失败";
- result.reason = data.ErrMsg;
- _ = TkLogCore.PromotionImgLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- }, APIResultCodeEnum.OK, new JsonSerializerOptions
- {
- Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- }
- if (!"0".Equals(data.Status))
- {
- result.success = false;
- result.message = "调用失败";
- result.reason = data.Desc;
- _ = TkLogCore.PromotionImgLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- }, APIResultCodeEnum.OK, new JsonSerializerOptions
- {
- Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- }
- }
- else
- {
- }
- //500ms后再提交一次
- _ = Task.Run(() =>
- {
- return taobao.PromotionQueryRequest(img, oaid);
- });
- var ts = DateTime.Now - stime;
- result.elapsedTime2 = (int)ts.TotalMilliseconds;
- //result.url = data.Url;
- result.url = $"https://mo.m.taobao.com/etao/search_pic_detail?appKey={account.appkey}&oaid={oaid}&pid={account.open_pid}";
- string cookies = string.Empty;
- var requestTask = Task.Run(async () =>
- {
- if (recommand)
- {
- //DateTime stime = DateTime.Now;
- //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
- //var ts = DateTime.Now - stime;
- //result.elapsedTime4 = (int)ts.TotalMilliseconds;
- var similarPromotionTask = taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies, cts.Token);
- var promotionImgTask = taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
- await Task.WhenAll(similarPromotionTask, promotionImgTask);
- (result.SimilarPromotion, cookies, result.elapsedTime3) = await similarPromotionTask;
- (result.PromotionImg, cookies, result.elapsedTime4) = await promotionImgTask;
- }
- else
- {
- result.elapsedTime3 = 1;
- (result.PromotionImg, cookies, result.elapsedTime4) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
- }
- result.similarPromotion = result.SimilarPromotion.Convert2Json();
- result.promotionImg = result.PromotionImg.Convert2Json();
- return result;
- }, cts.Token);
- var delayTask = Task.Delay(timeout, cts.Token);
- var completedTask = await Task.WhenAny(requestTask, delayTask);
- if (completedTask == requestTask)
- {
- result = await requestTask;
- }
- else
- {
- //============================== 放弃转链-请求超时 ==============================
- cts.Cancel();
- result.success = false;
- result.message = "调用失败";
- result.reason = "请求超时";
- if (!recommand) result.elapsedTime3 = 1;
- _ = TkLogCore.PromotionImgLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- }, APIResultCodeEnum.OK, new JsonSerializerOptions
- {
- Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- }
- //(result.SimilarPromotion, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies);
- //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies);
- //result.similarPromotion = result.SimilarPromotion.Convert2Json();
- //result.promotionImg = result.PromotionImg.Convert2Json();
- result.success = true;
- result.message = "调用成功";
- _ = TkLogCore.PromotionImgLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- similarPromotion = result.SimilarPromotion,
- promotionImg = result.PromotionImg,
- result.url,
- }, APIResultCodeEnum.OK, new JsonSerializerOptions
- {
- Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- }
- catch (Exception ex)
- {
- result.success = false;
- result.message = "调用失败";
- result.reason = ex.Message;
- _ = TkLogCore.PromotionImgLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- }, APIResultCodeEnum.OK, new JsonSerializerOptions
- {
- Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- }
- }
- }
- }
|