parse_2.cs 43 KB

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