Jelajahi Sumber

调整搜图入参和超时处理;

dodo hold 2 tahun lalu
induk
melakukan
28086a2812

+ 9 - 1
molilian.api/Controllers/public/ApiController.cs

@@ -75,13 +75,21 @@ namespace molilian.api.Controllers
             var url = form.Read("url", string.Empty);
             var ip = form.Read("ip", string.Empty);
             var oaid = form.Read("oaid", string.Empty);
+            var recommand = form.Read("recommand", true);
 
             if (!string.IsNullOrEmpty(url))
             {
                 var bytes = new WebClientUtility().Request(url).ResponseBody;
                 img = Convert.ToBase64String(bytes);
             }
-            return await core.PromotionQueryAsync(img, oaid, ip);
+
+            if (string.IsNullOrEmpty(oaid))
+            {
+                Random rand = new Random();
+                oaid = $"{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}";
+            }
+
+            return await core.PromotionQueryAsync(img, oaid, ip, recommand);
         }
 
 

File diff ditekan karena terlalu besar
+ 0 - 0
molilian.api/Properties/PublishProfiles/https___ccr.ccs.tencentyun.com_shaobin.pubxml.user


+ 98 - 123
molilian.core/Core/taoke/TaokeOpenCore.cs

@@ -1,5 +1,6 @@
 using dodohold.core;
 using System.Text.Json;
+using Top.Api.Response;
 
 
 namespace molilian.core
@@ -21,7 +22,7 @@ namespace molilian.core
         }
 
 
-        public async Task<APIResult> PromotionQueryAsync(string img, string oaid = "", string ip = "")
+        public async Task<APIResult> PromotionQueryAsync(string img, string oaid = "", string ip = "", bool recommand = true)
         {
             PromotionQueryDTO result = new()
             {
@@ -30,6 +31,22 @@ namespace molilian.core
                 oaid = oaid,
                 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))
             {
@@ -67,63 +84,100 @@ namespace molilian.core
                     });
                 }
 
+
                 TaobaoOpenPlus taobao = new(account.appkey, account.appSecret, account.open_pid);
-                var data = taobao.PromotionQueryRequest(img, oaid);
-                if (data.IsError)
+                int timeout = _config.similar_timeout;
+#if DEBUG
+                timeout = 5000;
+#endif
+                using var cts = new CancellationTokenSource();
+                cts.CancelAfter(timeout);
+
+
+
+                DateTime stime = DateTime.Now;
+
+                var apiDelayTask = Task.Delay(500, cts.Token);
+                var apiRequestTask = Task.Run(() =>
                 {
-                    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))
+                    return taobao.PromotionQueryRequest(img, oaid);
+
+                }, cts.Token);
+
+                TbkThorMfrsPromotionQueryResponse data;
+                var apiCompletedTask = await Task.WhenAny(apiRequestTask, apiDelayTask);
+                if (apiCompletedTask == apiRequestTask)
                 {
-                    result.success = false;
-                    result.message = "调用失败";
-                    result.reason = data.Desc;
-                    _ = TkLogCore.PromotionImgLogAsync(result);
-                    return new APIResult(new
+                    data = await apiRequestTask;
+                    if (data.IsError)
                     {
-                        result.success,
-                        result.message,
-                        result.reason,
-                    }, APIResultCodeEnum.OK, new JsonSerializerOptions
+                        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))
                     {
-                        Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
-                    });
+                        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
+                        });
+                    }
                 }
-                result.url = data.Url;
-
-                oaid = data.Url.GetContentPart("oaid=", "&");
-                string cookies = string.Empty;
 
+                var ts = DateTime.Now - stime;
+                result.elapsedTime2 = (int)ts.TotalMilliseconds;
 
 
-                int timeout = _config.similar_timeout;
-#if DEBUG
-                timeout = 5000;
-#endif
-                using var cts = new CancellationTokenSource();
-                cts.CancelAfter(timeout);
-
+                //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 () =>
                 {
-                    (result.SimilarPromotion, cookies) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies, cts.Token);
-                    (result.PromotionImg, cookies) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
-                    result.similarPromotion = result.SimilarPromotion.Convert2Json();
-                    result.promotionImg = result.PromotionImg.Convert2Json();
+                    if (recommand)
+                    {
+
+                        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;
+                        result.similarPromotion = result.SimilarPromotion.Convert2Json();
+                        result.promotionImg = result.PromotionImg.Convert2Json();
+
+                    }
+                    else
+                    {
+                        //(result.SimilarPromotion, cookies) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies, cts.Token);
+                        (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;
+                    }
                     return result;
 
+
                 }, cts.Token);
 
                 var delayTask = Task.Delay(timeout, cts.Token);
@@ -191,85 +245,6 @@ namespace molilian.core
                 });
             }
 
