parse_2.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. using dodohold.core;
  2. using System.Text.RegularExpressions;
  3. using System.Diagnostics;
  4. using System.Text.Json;
  5. using TencentCloud.Fmu.V20191213.Models;
  6. using System;
  7. using MySqlX.XDevAPI;
  8. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  9. using System.Threading;
  10. using TencentCloud.Tcm.V20210413.Models;
  11. using COSXML.Network;
  12. using System.Net;
  13. using TencentCloud.Cdb.V20170320.Models;
  14. using System.Security.Cryptography;
  15. namespace molilian.core
  16. {
  17. public partial class AlimamaPlus
  18. {
  19. public static async Task<TkDataDTO> testTaskAsync(TkDataDTO result, CancellationToken token)
  20. {
  21. string url = "https://www.taobao.com";
  22. try
  23. {
  24. WebClientUtility client = new WebClientUtility();
  25. await Task.Delay(5000);
  26. client.Timeout = TimeSpan.FromMilliseconds(3000);
  27. var response = await client.RequestAsync(url);
  28. var body = response.Body();
  29. }
  30. catch
  31. {
  32. result.success = false;
  33. result.message = "Error";
  34. }
  35. return result;
  36. }
  37. /// <summary>
  38. /// 提取url里面的id参数
  39. /// </summary>
  40. /// <param name="url"></param>
  41. /// <returns></returns>
  42. public static string? ExtractItemId(string url)
  43. {
  44. Match match = Regex.Match(url, @"(?:\?|&)id=(\d+)");
  45. if (match.Success)
  46. {
  47. return match.Groups[1].Value;
  48. }
  49. return null;
  50. }
  51. /// <summary>
  52. /// 是否淘宝链接,如果是商品链接就格式化成https://item.taobao.com/item.htm?id=****
  53. /// </summary>
  54. /// <param name="url"></param>
  55. /// <returns></returns>
  56. public static (bool, string) IsTaobaoUrl(string url)
  57. {
  58. if (url.Contains("//a.m.taobao.com/i") ||
  59. url.Contains("//internal.tt.detail.taobao.com/i"))
  60. {
  61. //第一种提取方法
  62. string itemId = url.GetContentPart(".com/i", ".htm");
  63. if (!string.IsNullOrEmpty(itemId) && long.TryParse(itemId, out long _))
  64. {
  65. string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
  66. return (true, resultUrl);
  67. }
  68. }
  69. //https://pages-g.m.taobao.com/wow/z/app/detail-next/item/index?x-ssr=true&id=753169018016
  70. if (url.Contains("//item.taobao.com/item.htm?") ||
  71. url.Contains("//pages-g.m.taobao.com/wow/z/app/detail-next/item/index?") ||
  72. url.Contains("//main.m.taobao.com/detail/index.html?") ||
  73. url.Contains("//main.m.taobao.com/security-h5-detail/home?") ||
  74. url.Contains("//h5.m.taobao.com/awp/core/detail.htm?") ||
  75. url.Contains("//new.m.taobao.com/detail.htm?") ||
  76. url.Contains("//outfliggys.m.taobao.com/app/trip/rx-travel-detail") ||
  77. url.Contains("//detail.m.tmall.com/item.htm?") ||
  78. url.Contains("//tmallx.tmall.com/app/tmallx-src/goods-detail") ||
  79. url.Contains("//detail.tmall.com/item.htm?") ||
  80. url.Contains("//internal.tt.detail.taobao.com/item.htm?") ||
  81. url.Contains("//internal.tt.detail.taobao.com/") ||
  82. url.Contains("//detail.tmall.hk/hk/item.htm?"))
  83. {
  84. string? itemId = ExtractItemId(url);
  85. if (!string.IsNullOrEmpty(itemId))
  86. {
  87. string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
  88. return (true, resultUrl);
  89. }
  90. }
  91. return (false, url);
  92. }
  93. /// <summary>
  94. /// 排除已知的淘客链接
  95. /// </summary>
  96. /// <param name="url"></param>
  97. /// <returns></returns>
  98. public static bool IsAffLink(string url)
  99. {
  100. if (!url.StartsWith("https://") || !url.StartsWith("https://")) url = "https://" + url;
  101. Uri uri = new(url);
  102. if (uri.Host.Equals("s.click.taobao.com", StringComparison.OrdinalIgnoreCase) ||
  103. uri.Host.Equals("uland.taobao.com", StringComparison.OrdinalIgnoreCase))
  104. {
  105. return true;
  106. }
  107. return false;
  108. }
  109. public static bool IsShortLink(string url)
  110. {
  111. if (!url.StartsWith("https://") || !url.StartsWith("https://")) url = "https://" + url;
  112. Uri uri = new(url);
  113. if (uri.Host.Equals("e.tb.cn", StringComparison.OrdinalIgnoreCase) ||
  114. uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase) ||
  115. uri.Host.Equals("u.tb.cn", StringComparison.OrdinalIgnoreCase) ||
  116. uri.Host.Equals("s.tb.cn", StringComparison.OrdinalIgnoreCase))
  117. {
  118. return true;
  119. }
  120. return false;
  121. }
  122. /// <summary>
  123. /// 判断淘客的¥羊角符号
  124. /// </summary>
  125. /// <param name="content"></param>
  126. /// <returns></returns>
  127. static bool ContainsSpecialFormat(string content)
  128. {
  129. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  130. //string pattern = @"¥[a-zA-Z0-9\s]+¥";
  131. string pattern = @"[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]*";
  132. return Regex.IsMatch(content, pattern);
  133. }
  134. public TimeSpan GetRequestTimeout()
  135. {
  136. int min = (int)(_config.rt_max * 0.5);
  137. if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
  138. int timeout = _config.rt_max;
  139. timeout -= min;
  140. return TimeSpan.FromMilliseconds(timeout);
  141. }
  142. public TimeSpan GetRequestTimeout2(int deduction = 0)
  143. {
  144. int min = (int)(_config.rt_max * 0.5);
  145. if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
  146. int timeout = _config.rt_max;
  147. timeout -= deduction;
  148. if (timeout <= min) return TimeSpan.FromMilliseconds(min);
  149. return TimeSpan.FromMilliseconds(timeout);
  150. }
  151. /// <summary>
  152. /// 从http获取跳转的url
  153. /// </summary>
  154. /// <param name="url"></param>
  155. /// <returns></returns>
  156. public async Task<(bool, string)> GetDesiredUrlAsync(WebProxy proxy, string url, CancellationToken cancellationToken = default)
  157. {
  158. string result;
  159. try
  160. {
  161. if (!IsShortLink(url)) return (false, "不是淘宝短网址");
  162. if (!_account.enable_deep_url) return (true, url);
  163. string desiredUrlPattern = "var url = '(.*?)'";
  164. WebClientUtility client = new()
  165. {
  166. Proxy = proxy,
  167. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  168. };
  169. if (url.Contains("u.tb.cn"))
  170. {
  171. client.AllowAutoRedirect = false;
  172. }
  173. url = url.Replace("http://u.tb.cn", "https://u.tb.cn");
  174. #if DEBUG
  175. client.Proxy = null;
  176. var response = await client.RequestAsync(url, "GET");
  177. #else
  178. client.Timeout = GetRequestTimeout();
  179. var response = await client.RequestAsync(url, "GET", cancellationToken);
  180. #endif
  181. if (response.ResponseMessage?.StatusCode == HttpStatusCode.Found &&
  182. !string.IsNullOrEmpty(response.ResponseMessage?.Headers?.Location?.OriginalString))
  183. {
  184. string redirectUrl = response.ResponseMessage?.Headers?.Location?.OriginalString;
  185. return (true, redirectUrl);
  186. }
  187. var responseBody = response.Body();
  188. result = responseBody;
  189. if (response.Successed)
  190. {
  191. if (response.ResponseMessage.IsSuccessStatusCode)
  192. {
  193. Match match = Regex.Match(responseBody, desiredUrlPattern);
  194. if (match.Success)
  195. {
  196. result = match.Groups[1].Value; // 返回匹配的 URL
  197. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  198. if (result.StartsWith("//"))
  199. result = "https:" + result;
  200. return (true, result);
  201. }
  202. }
  203. else
  204. {
  205. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  206. }
  207. }
  208. else
  209. {
  210. throw response.ResponseException;
  211. }
  212. }
  213. catch (Exception ex)
  214. {
  215. _ = new LoggerLibrary("api_error", "desiredUrl_error").Info(ex.Message, ex.StackTrace).SaveAsync();
  216. result = "网络异常";
  217. }
  218. return (false, result);
  219. }
  220. /// <summary>
  221. /// 从http获取跳转的url
  222. /// </summary>
  223. /// <param name="url"></param>
  224. /// <returns></returns>
  225. (bool, string) GetDesiredUrl(string url)
  226. {
  227. string result;
  228. try
  229. {
  230. if (!IsShortLink(url)) return (false, "不是淘宝短网址");
  231. string desiredUrlPattern = "var url = '(.*?)'";
  232. WebClientUtility client = new()
  233. {
  234. Proxy = ProxyNodesCore.RandomOne(_account.nodeName),
  235. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  236. };
  237. #if DEBUG
  238. client.Proxy = null;
  239. #endif
  240. client.Timeout = GetRequestTimeout();
  241. var response = client.Request(url);
  242. var responseBody = response.Body();
  243. result = responseBody;
  244. if (response.Successed)
  245. {
  246. if (response.ResponseMessage.IsSuccessStatusCode)
  247. {
  248. Match match = Regex.Match(responseBody, desiredUrlPattern);
  249. if (match.Success)
  250. {
  251. result = match.Groups[1].Value; // 返回匹配的 URL
  252. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  253. return (true, result);
  254. }
  255. }
  256. else
  257. {
  258. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  259. }
  260. }
  261. else
  262. {
  263. throw response.ResponseException;
  264. }
  265. }
  266. catch (Exception ex)
  267. {
  268. result = $"{ex.Message}\n{ex.StackTrace}";
  269. }
  270. return (false, result);
  271. }
  272. private void InternalTextProcessing(string content, ref TkDataDTO result)
  273. {
  274. //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result);
  275. bool success;
  276. string resultText;
  277. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  278. string url = GetLink(content);
  279. if (!string.IsNullOrEmpty(url))
  280. {
  281. //排除已知的淘客链接
  282. if (IsAffLink(url))
  283. {
  284. result.success = false;
  285. result.content = "排除淘客链接";
  286. result.link_type = LinkTypeEnum.other_aff;
  287. result.shortLinkurl = url;
  288. return;
  289. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  290. }
  291. //直接提取
  292. (success, resultText) = IsTaobaoUrl(url);
  293. if (success)
  294. {
  295. result.success = true;
  296. result.content = resultText;
  297. result.link_type = LinkTypeEnum.goods;
  298. result.shortLinkurl = resultText;
  299. return;
  300. //return (true, result, LinkTypeEnum.goods, result);
  301. }
  302. //从http获取跳转的url
  303. Stopwatch stopwatch = Stopwatch.StartNew();
  304. stopwatch.Start();
  305. (success, resultText) = GetDesiredUrl(url);
  306. stopwatch.Stop();
  307. // 获取执行时间
  308. result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
  309. if (success)
  310. {
  311. //排除已知的淘客链接
  312. if (IsAffLink(resultText))
  313. {
  314. result.success = false;
  315. result.content = "排除淘客链接";
  316. result.link_type = LinkTypeEnum.other_aff;
  317. result.shortLinkurl = url;
  318. return;
  319. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  320. }
  321. (success, resultText) = IsTaobaoUrl(resultText);
  322. if (success)
  323. {
  324. result.success = true;
  325. result.content = resultText;
  326. result.link_type = LinkTypeEnum.goods;
  327. result.shortLinkurl = resultText;
  328. return;
  329. //return (true, resultText, LinkTypeEnum.goods, resultText);
  330. }
  331. link_type = GetLinkType(resultText);
  332. }
  333. else
  334. {
  335. link_type = GetLinkType(url);
  336. }
  337. result.success = false;
  338. result.content = resultText;
  339. result.link_type = link_type;
  340. result.shortLinkurl = url;
  341. return;
  342. //return (false, result, link_type, url);
  343. }
  344. //判断淘客的¥羊角符号
  345. if (ContainsSpecialFormat(content))
  346. {
  347. result.success = false;
  348. result.content = "排除淘口令";
  349. result.link_type = LinkTypeEnum.other_aff;
  350. result.shortLinkurl = null;
  351. return;
  352. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  353. }
  354. //todo 临时应急 0614
  355. //return (false, "纯口令 没链接", link_type, null);
  356. result.success = false;
  357. result.content = "纯口令 没链接";
  358. result.link_type = LinkTypeEnum.other_aff;
  359. result.shortLinkurl = null;
  360. }
  361. public async Task<TkDataDTO> InternalTextProcessingAsync(string content, TkDataDTO result,
  362. CancellationToken cancellationToken = default,
  363. Dictionary<string, long> swData = null)
  364. {
  365. bool success;
  366. string resultText;
  367. result.link_type = LinkTypeEnum.unknown;
  368. //============================== 放弃转链-其他推广链接 ==============================
  369. string url = GetLink(content);
  370. result.shortLinkurl = url;
  371. if (!string.IsNullOrEmpty(url))
  372. {
  373. bool is_aff = IsAffLink(url);
  374. if (is_aff)
  375. {
  376. result.success = false;
  377. result.message = "放弃转链";
  378. result.reason = "其他推广链接";
  379. result.subCode = TkSubCodeEnum.OtherPromo;
  380. result.link_type = LinkTypeEnum.other_aff;
  381. //result.shortLinkurl = url;
  382. return result;
  383. }
  384. (success, resultText) = IsTaobaoUrl(url);
  385. if (success)
  386. {
  387. result.success = true;
  388. result.content = resultText;
  389. result.link_type = LinkTypeEnum.goods;
  390. //result.shortLinkurl = resultText;
  391. return result;
  392. }
  393. Stopwatch sw = Stopwatch.StartNew();
  394. var proxy = ProxyNodesCore.RandomOne(_account.nodeName);
  395. if (_proxy != null && proxy != null && _proxy.Address != proxy.Address)
  396. {
  397. result.proxy_node = proxy?.Address?.Authority;
  398. }
  399. (success, resultText) = await GetDesiredUrlAsync(proxy, url, cancellationToken);
  400. sw.Stop();
  401. // 获取执行时间
  402. result.elapsedTime2 = (int)sw.ElapsedMilliseconds;
  403. swData?.Add("\tGetDesiredUrlAsync", sw.ElapsedMilliseconds);
  404. //============================== 放弃转链-请求超时 ==============================
  405. #if DEBUG
  406. #else
  407. if (result.elapsedTime2 >= _config.rt_max || (!success && resultText.Contains("was canceled")))
  408. {
  409. result.success = false;
  410. result.message = "放弃转链";
  411. result.reason = "请求超时";
  412. result.subCode = TkSubCodeEnum.Other;
  413. return result;
  414. }
  415. #endif
  416. if (success)
  417. {
  418. string desiredUrl = resultText;
  419. //============================== 放弃转链-其他推广链接 ==============================
  420. bool is_aff2 = IsAffLink(desiredUrl);
  421. if (is_aff2)
  422. {
  423. result.success = false;
  424. result.message = "放弃转链";
  425. result.reason = "其他推广链接";
  426. result.subCode = TkSubCodeEnum.OtherPromo;
  427. result.link_type = LinkTypeEnum.other_aff;
  428. //result.shortLinkurl = url;
  429. return result;
  430. }
  431. (success, desiredUrl) = IsTaobaoUrl(desiredUrl);
  432. if (success)
  433. {
  434. result.success = true;
  435. result.content = desiredUrl;
  436. result.link_type = LinkTypeEnum.goods;
  437. //result.shortLinkurl = desiredUrl;
  438. return result;
  439. }
  440. result.link_type = GetLinkType(desiredUrl);
  441. }
  442. else
  443. {
  444. result.link_type = GetLinkType(url);
  445. if ("网络错误".Equals(resultText))
  446. {
  447. result.link_type = LinkTypeEnum.unknown;
  448. result.content = resultText;
  449. result.reason = resultText;
  450. }
  451. }
  452. if (result.content.Contains("霸下通用 web 页面-验证码"))
  453. {
  454. result.subCode = TkSubCodeEnum.Captcha;
  455. result.reason = "霸下验证码";
  456. }
  457. else
  458. {
  459. switch (result.link_type)
  460. {
  461. case LinkTypeEnum.profile:
  462. case LinkTypeEnum.live:
  463. case LinkTypeEnum.video:
  464. case LinkTypeEnum.note:
  465. case LinkTypeEnum.goods:
  466. case LinkTypeEnum.baseDomain:
  467. result.reason = string.Empty;
  468. result.content = content;
  469. break;
  470. default:
  471. result.reason = "非标准链接";
  472. result.subCode = TkSubCodeEnum.NonStdLink;
  473. break;
  474. }
  475. //if (result.link_type == LinkTypeEnum.baseDomain)
  476. //{
  477. // result.content = content;
  478. //}
  479. //else
  480. //{
  481. // result.reason = "非标准链接";
  482. // result.subCode = TkSubCodeEnum.NonStdLink;
  483. //}
  484. }
  485. result.success = false;
  486. result.message = "放弃转链";
  487. result.content = resultText;
  488. result.shortLinkurl = url;
  489. return result;
  490. //return (false, result, link_type, url);
  491. }
  492. //判断淘客的¥羊角符号
  493. bool is_aff3 = ContainsSpecialFormat(content);
  494. if (is_aff3)
  495. {
  496. result.success = false;
  497. result.message = "放弃转链";
  498. result.reason = "排除淘口令";
  499. result.subCode = TkSubCodeEnum.OtherPromo;
  500. result.link_type = LinkTypeEnum.other_aff;
  501. result.shortLinkurl = null;
  502. return result;
  503. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  504. }
  505. //todo 临时应急 0614
  506. //return (false, "纯口令 没链接", link_type, null);
  507. result.success = false;
  508. result.message = "放弃转链";
  509. result.reason = "纯口令 没链接";
  510. result.subCode = TkSubCodeEnum.OtherPromo;
  511. result.link_type = LinkTypeEnum.other_aff;
  512. result.shortLinkurl = null;
  513. return result;
  514. }
  515. private LinkTypeEnum GetLinkType(string url)
  516. {
  517. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  518. if (IsShortLink(url)) return LinkTypeEnum.baseDomain;
  519. if (url.StartsWith("https://huodong.m.taobao.com/act/talent/live.html")) return LinkTypeEnum.live;
  520. if (url.StartsWith("https://web.m.taobao.com/app/tnode/web/index")) return LinkTypeEnum.video;
  521. if (url.StartsWith("https://shop.m.taobao.com/shop/shopIndex.htm")) return LinkTypeEnum.profile;
  522. string pattern = @"^https://shop\d+(\.m)*\.taobao\.com";
  523. if (Regex.IsMatch(url, pattern))
  524. {
  525. return LinkTypeEnum.profile;
  526. }
  527. //https://shop.m.taobao.com/shop/shopIndex.htm?
  528. return LinkTypeEnum.unknown;
  529. }
  530. }
  531. }