| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087 |
- using dodohold.core;
- using System.Text.RegularExpressions;
- using System.Diagnostics;
- using System.Text.Json;
- using TencentCloud.Fmu.V20191213.Models;
- using System;
- using MySqlX.XDevAPI;
- using static dodohold.core.ZTOExpress.CreateOrderArgs;
- using System.Threading;
- using TencentCloud.Tcm.V20210413.Models;
- namespace molilian.core
- {
- public partial class AlimamaPlus
- {
- public async Task<TkDataDTO> testTaskAsync()
- {
- string url = "https://www.taobao.com";
- TkDataDTO result = new TkDataDTO();
- WebClientUtility client = new WebClientUtility();
- client.Timeout = TimeSpan.FromMilliseconds(100);
- //var requestTask = alimama.UnionParse2Async(content, result, cts.Token, swData);
- //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 = "请求超时";
- // result.itemName = "点击打开淘宝APP";
- //}
- return null;
- }
- public void UnionParse2(string content, ref TkDataDTO result)
- {
- if ("veapi".Equals(_api) && _api_id > 0)
- {
- var account = VeapiPoolCore.Get(_api_id);
- if (account != null)
- {
- var core = new VeapiPlus(account.name, account.key, account.sessionKey);
- core.UnionParse(content, ref result);
- return;
- }
- }
- UnionParse(content, ref result);
- }
- public async Task<TkDataDTO> UnionParse2Async(string content, TkDataDTO result, CancellationToken cancellationToken = default, Dictionary<string, long> swData = null)
- {
- if ("veapi".Equals(_api) && _api_id > 0)
- {
- var account = VeapiPoolCore.Get(_api_id);
- if (account != null)
- {
- var core = new VeapiPlus(account.name, account.key, account.sessionKey);
- result = await core.UnionParseAsync(content, result, cancellationToken);
- return result;
- }
- }
- result = await UnionParseAsync(content, result, cancellationToken, swData);
- return result;
- }
- /// <summary>
- /// 提取url里面的id参数
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- static string? ExtractItemId(string url)
- {
- Match match = Regex.Match(url, @"(?:\?|&)id=(\d+)");
- if (match.Success)
- {
- return match.Groups[1].Value;
- }
- return null;
- }
- /// <summary>
- /// 是否淘宝链接
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- private static (bool, string) IsTaobaoUrl(string url)
- {
- if (url.Contains("//a.m.taobao.com/i"))
- {
- //第一种提取方法
- string itemId = url.GetContentPart("//a.m.taobao.com/i", ".htm");
- if (!string.IsNullOrEmpty(itemId))
- {
- string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
- return (true, resultUrl);
- }
- }
- if (url.Contains("//item.taobao.com/item.htm?") ||
- url.Contains("//h5.m.taobao.com/awp/core/detail.htm?") ||
- url.Contains("//new.m.taobao.com/detail.htm?") ||
- url.Contains("//outfliggys.m.taobao.com/app/trip/rx-travel-detail") ||
- url.Contains("//detail.m.tmall.com/item.htm?") ||
- url.Contains("//tmallx.tmall.com/app/tmallx-src/goods-detail") ||
- url.Contains("//detail.tmall.com/item.htm?") ||
- url.Contains("//detail.tmall.hk/hk/item.htm?"))
- {
- string? itemId = ExtractItemId(url);
- if (!string.IsNullOrEmpty(itemId))
- {
- string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
- return (true, resultUrl);
- }
- }
- return (false, url);
- }
- /// <summary>
- /// 排除已知的淘客链接
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- static bool IsAffLink(string url)
- {
- if (!url.StartsWith("https://") || !url.StartsWith("https://")) url = "https://" + url;
- Uri uri = new(url);
- if (uri.Host.Equals("s.click.taobao.com", StringComparison.OrdinalIgnoreCase) ||
- uri.Host.Equals("uland.taobao.com", StringComparison.OrdinalIgnoreCase))
- {
- return true;
- }
- //if (uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase) &&
- // uri.AbsolutePath.StartsWith("/h.") &&
- // string.IsNullOrEmpty(uri.Query) && // No query parameters
- // !url.Contains("?tk=")) // No "tk" parameter
- //{
- // return true;
- //}
- return false;
- }
- /// <summary>
- /// 判断淘客的¥羊角符号
- /// </summary>
- /// <param name="content"></param>
- /// <returns></returns>
- static bool ContainsSpecialFormat(string content)
- {
- // 使用正则表达式匹配内容中是否含有特定格式的字符串
- //string pattern = @"¥[a-zA-Z0-9\s]+¥";
- string pattern = @"[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]*";
- return Regex.IsMatch(content, pattern);
- }
- public TimeSpan GetRequestTimeout(int deduction = 0)
- {
- #if DEBUG
- //return TimeSpan.FromMilliseconds(10 * 1000);
- #endif
- int min = (int)(_config.rt_max * 0.3);
- if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
- int timeout = _config.rt_max;
- if (deduction == 0)
- {
- timeout -= min;
- return TimeSpan.FromMilliseconds(timeout);
- }
- timeout -= deduction;
- if (timeout <= min) return TimeSpan.FromMilliseconds(1);
- return TimeSpan.FromMilliseconds(min);
- }
- /// <summary>
- /// 从http获取跳转的url
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- async Task<(bool, string)> GetDesiredUrlAsync(string url, CancellationToken cancellationToken = default)
- {
- string result;
- try
- {
- Uri uri = new(url);
- if (!uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase)) return (false, "不是淘宝短网址");
- string desiredUrlPattern = "var url = '(.*?)'";
- WebClientUtility client = new()
- {
- Proxy = _proxy,
- UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
- };
- #if DEBUG
- client.Proxy = null;
- #endif
- client.Timeout = GetRequestTimeout(0);
- var response = await client.RequestAsync(url, "GET", cancellationToken);
- var responseBody = response.Body();
- result = responseBody;
- if (response.Successed)
- {
- if (response.ResponseMessage.IsSuccessStatusCode)
- {
- Match match = Regex.Match(responseBody, desiredUrlPattern);
- if (match.Success)
- {
- result = match.Groups[1].Value; // 返回匹配的 URL
- if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
- return (true, result);
- }
- }
- else
- {
- result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
- }
- }
- else
- {
- throw response.ResponseException;
- }
- }
- catch (Exception ex)
- {
- result = $"{ex.Message}\n{ex.StackTrace}";
- }
- return (false, result);
- }
- /// <summary>
- /// 从http获取跳转的url
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- (bool, string) GetDesiredUrl(string url)
- {
- string result;
- try
- {
- Uri uri = new(url);
- if (!uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase)) return (false, "不是淘宝短网址");
- string desiredUrlPattern = "var url = '(.*?)'";
- WebClientUtility client = new()
- {
- Proxy = _proxy,
- UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
- };
- #if DEBUG
- client.Proxy = null;
- #endif
- client.Timeout = GetRequestTimeout(0);
- var response = client.Request(url);
- var responseBody = response.Body();
- result = responseBody;
- if (response.Successed)
- {
- if (response.ResponseMessage.IsSuccessStatusCode)
- {
- Match match = Regex.Match(responseBody, desiredUrlPattern);
- if (match.Success)
- {
- result = match.Groups[1].Value; // 返回匹配的 URL
- if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
- return (true, result);
- }
- }
- else
- {
- result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
- }
- }
- else
- {
- throw response.ResponseException;
- }
- }
- catch (Exception ex)
- {
- result = $"{ex.Message}\n{ex.StackTrace}";
- }
- return (false, result);
- }
- private void InternalTextProcessing(string content, ref TkDataDTO result)
- {
- //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result);
- bool success;
- string resultText;
- LinkTypeEnum link_type = LinkTypeEnum.unknown;
- string url = GetLink(content);
- if (!string.IsNullOrEmpty(url))
- {
- //排除已知的淘客链接
- if (IsAffLink(url))
- {
- result.success = false;
- result.content = "排除淘客链接";
- result.link_type = LinkTypeEnum.other_aff;
- result.shortLinkurl = url;
- return;
- //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
- }
- //直接提取
- (success, resultText) = IsTaobaoUrl(url);
- if (success)
- {
- result.success = true;
- result.content = resultText;
- result.link_type = LinkTypeEnum.goods;
- result.shortLinkurl = resultText;
- return;
- //return (true, result, LinkTypeEnum.goods, result);
- }
- //从http获取跳转的url
- Stopwatch stopwatch = Stopwatch.StartNew();
- stopwatch.Start();
- (success, resultText) = GetDesiredUrl(url);
- stopwatch.Stop();
- // 获取执行时间
- result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
- if (success)
- {
- //排除已知的淘客链接
- if (IsAffLink(resultText))
- {
- result.success = false;
- result.content = "排除淘客链接";
- result.link_type = LinkTypeEnum.other_aff;
- result.shortLinkurl = url;
- return;
- //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
- }
- (success, resultText) = IsTaobaoUrl(resultText);
- if (success)
- {
- result.success = true;
- result.content = resultText;
- result.link_type = LinkTypeEnum.goods;
- result.shortLinkurl = resultText;
- return;
- //return (true, resultText, LinkTypeEnum.goods, resultText);
- }
- link_type = GetLinkType(resultText);
- }
- else
- {
- link_type = GetLinkType(url);
- }
- result.success = false;
- result.content = resultText;
- result.link_type = link_type;
- result.shortLinkurl = url;
- return;
- //return (false, result, link_type, url);
- }
- //判断淘客的¥羊角符号
- if (ContainsSpecialFormat(content))
- {
- result.success = false;
- result.content = "排除淘口令";
- result.link_type = LinkTypeEnum.other_aff;
- result.shortLinkurl = null;
- return;
- //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
- }
- //todo 临时应急 0614
- //return (false, "纯口令 没链接", link_type, null);
- result.success = false;
- result.content = "纯口令 没链接";
- result.link_type = LinkTypeEnum.other_aff;
- result.shortLinkurl = null;
- }
- private async Task<TkDataDTO> InternalTextProcessingAsync(string content, TkDataDTO result,
- CancellationToken cancellationToken,
- Dictionary<string, long> swData = null)
- {
- Stopwatch stopwatch = Stopwatch.StartNew();
- bool success;
- string resultText;
- LinkTypeEnum link_type = LinkTypeEnum.unknown;
- if (swData != null)
- {
- stopwatch.Reset();
- stopwatch.Start();
- }
- string url = GetLink(content);
- if (swData != null)
- {
- stopwatch.Stop();
- swData.Add("\tGetLink", stopwatch.ElapsedMilliseconds);
- }
- if (!string.IsNullOrEmpty(url))
- {
- if (swData != null)
- {
- stopwatch.Reset();
- stopwatch.Start();
- }
- bool is_aff = IsAffLink(url);
- if (swData != null)
- {
- stopwatch.Stop();
- swData.Add("\t\tGetLink", stopwatch.ElapsedMilliseconds);
- }
- if (is_aff)
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "其他推广链接";
- result.link_type = LinkTypeEnum.other_aff;
- result.shortLinkurl = url;
- return result;
- //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
- }
- //直接提取
- if (swData != null)
- {
- stopwatch.Reset();
- stopwatch.Start();
- }
- (success, resultText) = IsTaobaoUrl(url);
- if (swData != null)
- {
- stopwatch.Stop();
- swData.Add("\tIsTaobaoUrl", stopwatch.ElapsedMilliseconds);
- }
- if (success)
- {
- result.success = true;
- result.content = resultText;
- result.link_type = LinkTypeEnum.goods;
- result.shortLinkurl = resultText;
- return result;
- //return (true, result, LinkTypeEnum.goods, result);
- }
- Stopwatch sw = Stopwatch.StartNew();
- (success, resultText) = await GetDesiredUrlAsync(url, cancellationToken);
- sw.Stop();
- // 获取执行时间
- result.elapsedTime2 = (int)sw.ElapsedMilliseconds;
- if (swData != null)
- {
- swData.Add("\tGetDesiredUrlAsync", sw.ElapsedMilliseconds);
- }
- if (result.elapsedTime2 >= _config.rt_max)
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "请求超时";
- result.itemName = "点击打开淘宝APP";
- return result;
- //cancellationToken.ThrowIfCancellationRequested();
- }
- if (success)
- {
- //排除已知的淘客链接
- if (swData != null)
- {
- stopwatch.Reset();
- stopwatch.Start();
- }
- bool is_aff2 = IsAffLink(resultText);
- if (swData != null)
- {
- stopwatch.Stop();
- swData.Add("\t\tIsAffLink", stopwatch.ElapsedMilliseconds);
- }
- if (is_aff2)
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "其他推广链接";
- result.link_type = LinkTypeEnum.other_aff;
- result.shortLinkurl = url;
- return result;
- //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
- }
- if (swData != null)
- {
- stopwatch.Reset();
- stopwatch.Start();
- }
- (success, resultText) = IsTaobaoUrl(resultText);
- if (swData != null)
- {
- stopwatch.Stop();
- swData.Add("\t\tIsTaobaoUrl", stopwatch.ElapsedMilliseconds);
- }
- if (success)
- {
- result.success = true;
- result.content = resultText;
- result.link_type = LinkTypeEnum.goods;
- result.shortLinkurl = resultText;
- return result;
- //return (true, resultText, LinkTypeEnum.goods, resultText);
- }
- link_type = GetLinkType(resultText);
- }
- else
- {
- link_type = GetLinkType(url);
- }
- result.success = false;
- result.content = resultText;
- result.link_type = link_type;
- result.shortLinkurl = url;
- return result;
- //return (false, result, link_type, url);
- }
- if (swData != null)
- {
- stopwatch.Reset();
- stopwatch.Start();
- }
- bool is_aff3 = ContainsSpecialFormat(content);
- if (swData != null)
- {
- stopwatch.Stop();
- swData.Add("\tContainsSpecialFormat", stopwatch.ElapsedMilliseconds);
- }
- //判断淘客的¥羊角符号
- if (is_aff3)
- {
- result.success = false;
- result.content = "排除淘口令";
- result.link_type = LinkTypeEnum.other_aff;
- result.shortLinkurl = null;
- return result;
- //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
- }
- //todo 临时应急 0614
- //return (false, "纯口令 没链接", link_type, null);
- result.success = false;
- result.content = "纯口令 没链接";
- result.link_type = LinkTypeEnum.other_aff;
- result.shortLinkurl = null;
- return result;
- }
- private (bool, string, LinkTypeEnum, string) InternalTextProcessing222(string content)
- {
- bool success;
- string result;
- LinkTypeEnum link_type = LinkTypeEnum.unknown;
- string url = GetLink(content);
- if (!string.IsNullOrEmpty(url))
- {
- //排除已知的淘客链接
- if (IsAffLink(url)) return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
- //直接提取
- (success, result) = IsTaobaoUrl(url);
- if (success) return (true, result, LinkTypeEnum.goods, result);
- //https://uland.taobao.com/coupon/edetail?af=4&e=%2B0aTAvEt16MoRnbUfHBdCarU9683KT3AsB67n92UtFJMmdsrkidbOWBzzpT26idJbwZX%2BHY%2B8QD9zJXNeOSc1euXSPKeLUBBWr02LqsxPV8Bf6pSMNy5GSw6iqMObXLLk3toOSlQXmUUcLJJYP0Uq5lvDADUDTeHRiuxU0i9W%2BtrC8Oh6piEfdwyUIuKQbeM8rUgQejRG616MoRnbUfHBdCarU9683KT3AsB67n92UtFv6uIFQszbmWWPZ8ka7%2BkV5xXydLt%2FcnuZ4%2BBFYnwwiUaCWC3TckcCwMW8xe3XqOGsb8uv9wbhxdkKJ0hxTrTkj7DNgliTXDluAYBRglsbQ%3D%3D&traceId=212cbaa417178512249145009e05a8&union_lens=lensId%3APUB%401717851219%402103c04a_0c89_18ff7e96612_9d39%40031w5YJSc7jl3U7gYjdvuCMM%40eyJmbG9vcklkIjo4NTAvEt16MoRnbUfHBdCarU9683JDaxNfPy83okP3kLScwCkGiuMcyC4PcynGV0YWlsX2h0bSIsInNyY0Zsb29ySWQiiOiiI4MDY3NCIsImNyZWF0aXZpdHlHcm91cElkIjowfQieie%3BtkScm%3AselectionPlaza_site_4358%3Bscm%3A1007.30148.329090.pub_search-item_c02df4bb-631f-4cc1-a9d5-abe802b8e134_&un=bfc196d23e155d289f2917200ca32a93&share_crt_v=1&un_site=0&ut_sk=1.utdid_null_1717851225204.TaoPassword-Outside.lianmeng-app&spm=a2159r.13376465.0.0&sp_tk=d2hLeFdDNHpKb3Y%3D&cpp=1&shareurl=true&short_name=h.gVx6mvI
- //从http获取跳转的url
- (success, result) = GetDesiredUrl(url);
- if (success)
- {
- //排除已知的淘客链接
- if (IsAffLink(result)) return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
- (success, result) = IsTaobaoUrl(result);
- if (success) return (true, result, LinkTypeEnum.goods, result);
- link_type = GetLinkType(result);
- }
- else
- {
- link_type = GetLinkType(url);
- }
- return (false, result, link_type, url);
- }
- //判断淘客的¥羊角符号
- if (ContainsSpecialFormat(content)) return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
- //todo 临时应急 0614
- //return (false, "纯口令 没链接", link_type, null);
- return (false, "纯口令 没链接", LinkTypeEnum.other_aff, null);
- }
- private LinkTypeEnum GetLinkType(string url)
- {
- if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
- if (url.StartsWith("https://huodong.m.taobao.com/act/talent/live.html")) return LinkTypeEnum.live;
- if (url.StartsWith("https://web.m.taobao.com/app/tnode/web/index")) return LinkTypeEnum.video;
- string pattern = @"^https://shop\d+\.m\.taobao\.com";
- if (Regex.IsMatch(url, pattern))
- {
- return LinkTypeEnum.profile;
- }
- return LinkTypeEnum.unknown;
- }
- private async Task<TkDataDTO> alimamaParseAsync(string content, TkDataDTO result, CancellationToken cancellationToken = default)
- {
- string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
- Random random = new();
- double randomNumber = random.NextDouble();
- string randomString = randomNumber.ToString()[2..];
- string cna = _cookies.GetContentPart("cna=", ";");
- string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
- var variableMap = new
- {
- url = content,
- union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
- lensScene = "PUB",
- spmB = "_portal_v2_tool_links_page_home_index_htm"
- }.Convert2Json(true).UrlEncode();
- variableMap = variableMap.Replace(" ", "%20");
- string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
- $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
- Stopwatch stopwatch = Stopwatch.StartNew();
- stopwatch.Start();
- var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
- .AddHeaders("X-Requested-With", "XMLHttpRequest")
- .AddHeaders("Cookie", _cookies);
- client.Proxy = _proxy;
- #if DEBUG
- client.Proxy = null;
- #endif
- if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
- client.Timeout = GetRequestTimeout(result.elapsedTime2);
- //var response = client.Request(url);
- var response = await client.RequestAsync(url, "GET", cancellationToken);
- stopwatch.Stop();
- result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
- var body = string.Empty;
- try
- {
- if (!response.Successed)
- {
- result.channel = TkChannelEnum.tb;
- result.accountId = _accountId;
- result.accountName = _accountName;
- result.link_type = LinkTypeEnum.unknown;
- result.success = false;
- result.message = "fail";
- if (response.ResponseException != null)
- {
- _ = new LoggerLibrary("api_error", "fail")
- .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
- .SaveAsync();
- throw response.ResponseException;
- }
- return result;
- }
- if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.Found)
- {
- _ = new LoggerLibrary("api_error", "fail")
- .Info($"302:{response.ResponseMessage.Headers.Location}")
- .SaveAsync();
- throw new Exception("302 Found, was canceled");
- }
- JsonElement root;
- try
- {
- body = response.Body();
- root = body.Convert2JsonElement();
- }
- catch (Exception ex)
- {
- throw new Exception(body);
- }
- bool success = root.Read<bool>("success", false);
- string message = root.Read("message", string.Empty);
- string info_message = root.PathRead("info.message", string.Empty);
- if (!string.IsNullOrEmpty(info_message)) message = info_message;
- string taoToken = root.PathRead("data.taoToken", string.Empty);
- string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
- string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
- string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
- decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
- string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
- string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
- string itemId = root.PathRead("data.itemId", string.Empty);
- string itemName = root.PathRead("data.itemName", string.Empty);
- decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
- string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
- string shopTitle = root.PathRead("data.shopTitle", string.Empty);
- string pic = root.PathRead("data.pic", string.Empty);
- string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
- if (response.ResponseMessage != null)
- {
- switch (response.ResponseMessage.StatusCode)
- {
- case System.Net.HttpStatusCode.OK:
- {
- if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
- {
- taoToken = couponLinkTaoToken;
- shortLinkurl = couponShortLinkUrl;
- }
- string shortLink = GetLink(taoToken);
- content = ReplaceUrls(content, shortLink);
- }
- break;
- case System.Net.HttpStatusCode.Found:
- string location = response.ResponseMessage.Headers.Location.OriginalString;
- if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
- {
- success = false;
- message = "nologin";
- }
- break;
- default:
- {
- success = false;
- message = "other";
- }
- break;
- }
- }
- if (!success)
- {
- shortLinkurl = GetLink(content);
- switch (message)
- {
- case "该商品已经下架或未加入淘宝客":
- case "该链接不支持转化,请更换链接尝试":
- case "网络错误":
- break;
- default:
- var _headers = response.ResponseMessage.Headers;
- var headers = "";
- foreach (var h in _headers)
- {
- foreach (var Value in h.Value)
- {
- headers += $"{h.Key}: {Value}\n";
- }
- }
- _ = new LoggerLibrary("api_error", "fail")
- .Info(message, content)
- .Info(headers, body)
- .SaveAsync();
- break;
- }
- }
- string deeplink_url = GetDeeplink(shortLinkurl);
- result.channel = TkChannelEnum.tb;
- result.accountId = _accountId;
- result.accountName = _accountName;
- result.link_type = success ? LinkTypeEnum.goods : result.link_type;
- result.success = success;
- result.message = message;
- result.content = content;
- result.couponAmount = couponAmount;
- result.couponEffectiveEndTime = couponEffectiveEndTime;
- result.couponEffectiveStartTime = couponEffectiveStartTime;
- result.taoToken = taoToken;
- result.shortLinkurl = shortLinkurl;
- result.deeplink_url = deeplink_url;
- result.itemId = itemId;
- result.itemName = itemName;
- result.promotionPrice = promotionPrice;
- result.sellerNickName = sellerNickName;
- result.shopTitle = shopTitle;
- result.pic = pic;
- result.qrCodeUrl = qrCodeUrl;
- }
- catch
- {
- if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
- {
- if (body.Contains("<p>抱歉!页面无法访问……</p>"))
- {
- throw new APIException("抱歉!页面无法访问……");
- }
- }
- throw;
- }
- return result;
- }
- private TkDataDTO alimamaParse(string content, TkDataDTO result)
- {
- string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
- Random random = new();
- double randomNumber = random.NextDouble();
- string randomString = randomNumber.ToString()[2..];
- string cna = _cookies.GetContentPart("cna=", ";");
- string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
- var variableMap = new
- {
- url = content,
- union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
- lensScene = "PUB",
- spmB = "_portal_v2_tool_links_page_home_index_htm"
- }.Convert2Json(true).UrlEncode();
- variableMap = variableMap.Replace(" ", "%20");
- string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
- $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
- Stopwatch stopwatch = Stopwatch.StartNew();
- stopwatch.Start();
- var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
- .AddHeaders("X-Requested-With", "XMLHttpRequest")
- .AddHeaders("Cookie", _cookies);
- client.Proxy = _proxy;
- if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
- #if DEBUG
- client.Proxy = null;
- url = "https://47.246.177.226/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
- $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
- client.Headers["Host"] = "pub.alimama.com";
- #endif
- client.Timeout = GetRequestTimeout(result.elapsedTime2);
- //var response = client.Request(url);
- var response = client.Request(url);
- stopwatch.Stop();
- result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
- var body = string.Empty;
- try
- {
- if (!response.Successed)
- {
- result.channel = TkChannelEnum.tb;
- result.accountId = _accountId;
- result.accountName = _accountName;
- result.link_type = LinkTypeEnum.unknown;
- result.success = false;
- result.message = "fail";
- if (response.ResponseException != null)
- {
- _ = new LoggerLibrary("api_error", "fail")
- .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
- .SaveAsync();
- throw response.ResponseException;
- }
- return result;
- }
- body = response.Body();
- //{"code":601,"info":{"ok":false,"message":"nologin"}}
- JsonElement root = body.Convert2JsonElement();
- bool success = root.Read<bool>("success", false);
- string message = root.Read("message", string.Empty);
- string info_message = root.PathRead("info.message", string.Empty);
- if (!string.IsNullOrEmpty(info_message)) message = info_message;
- string taoToken = root.PathRead("data.taoToken", string.Empty);
- string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
- string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
- string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
- decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
- string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
- string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
- string itemId = root.PathRead("data.itemId", string.Empty);
- string itemName = root.PathRead("data.itemName", string.Empty);
- decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
- string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
- string shopTitle = root.PathRead("data.shopTitle", string.Empty);
- string pic = root.PathRead("data.pic", string.Empty);
- string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
- if (response.ResponseMessage != null)
- {
- switch (response.ResponseMessage.StatusCode)
- {
- case System.Net.HttpStatusCode.OK:
- {
- if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
- {
- taoToken = couponLinkTaoToken;
- shortLinkurl = couponShortLinkUrl;
- }
- string shortLink = GetLink(taoToken);
- content = ReplaceUrls(content, shortLink);
- }
- break;
- case System.Net.HttpStatusCode.Found:
- string location = response.ResponseMessage.Headers.Location.OriginalString;
- if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
- {
- success = false;
- message = "nologin";
- }
- break;
- default:
- {
- success = false;
- message = "other";
- }
- break;
- }
- }
- if (!success)
- {
- shortLinkurl = GetLink(content);
- switch (message)
- {
- case "该商品已经下架或未加入淘宝客":
- case "该链接不支持转化,请更换链接尝试":
- case "网络错误":
- break;
- default:
- var _headers = response.ResponseMessage.Headers;
- var headers = "";
- foreach (var h in _headers)
- {
- foreach (var Value in h.Value)
- {
- headers += $"{h.Key}: {Value}\n";
- }
- }
- _ = new LoggerLibrary("api_error", "fail")
- .Info(message, content)
- .Info(headers, body)
- .SaveAsync();
- break;
- }
- }
- if (success)
- {
- if (string.IsNullOrEmpty(itemName))
- {
- itemName = GetTitle(taoToken);
- }
- }
- else
- {
- itemName = GetTitle(content);
- }
- string deeplink_url = GetDeeplink(shortLinkurl);
- result.channel = TkChannelEnum.tb;
- result.accountId = _accountId;
- result.accountName = _accountName;
- if (success)
- {
- switch (result.link_type)
- {
- case LinkTypeEnum.profile:
- case LinkTypeEnum.goods:
- break;
- default:
- result.link_type = LinkTypeEnum.goods;
- break;
- }
- }
- result.success = success;
- result.message = message;
- result.content = content;
- result.couponAmount = couponAmount;
- result.couponEffectiveEndTime = couponEffectiveEndTime;
- result.couponEffectiveStartTime = couponEffectiveStartTime;
- result.taoToken = taoToken;
- result.shortLinkurl = shortLinkurl;
- result.deeplink_url = deeplink_url;
- result.itemId = itemId;
- result.itemName = itemName;
- result.promotionPrice = promotionPrice;
- result.sellerNickName = sellerNickName;
- result.shopTitle = shopTitle;
- result.pic = pic;
- result.qrCodeUrl = qrCodeUrl;
- }
- catch
- {
- if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
- {
- if (body.Contains("<p>抱歉!页面无法访问……</p>"))
- {
- throw new APIException("抱歉!页面无法访问……");
- }
- }
- throw;
- }
- return result;
- }
- }
- }
|