-
-            //try
-            //{
-            //    var account = TkPoolCore.GetOne(TkPoolCore.TkAction.promotionQuery);
-            //    if (account == null)
-            //    {
-            //        result.success = false;
-            //        result.message = "放弃转链";
-            //        result.reason = "没有匹配账号";
-            //        //_ = TkLogCore.CouponLogAsync(result);
-            //        return new APIResult(new
-            //        {
-            //            result.success,
-            //            message = "没有优惠券",
-            //            channel = result.channel.ToString(),
-            //        });
-            //    }
-            //    result.accountId = account.id;
-            //    result.accountName = account.name;
-            //    alimama = new AlimamaPlus(account);
-            //    result = await alimama.PromotionQueryAsync(result);
-
-            //    if (result.success && result.couponAmount == 0)
-            //    {
-            //        result.success = false;
-            //        result.message = "没有优惠券";
-            //        result.reason = "没有优惠券";
-            //    }
-            //}
-            //catch (Exception ex)
-            //{
-            //    if (ex.Message.Contains("was canceled"))
-            //    {
-            //        result.success = false;
-            //        result.message = "放弃转链";
-            //        result.reason = "请求超时";
-            //    }
-            //    else
-            //    {
-            //        _ = new LoggerLibrary("unionCoupon", "tb_error")
-            //            .Info(ip, oaid)
-            //            .Info(content)
-            //            .Info(ex.Message, ex.StackTrace)
-            //            .SaveAsync();
-
-            //        NotifyCore.Notify(new NifyMessage
-            //        {
-            //            message = $"【转链异常TB】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
-            //            priority = NifyMessagePriority.high,
-            //            tags = ["red_circle"]
-            //        });
-            //        result.success = false;
-            //        result.message = "放弃转链";
-            //        result.reason = "转链接口异常";
-            //    }
-            //}
-            //_ = TkLogCore.CouponLogAsync(result, alimama);
-            //if (!result.success)
-            //{
-            //    return new APIResult(new
-            //    {
-            //        result.success,
-            //        message = "没有优惠券",
-            //        channel = result?.channel.ToString(),
-            //    });
-            //}
-            //int couponAmount = (int)result.couponAmount * 100;
-
-            //return new APIResult(new
-            //{
-            //    result.success,
-            //    result.message,
-            //    channel = result?.channel.ToString(),
-            //    result?.itemId,
-            //    result?.deeplink_url,
-            //    result?.pic,
-            //    result?.couponEffectiveStartTime,
-            //    result?.couponEffectiveEndTime,
-            //});
         }
 
     }

+ 3 - 0
molilian.core/DTO/alimama/PromotionQueryDTO.cs

@@ -21,6 +21,9 @@ namespace molilian.core
         public string promotionImg { get; set; } = string.Empty;
 
         public int elapsedTime { get; set; } = 0;
+        public int elapsedTime2 { get; set; } = 0;
+        public int elapsedTime3 { get; set; } = 0;
+        public int elapsedTime4 { get; set; } = 0;
         public DateTime create_time { get; set; } = DateTime.Now;
         public string end_point { get; set; } = string.Empty;
 

+ 1 - 19
molilian.core/Plus/Alimama/crawler.cs

@@ -1,23 +1,5 @@
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc.Controllers;
-using Microsoft.AspNetCore.Mvc.Filters;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using dodohold.core;
-using Dataoke;
-using System.Text.RegularExpressions;
-using Org.BouncyCastle.Ocsp;
-using System.Diagnostics;
-using TencentCloud.Fmu.V20191213.Models;
-using Microsoft.VisualBasic;
+using dodohold.core;
 using System.Text.Json;
-using static System.Runtime.InteropServices.JavaScript.JSType;
-using TencentCloud.Cdn.V20180606.Models;
-using System.Net;
-using TencentCloud.Teo.V20220901.Models;
-using MySqlX.XDevAPI;
 
 
 namespace molilian.core

+ 13 - 5
molilian.core/Plus/TaobaoOpen/TaobaoOpenPlus.cs

@@ -1,4 +1,5 @@
 using dodohold.core;
+using System.Net;
 using System.Text.Json;
 using System.Xml;
 using TencentCloud.Lighthouse.V20200324.Models;
@@ -50,6 +51,7 @@ namespace molilian.core
         }
 
 
+
         public TbkThorMfrsPromotionQueryResponse PromotionQueryRequest(string base64, string oaid)
         {
             ITopClient client = new DefaultTopClient(_server_url, _app_key, _app_secret, "json");
@@ -71,17 +73,23 @@ namespace molilian.core
 
 
 
-        public async Task<(List<PromotionQueryItemDTO>, string)> GetRecommendDataWithRetry(RecommendRequestType type, string oaid,
-           string cookies, CancellationToken cancellationToken = default)
+        public async Task<(List<PromotionQueryItemDTO>, string, int)> GetRecommendDataWithRetry(
+            RecommendRequestType type, string oaid,
+           string cookies,
+           CancellationToken cancellationToken = default)
         {
+            DateTime stime = DateTime.Now;
             // 假设传入了一个 CancellationToken cancellationToken 和 int maxRetries 表示最大重试次数
             int retryCount = 0;
-            int maxRetries = 10;
+            int maxRetries = 50;
             while (true)
             {
                 try
                 {
-                    return await GetRecommendData(type, oaid, cookies, cancellationToken);
+                    (var data, string cookie) = await GetRecommendData(type, oaid, cookies, cancellationToken);
+                    var ts = DateTime.Now - stime;
+                    int elapsedTime = (int)ts.TotalMilliseconds;
+                    return (data, cookie, elapsedTime);
                 }
                 catch (Exception ex)
                 {
@@ -90,7 +98,7 @@ namespace molilian.core
                         throw;
                     }
                     retryCount++;
-                    Thread.Sleep(400);
+                    Thread.Sleep(100);
                 }
             }
 

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini