parse_2.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. namespace molilian.core
  12. {
  13. public partial class AlimamaPlus
  14. {
  15. public async Task<TkDataDTO> testTaskAsync()
  16. {
  17. string url = "https://www.taobao.com";
  18. TkDataDTO result = new TkDataDTO();
  19. WebClientUtility client = new WebClientUtility();
  20. client.Timeout = TimeSpan.FromMilliseconds(100);
  21. //var requestTask = alimama.UnionParse2Async(content, result, cts.Token, swData);
  22. //var delayTask = Task.Delay(timeout, cts.Token);
  23. //var completedTask = await Task.WhenAny(requestTask, delayTask);
  24. //if (completedTask == requestTask)
  25. //{
  26. // result = await requestTask;
  27. //}
  28. //else
  29. //{
  30. // cts.Cancel();
  31. // result.success = false;
  32. // result.message = "放弃转链";
  33. // result.reason = "请求超时";
  34. // result.itemName = "点击打开淘宝APP";
  35. //}
  36. return null;
  37. }
  38. public void UnionParse2(string content, ref TkDataDTO result)
  39. {
  40. if ("veapi".Equals(_api) && _api_id > 0)
  41. {
  42. var account = VeapiPoolCore.Get(_api_id);
  43. if (account != null)
  44. {
  45. var core = new VeapiPlus(account.name, account.key, account.sessionKey);
  46. core.UnionParse(content, ref result);
  47. return;
  48. }
  49. }
  50. UnionParse(content, ref result);
  51. }
  52. public async Task<TkDataDTO> UnionParse2Async(string content, TkDataDTO result, CancellationToken cancellationToken = default, Dictionary<string, long> swData = null)
  53. {
  54. if ("veapi".Equals(_api) && _api_id > 0)
  55. {
  56. var account = VeapiPoolCore.Get(_api_id);
  57. if (account != null)
  58. {
  59. var core = new VeapiPlus(account.name, account.key, account.sessionKey);
  60. result = await core.UnionParseAsync(content, result, cancellationToken);
  61. return result;
  62. }
  63. }
  64. result = await UnionParseAsync(content, result, cancellationToken, swData);
  65. return result;
  66. }
  67. /// <summary>
  68. /// 提取url里面的id参数
  69. /// </summary>
  70. /// <param name="url"></param>
  71. /// <returns></returns>
  72. static string? ExtractItemId(string url)
  73. {
  74. Match match = Regex.Match(url, @"(?:\?|&)id=(\d+)");
  75. if (match.Success)
  76. {
  77. return match.Groups[1].Value;
  78. }
  79. return null;
  80. }
  81. /// <summary>
  82. /// 是否淘宝链接
  83. /// </summary>
  84. /// <param name="url"></param>
  85. /// <returns></returns>
  86. private static (bool, string) IsTaobaoUrl(string url)
  87. {
  88. if (url.Contains("//a.m.taobao.com/i"))
  89. {
  90. //第一种提取方法
  91. string itemId = url.GetContentPart("//a.m.taobao.com/i", ".htm");
  92. if (!string.IsNullOrEmpty(itemId))
  93. {
  94. string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
  95. return (true, resultUrl);
  96. }
  97. }
  98. if (url.Contains("//item.taobao.com/item.htm?") ||
  99. url.Contains("//h5.m.taobao.com/awp/core/detail.htm?") ||
  100. url.Contains("//new.m.taobao.com/detail.htm?") ||
  101. url.Contains("//outfliggys.m.taobao.com/app/trip/rx-travel-detail") ||
  102. url.Contains("//detail.m.tmall.com/item.htm?") ||
  103. url.Contains("//tmallx.tmall.com/app/tmallx-src/goods-detail") ||
  104. url.Contains("//detail.tmall.com/item.htm?") ||
  105. url.Contains("//detail.tmall.hk/hk/item.htm?"))
  106. {
  107. string? itemId = ExtractItemId(url);
  108. if (!string.IsNullOrEmpty(itemId))
  109. {
  110. string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
  111. return (true, resultUrl);
  112. }
  113. }
  114. return (false, url);
  115. }
  116. /// <summary>
  117. /// 排除已知的淘客链接
  118. /// </summary>
  119. /// <param name="url"></param>
  120. /// <returns></returns>
  121. static bool IsAffLink(string url)
  122. {
  123. if (!url.StartsWith("https://") || !url.StartsWith("https://")) url = "https://" + url;
  124. Uri uri = new(url);
  125. if (uri.Host.Equals("s.click.taobao.com", StringComparison.OrdinalIgnoreCase) ||
  126. uri.Host.Equals("uland.taobao.com", StringComparison.OrdinalIgnoreCase))
  127. {
  128. return true;
  129. }
  130. //if (uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase) &&
  131. // uri.AbsolutePath.StartsWith("/h.") &&
  132. // string.IsNullOrEmpty(uri.Query) && // No query parameters
  133. // !url.Contains("?tk=")) // No "tk" parameter
  134. //{
  135. // return true;
  136. //}
  137. return false;
  138. }
  139. /// <summary>
  140. /// 判断淘客的¥羊角符号
  141. /// </summary>
  142. /// <param name="content"></param>
  143. /// <returns></returns>
  144. static bool ContainsSpecialFormat(string content)
  145. {
  146. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  147. //string pattern = @"¥[a-zA-Z0-9\s]+¥";
  148. string pattern = @"[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]*";
  149. return Regex.IsMatch(content, pattern);
  150. }
  151. public TimeSpan GetRequestTimeout(int deduction = 0)
  152. {
  153. #if DEBUG
  154. //return TimeSpan.FromMilliseconds(10 * 1000);
  155. #endif
  156. int min = (int)(_config.rt_max * 0.3);
  157. if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
  158. int timeout = _config.rt_max;
  159. if (deduction == 0)
  160. {
  161. timeout -= min;
  162. return TimeSpan.FromMilliseconds(timeout);
  163. }
  164. timeout -= deduction;
  165. if (timeout <= min) return TimeSpan.FromMilliseconds(1);
  166. return TimeSpan.FromMilliseconds(min);
  167. }
  168. /// <summary>
  169. /// 从http获取跳转的url
  170. /// </summary>
  171. /// <param name="url"></param>
  172. /// <returns></returns>
  173. async Task<(bool, string)> GetDesiredUrlAsync(string url, CancellationToken cancellationToken = default)
  174. {
  175. string result;
  176. try
  177. {
  178. Uri uri = new(url);
  179. if (!uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase)) return (false, "不是淘宝短网址");
  180. string desiredUrlPattern = "var url = '(.*?)'";
  181. WebClientUtility client = new()
  182. {
  183. Proxy = _proxy,
  184. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  185. };
  186. #if DEBUG
  187. client.Proxy = null;
  188. #endif
  189. client.Timeout = GetRequestTimeout(0);
  190. var response = await client.RequestAsync(url, "GET", cancellationToken);
  191. var responseBody = response.Body();
  192. result = responseBody;
  193. if (response.Successed)
  194. {
  195. if (response.ResponseMessage.IsSuccessStatusCode)
  196. {
  197. Match match = Regex.Match(responseBody, desiredUrlPattern);
  198. if (match.Success)
  199. {
  200. result = match.Groups[1].Value; // 返回匹配的 URL
  201. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  202. return (true, result);
  203. }
  204. }
  205. else
  206. {
  207. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  208. }
  209. }
  210. else
  211. {
  212. throw response.ResponseException;
  213. }
  214. }
  215. catch (Exception ex)
  216. {
  217. result = $"{ex.Message}\n{ex.StackTrace}";
  218. }
  219. return (false, result);
  220. }
  221. /// <summary>
  222. /// 从http获取跳转的url
  223. /// </summary>
  224. /// <param name="url"></param>
  225. /// <returns></returns>
  226. (bool, string) GetDesiredUrl(string url)
  227. {
  228. string result;
  229. try
  230. {
  231. Uri uri = new(url);
  232. if (!uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase)) return (false, "不是淘宝短网址");
  233. string desiredUrlPattern = "var url = '(.*?)'";
  234. WebClientUtility client = new()
  235. {
  236. Proxy = _proxy,
  237. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  238. };
  239. #if DEBUG
  240. client.Proxy = null;
  241. #endif
  242. client.Timeout = GetRequestTimeout(0);
  243. var response = client.Request(url);
  244. var responseBody = response.Body();
  245. result = responseBody;
  246. if (response.Successed)
  247. {
  248. if (response.ResponseMessage.IsSuccessStatusCode)
  249. {
  250. Match match = Regex.Match(responseBody, desiredUrlPattern);
  251. if (match.Success)
  252. {
  253. result = match.Groups[1].Value; // 返回匹配的 URL
  254. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  255. return (true, result);
  256. }
  257. }
  258. else
  259. {
  260. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  261. }
  262. }
  263. else
  264. {
  265. throw response.ResponseException;
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. result = $"{ex.Message}\n{ex.StackTrace}";
  271. }
  272. return (false, result);
  273. }
  274. private void InternalTextProcessing(string content, ref TkDataDTO result)
  275. {
  276. //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result);
  277. bool success;
  278. string resultText;
  279. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  280. string url = GetLink(content);
  281. if (!string.IsNullOrEmpty(url))
  282. {
  283. //排除已知的淘客链接
  284. if (IsAffLink(url))
  285. {
  286. result.success = false;
  287. result.content = "排除淘客链接";
  288. result.link_type = LinkTypeEnum.other_aff;
  289. result.shortLinkurl = url;
  290. return;
  291. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  292. }
  293. //直接提取
  294. (success, resultText) = IsTaobaoUrl(url);
  295. if (success)
  296. {
  297. result.success = true;
  298. result.content = resultText;
  299. result.link_type = LinkTypeEnum.goods;
  300. result.shortLinkurl = resultText;
  301. return;
  302. //return (true, result, LinkTypeEnum.goods, result);
  303. }
  304. //从http获取跳转的url
  305. Stopwatch stopwatch = Stopwatch.StartNew();
  306. stopwatch.Start();
  307. (success, resultText) = GetDesiredUrl(url);
  308. stopwatch.Stop();
  309. // 获取执行时间
  310. result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
  311. if (success)
  312. {
  313. //排除已知的淘客链接
  314. if (IsAffLink(resultText))
  315. {
  316. result.success = false;
  317. result.content = "排除淘客链接";
  318. result.link_type = LinkTypeEnum.other_aff;
  319. result.shortLinkurl = url;
  320. return;
  321. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  322. }
  323. (success, resultText) = IsTaobaoUrl(resultText);
  324. if (success)
  325. {
  326. result.success = true;
  327. result.content = resultText;
  328. result.link_type = LinkTypeEnum.goods;
  329. result.shortLinkurl = resultText;
  330. return;
  331. //return (true, resultText, LinkTypeEnum.goods, resultText);
  332. }
  333. link_type = GetLinkType(resultText);
  334. }
  335. else
  336. {
  337. link_type = GetLinkType(url);
  338. }
  339. result.success = false;
  340. result.content = resultText;
  341. result.link_type = link_type;
  342. result.shortLinkurl = url;
  343. return;
  344. //return (false, result, link_type, url);
  345. }
  346. //判断淘客的¥羊角符号
  347. if (ContainsSpecialFormat(content))
  348. {
  349. result.success = false;
  350. result.content = "排除淘口令";
  351. result.link_type = LinkTypeEnum.other_aff;
  352. result.shortLinkurl = null;
  353. return;
  354. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  355. }
  356. //todo 临时应急 0614
  357. //return (false, "纯口令 没链接", link_type, null);
  358. result.success = false;
  359. result.content = "纯口令 没链接";
  360. result.link_type = LinkTypeEnum.other_aff;
  361. result.shortLinkurl = null;
  362. }
  363. private async Task<TkDataDTO> InternalTextProcessingAsync(string content, TkDataDTO result,
  364. CancellationToken cancellationToken,
  365. Dictionary<string, long> swData = null)
  366. {
  367. Stopwatch stopwatch = Stopwatch.StartNew();
  368. bool success;
  369. string resultText;
  370. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  371. if (swData != null)
  372. {
  373. stopwatch.Reset();
  374. stopwatch.Start();
  375. }
  376. string url = GetLink(content);
  377. if (swData != null)
  378. {
  379. stopwatch.Stop();
  380. swData.Add("\tGetLink", stopwatch.ElapsedMilliseconds);
  381. }
  382. if (!string.IsNullOrEmpty(url))
  383. {
  384. if (swData != null)
  385. {
  386. stopwatch.Reset();
  387. stopwatch.Start();
  388. }
  389. bool is_aff = IsAffLink(url);
  390. if (swData != null)
  391. {
  392. stopwatch.Stop();
  393. swData.Add("\t\tGetLink", stopwatch.ElapsedMilliseconds);
  394. }
  395. if (is_aff)
  396. {
  397. result.success = false;
  398. result.message = "放弃转链";
  399. result.reason = "其他推广链接";
  400. result.link_type = LinkTypeEnum.other_aff;
  401. result.shortLinkurl = url;
  402. return result;
  403. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  404. }
  405. //直接提取
  406. if (swData != null)
  407. {
  408. stopwatch.Reset();
  409. stopwatch.Start();
  410. }
  411. (success, resultText) = IsTaobaoUrl(url);
  412. if (swData != null)
  413. {
  414. stopwatch.Stop();
  415. swData.Add("\tIsTaobaoUrl", stopwatch.ElapsedMilliseconds);
  416. }
  417. if (success)
  418. {
  419. result.success = true;
  420. result.content = resultText;
  421. result.link_type = LinkTypeEnum.goods;
  422. result.shortLinkurl = resultText;
  423. return result;
  424. //return (true, result, LinkTypeEnum.goods, result);
  425. }
  426. Stopwatch sw = Stopwatch.StartNew();
  427. (success, resultText) = await GetDesiredUrlAsync(url, cancellationToken);
  428. sw.Stop();
  429. // 获取执行时间
  430. result.elapsedTime2 = (int)sw.ElapsedMilliseconds;
  431. if (swData != null)
  432. {
  433. swData.Add("\tGetDesiredUrlAsync", sw.ElapsedMilliseconds);
  434. }
  435. if (result.elapsedTime2 >= _config.rt_max)
  436. {
  437. result.success = false;
  438. result.message = "放弃转链";
  439. result.reason = "请求超时";
  440. result.itemName = "点击打开淘宝APP";
  441. return result;
  442. //cancellationToken.ThrowIfCancellationRequested();
  443. }
  444. if (success)
  445. {
  446. //排除已知的淘客链接
  447. if (swData != null)
  448. {
  449. stopwatch.Reset();
  450. stopwatch.Start();
  451. }
  452. bool is_aff2 = IsAffLink(resultText);
  453. if (swData != null)
  454. {
  455. stopwatch.Stop();
  456. swData.Add("\t\tIsAffLink", stopwatch.ElapsedMilliseconds);
  457. }
  458. if (is_aff2)
  459. {
  460. result.success = false;
  461. result.message = "放弃转链";
  462. result.reason = "其他推广链接";
  463. result.link_type = LinkTypeEnum.other_aff;
  464. result.shortLinkurl = url;
  465. return result;
  466. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  467. }
  468. if (swData != null)
  469. {
  470. stopwatch.Reset();
  471. stopwatch.Start();
  472. }
  473. (success, resultText) = IsTaobaoUrl(resultText);
  474. if (swData != null)
  475. {
  476. stopwatch.Stop();
  477. swData.Add("\t\tIsTaobaoUrl", stopwatch.ElapsedMilliseconds);
  478. }
  479. if (success)
  480. {
  481. result.success = true;
  482. result.content = resultText;
  483. result.link_type = LinkTypeEnum.goods;
  484. result.shortLinkurl = resultText;
  485. return result;
  486. //return (true, resultText, LinkTypeEnum.goods, resultText);
  487. }
  488. link_type = GetLinkType(resultText);
  489. }
  490. else
  491. {
  492. link_type = GetLinkType(url);
  493. }
  494. result.success = false;
  495. result.content = resultText;
  496. result.link_type = link_type;
  497. result.shortLinkurl = url;
  498. return result;
  499. //return (false, result, link_type, url);
  500. }
  501. if (swData != null)
  502. {
  503. stopwatch.Reset();
  504. stopwatch.Start();
  505. }
  506. bool is_aff3 = ContainsSpecialFormat(content);
  507. if (swData != null)
  508. {
  509. stopwatch.Stop();
  510. swData.Add("\tContainsSpecialFormat", stopwatch.ElapsedMilliseconds);
  511. }
  512. //判断淘客的¥羊角符号
  513. if (is_aff3)
  514. {
  515. result.success = false;
  516. result.content = "排除淘口令";
  517. result.link_type = LinkTypeEnum.other_aff;
  518. result.shortLinkurl = null;
  519. return result;
  520. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  521. }
  522. //todo 临时应急 0614
  523. //return (false, "纯口令 没链接", link_type, null);
  524. result.success = false;
  525. result.content = "纯口令 没链接";
  526. result.link_type = LinkTypeEnum.other_aff;
  527. result.shortLinkurl = null;
  528. return result;
  529. }
  530. private (bool, string, LinkTypeEnum, string) InternalTextProcessing222(string content)
  531. {
  532. bool success;
  533. string result;
  534. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  535. string url = GetLink(content);
  536. if (!string.IsNullOrEmpty(url))
  537. {
  538. //排除已知的淘客链接
  539. if (IsAffLink(url)) return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  540. //直接提取
  541. (success, result) = IsTaobaoUrl(url);
  542. if (success) return (true, result, LinkTypeEnum.goods, result);
  543. //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
  544. //从http获取跳转的url
  545. (success, result) = GetDesiredUrl(url);
  546. if (success)
  547. {
  548. //排除已知的淘客链接
  549. if (IsAffLink(result)) return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  550. (success, result) = IsTaobaoUrl(result);
  551. if (success) return (true, result, LinkTypeEnum.goods, result);
  552. link_type = GetLinkType(result);
  553. }
  554. else
  555. {
  556. link_type = GetLinkType(url);
  557. }
  558. return (false, result, link_type, url);
  559. }
  560. //判断淘客的¥羊角符号
  561. if (ContainsSpecialFormat(content)) return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  562. //todo 临时应急 0614
  563. //return (false, "纯口令 没链接", link_type, null);
  564. return (false, "纯口令 没链接", LinkTypeEnum.other_aff, null);
  565. }
  566. private LinkTypeEnum GetLinkType(string url)
  567. {
  568. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  569. if (url.StartsWith("https://huodong.m.taobao.com/act/talent/live.html")) return LinkTypeEnum.live;
  570. if (url.StartsWith("https://web.m.taobao.com/app/tnode/web/index")) return LinkTypeEnum.video;
  571. string pattern = @"^https://shop\d+\.m\.taobao\.com";
  572. if (Regex.IsMatch(url, pattern))
  573. {
  574. return LinkTypeEnum.profile;
  575. }
  576. return LinkTypeEnum.unknown;
  577. }
  578. private async Task<TkDataDTO> alimamaParseAsync(string content, TkDataDTO result, CancellationToken cancellationToken = default)
  579. {
  580. string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
  581. Random random = new();
  582. double randomNumber = random.NextDouble();
  583. string randomString = randomNumber.ToString()[2..];
  584. string cna = _cookies.GetContentPart("cna=", ";");
  585. string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
  586. var variableMap = new
  587. {
  588. url = content,
  589. union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
  590. lensScene = "PUB",
  591. spmB = "_portal_v2_tool_links_page_home_index_htm"
  592. }.Convert2Json(true).UrlEncode();
  593. variableMap = variableMap.Replace(" ", "%20");
  594. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  595. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  596. Stopwatch stopwatch = Stopwatch.StartNew();
  597. stopwatch.Start();
  598. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  599. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  600. .AddHeaders("Cookie", _cookies);
  601. client.Proxy = _proxy;
  602. #if DEBUG
  603. client.Proxy = null;
  604. #endif
  605. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  606. client.Timeout = GetRequestTimeout(result.elapsedTime2);
  607. //var response = client.Request(url);
  608. var response = await client.RequestAsync(url, "GET", cancellationToken);
  609. stopwatch.Stop();
  610. result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
  611. var body = string.Empty;
  612. try
  613. {
  614. if (!response.Successed)
  615. {
  616. result.channel = TkChannelEnum.tb;
  617. result.accountId = _accountId;
  618. result.accountName = _accountName;
  619. result.link_type = LinkTypeEnum.unknown;
  620. result.success = false;
  621. result.message = "fail";
  622. if (response.ResponseException != null)
  623. {
  624. _ = new LoggerLibrary("api_error", "fail")
  625. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  626. .SaveAsync();
  627. throw response.ResponseException;
  628. }
  629. return result;
  630. }
  631. if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.Found)
  632. {
  633. _ = new LoggerLibrary("api_error", "fail")
  634. .Info($"302:{response.ResponseMessage.Headers.Location}")
  635. .SaveAsync();
  636. throw new Exception("302 Found, was canceled");
  637. }
  638. JsonElement root;
  639. try
  640. {
  641. body = response.Body();
  642. root = body.Convert2JsonElement();
  643. }
  644. catch (Exception ex)
  645. {
  646. throw new Exception(body);
  647. }
  648. bool success = root.Read<bool>("success", false);
  649. string message = root.Read("message", string.Empty);
  650. string info_message = root.PathRead("info.message", string.Empty);
  651. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  652. string taoToken = root.PathRead("data.taoToken", string.Empty);
  653. string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
  654. string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
  655. string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
  656. decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
  657. string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
  658. string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
  659. string itemId = root.PathRead("data.itemId", string.Empty);
  660. string itemName = root.PathRead("data.itemName", string.Empty);
  661. decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
  662. string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
  663. string shopTitle = root.PathRead("data.shopTitle", string.Empty);
  664. string pic = root.PathRead("data.pic", string.Empty);
  665. string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
  666. if (response.ResponseMessage != null)
  667. {
  668. switch (response.ResponseMessage.StatusCode)
  669. {
  670. case System.Net.HttpStatusCode.OK:
  671. {
  672. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  673. {
  674. taoToken = couponLinkTaoToken;
  675. shortLinkurl = couponShortLinkUrl;
  676. }
  677. string shortLink = GetLink(taoToken);
  678. content = ReplaceUrls(content, shortLink);
  679. }
  680. break;
  681. case System.Net.HttpStatusCode.Found:
  682. string location = response.ResponseMessage.Headers.Location.OriginalString;
  683. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  684. {
  685. success = false;
  686. message = "nologin";
  687. }
  688. break;
  689. default:
  690. {
  691. success = false;
  692. message = "other";
  693. }
  694. break;
  695. }
  696. }
  697. if (!success)
  698. {
  699. shortLinkurl = GetLink(content);
  700. switch (message)
  701. {
  702. case "该商品已经下架或未加入淘宝客":
  703. case "该链接不支持转化,请更换链接尝试":
  704. case "网络错误":
  705. break;
  706. default:
  707. var _headers = response.ResponseMessage.Headers;
  708. var headers = "";
  709. foreach (var h in _headers)
  710. {
  711. foreach (var Value in h.Value)
  712. {
  713. headers += $"{h.Key}: {Value}\n";
  714. }
  715. }
  716. _ = new LoggerLibrary("api_error", "fail")
  717. .Info(message, content)
  718. .Info(headers, body)
  719. .SaveAsync();
  720. break;
  721. }
  722. }
  723. string deeplink_url = GetDeeplink(shortLinkurl);
  724. result.channel = TkChannelEnum.tb;
  725. result.accountId = _accountId;
  726. result.accountName = _accountName;
  727. result.link_type = success ? LinkTypeEnum.goods : result.link_type;
  728. result.success = success;
  729. result.message = message;
  730. result.content = content;
  731. result.couponAmount = couponAmount;
  732. result.couponEffectiveEndTime = couponEffectiveEndTime;
  733. result.couponEffectiveStartTime = couponEffectiveStartTime;
  734. result.taoToken = taoToken;
  735. result.shortLinkurl = shortLinkurl;
  736. result.deeplink_url = deeplink_url;
  737. result.itemId = itemId;
  738. result.itemName = itemName;
  739. result.promotionPrice = promotionPrice;
  740. result.sellerNickName = sellerNickName;
  741. result.shopTitle = shopTitle;
  742. result.pic = pic;
  743. result.qrCodeUrl = qrCodeUrl;
  744. }
  745. catch
  746. {
  747. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  748. {
  749. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  750. {
  751. throw new APIException("抱歉!页面无法访问……");
  752. }
  753. }
  754. throw;
  755. }
  756. return result;
  757. }
  758. private TkDataDTO alimamaParse(string content, TkDataDTO result)
  759. {
  760. string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
  761. Random random = new();
  762. double randomNumber = random.NextDouble();
  763. string randomString = randomNumber.ToString()[2..];
  764. string cna = _cookies.GetContentPart("cna=", ";");
  765. string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
  766. var variableMap = new
  767. {
  768. url = content,
  769. union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
  770. lensScene = "PUB",
  771. spmB = "_portal_v2_tool_links_page_home_index_htm"
  772. }.Convert2Json(true).UrlEncode();
  773. variableMap = variableMap.Replace(" ", "%20");
  774. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  775. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  776. Stopwatch stopwatch = Stopwatch.StartNew();
  777. stopwatch.Start();
  778. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  779. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  780. .AddHeaders("Cookie", _cookies);
  781. client.Proxy = _proxy;
  782. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  783. #if DEBUG
  784. client.Proxy = null;
  785. url = "https://47.246.177.226/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  786. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  787. client.Headers["Host"] = "pub.alimama.com";
  788. #endif
  789. client.Timeout = GetRequestTimeout(result.elapsedTime2);
  790. //var response = client.Request(url);
  791. var response = client.Request(url);
  792. stopwatch.Stop();
  793. result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
  794. var body = string.Empty;
  795. try
  796. {
  797. if (!response.Successed)
  798. {
  799. result.channel = TkChannelEnum.tb;
  800. result.accountId = _accountId;
  801. result.accountName = _accountName;
  802. result.link_type = LinkTypeEnum.unknown;
  803. result.success = false;
  804. result.message = "fail";
  805. if (response.ResponseException != null)
  806. {
  807. _ = new LoggerLibrary("api_error", "fail")
  808. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  809. .SaveAsync();
  810. throw response.ResponseException;
  811. }
  812. return result;
  813. }
  814. body = response.Body();
  815. //{"code":601,"info":{"ok":false,"message":"nologin"}}
  816. JsonElement root = body.Convert2JsonElement();
  817. bool success = root.Read<bool>("success", false);
  818. string message = root.Read("message", string.Empty);
  819. string info_message = root.PathRead("info.message", string.Empty);
  820. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  821. string taoToken = root.PathRead("data.taoToken", string.Empty);
  822. string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
  823. string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
  824. string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
  825. decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
  826. string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
  827. string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
  828. string itemId = root.PathRead("data.itemId", string.Empty);
  829. string itemName = root.PathRead("data.itemName", string.Empty);
  830. decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
  831. string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
  832. string shopTitle = root.PathRead("data.shopTitle", string.Empty);
  833. string pic = root.PathRead("data.pic", string.Empty);
  834. string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
  835. if (response.ResponseMessage != null)
  836. {
  837. switch (response.ResponseMessage.StatusCode)
  838. {
  839. case System.Net.HttpStatusCode.OK:
  840. {
  841. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  842. {
  843. taoToken = couponLinkTaoToken;
  844. shortLinkurl = couponShortLinkUrl;
  845. }
  846. string shortLink = GetLink(taoToken);
  847. content = ReplaceUrls(content, shortLink);
  848. }
  849. break;
  850. case System.Net.HttpStatusCode.Found:
  851. string location = response.ResponseMessage.Headers.Location.OriginalString;
  852. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  853. {
  854. success = false;
  855. message = "nologin";
  856. }
  857. break;
  858. default:
  859. {
  860. success = false;
  861. message = "other";
  862. }
  863. break;
  864. }
  865. }
  866. if (!success)
  867. {
  868. shortLinkurl = GetLink(content);
  869. switch (message)
  870. {
  871. case "该商品已经下架或未加入淘宝客":
  872. case "该链接不支持转化,请更换链接尝试":
  873. case "网络错误":
  874. break;
  875. default:
  876. var _headers = response.ResponseMessage.Headers;
  877. var headers = "";
  878. foreach (var h in _headers)
  879. {
  880. foreach (var Value in h.Value)
  881. {
  882. headers += $"{h.Key}: {Value}\n";
  883. }
  884. }
  885. _ = new LoggerLibrary("api_error", "fail")
  886. .Info(message, content)
  887. .Info(headers, body)
  888. .SaveAsync();
  889. break;
  890. }
  891. }
  892. if (success)
  893. {
  894. if (string.IsNullOrEmpty(itemName))
  895. {
  896. itemName = GetTitle(taoToken);
  897. }
  898. }
  899. else
  900. {
  901. itemName = GetTitle(content);
  902. }
  903. string deeplink_url = GetDeeplink(shortLinkurl);
  904. result.channel = TkChannelEnum.tb;
  905. result.accountId = _accountId;
  906. result.accountName = _accountName;
  907. if (success)
  908. {
  909. switch (result.link_type)
  910. {
  911. case LinkTypeEnum.profile:
  912. case LinkTypeEnum.goods:
  913. break;
  914. default:
  915. result.link_type = LinkTypeEnum.goods;
  916. break;
  917. }
  918. }
  919. result.success = success;
  920. result.message = message;
  921. result.content = content;
  922. result.couponAmount = couponAmount;
  923. result.couponEffectiveEndTime = couponEffectiveEndTime;
  924. result.couponEffectiveStartTime = couponEffectiveStartTime;
  925. result.taoToken = taoToken;
  926. result.shortLinkurl = shortLinkurl;
  927. result.deeplink_url = deeplink_url;
  928. result.itemId = itemId;
  929. result.itemName = itemName;
  930. result.promotionPrice = promotionPrice;
  931. result.sellerNickName = sellerNickName;
  932. result.shopTitle = shopTitle;
  933. result.pic = pic;
  934. result.qrCodeUrl = qrCodeUrl;
  935. }
  936. catch
  937. {
  938. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  939. {
  940. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  941. {
  942. throw new APIException("抱歉!页面无法访问……");
  943. }
  944. }
  945. throw;
  946. }
  947. return result;
  948. }
  949. }
  950. }