parse_2.cs 41 KB

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