using dodohold.core; using System.Text.RegularExpressions; using System.Diagnostics; using System.Text.Json; using TencentCloud.Fmu.V20191213.Models; namespace molilian.core { public partial class AlimamaPlus { 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 UnionParse2Async(string content, 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); result = await core.UnionParseAsync(content, result); return result; } } result = await UnionParseAsync(content, result); return result; } /// /// 提取url里面的id参数 /// /// /// static string? ExtractItemId(string url) { Match match = Regex.Match(url, @"(?:\?|&)id=(\d+)"); if (match.Success) { return match.Groups[1].Value; } return null; } /// /// 是否淘宝链接 /// /// /// 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); } /// /// 排除已知的淘客链接 /// /// /// 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; } /// /// 判断淘客的¥羊角符号 /// /// /// static bool ContainsSpecialFormat(string content) { // 使用正则表达式匹配内容中是否含有特定格式的字符串 //string pattern = @"¥[a-zA-Z0-9\s]+¥"; string pattern = @"[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲]+[a-zA-Z0-9]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲]*"; return Regex.IsMatch(content, pattern); } /// /// 从http获取跳转的url /// /// /// (bool, string) GetDesiredUrl(string url) { string result; try { string desiredUrlPattern = "var url = '(.*?)'"; WebClientUtility client = new() { Proxy = _proxy, UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer }; #if DEBUG client.Proxy = null; #else client.Timeout = TimeSpan.FromMilliseconds(3000); #endif 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 (bool, string, LinkTypeEnum, string) InternalTextProcessing(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); //从http获取跳转的url (success, result) = GetDesiredUrl(url); if (success) { //排除已知的淘客链接 if (IsAffLink(result)) return (false, "排除淘客链接", LinkTypeEnum.other_aff, result); (success, result) = IsTaobaoUrl(result); if (success) return (true, result, LinkTypeEnum.goods, result); } link_type = GetLinkType(url); return (false, result, link_type, url); } //判断淘客的¥羊角符号 if (ContainsSpecialFormat(content)) return (false, "排除淘口令", LinkTypeEnum.other_aff, null); return (false, "纯口令 没链接", link_type, 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 void alimamaParse(string content, ref 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}"; 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 = TimeSpan.FromMilliseconds(1000); var response = client.Request(url); var body = string.Empty; try { if (!response.Successed) { result.channel = TkChannelEnum.tb; 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) .Save(); throw response.ResponseException; } return; } body = response.Body(); JsonElement root = body.Convert2JsonElement(); bool success = root.Read("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("data.couponAmount", 0); string itemId = root.PathRead("data.itemId", string.Empty); string itemName = root.PathRead("data.itemName", string.Empty); decimal promotionPrice = root.PathRead("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 "网络错误": 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) .Save(); break; } } string deeplink_url = GetDeeplink(shortLinkurl); result.channel = TkChannelEnum.tb; 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.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("

抱歉!页面无法访问……

")) { throw new APIException("抱歉!页面无法访问……"); } } throw; } } private async Task alimamaParseAsync(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}"; 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; client.Timeout = TimeSpan.FromMilliseconds(1000); #if DEBUG client.Proxy = null; client.Timeout = TimeSpan.FromMilliseconds(10000); #endif var response = client.Request(url); var body = string.Empty; try { if (!response.Successed) { result.channel = TkChannelEnum.tb; 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) .Save(); throw response.ResponseException; } return result; } body = response.Body(); //{"code":601,"info":{"ok":false,"message":"nologin"}} JsonElement root = body.Convert2JsonElement(); bool success = root.Read("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("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("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 "网络错误": 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) .Save(); break; } } string deeplink_url = GetDeeplink(shortLinkurl); result.channel = TkChannelEnum.tb; 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("

抱歉!页面无法访问……

")) { throw new APIException("抱歉!页面无法访问……"); } } throw; } return result; } } }