parse_2.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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)
  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);
  35. return result;
  36. }
  37. }
  38. result = await UnionParseAsync(content, result);
  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. if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
  131. int timeout = _config.rt_max;
  132. if (deduction == 0) return TimeSpan.FromMilliseconds(timeout);
  133. timeout = timeout - deduction;
  134. if (timeout <= 200) return TimeSpan.FromMilliseconds(200);
  135. return TimeSpan.FromMilliseconds(timeout);
  136. }
  137. /// <summary>
  138. /// 从http获取跳转的url
  139. /// </summary>
  140. /// <param name="url"></param>
  141. /// <returns></returns>
  142. async Task<(bool, string)> GetDesiredUrlAsync(string url)
  143. {
  144. string result;
  145. try
  146. {
  147. Uri uri = new(url);
  148. if (!uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase)) return (false, "不是淘宝短网址");
  149. string desiredUrlPattern = "var url = '(.*?)'";
  150. WebClientUtility client = new()
  151. {
  152. Proxy = _proxy,
  153. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  154. };
  155. #if DEBUG
  156. client.Proxy = null;
  157. #endif
  158. client.Timeout = GetRequestTimeout(0);
  159. var response = await client.RequestAsync(url);
  160. var responseBody = response.Body();
  161. result = responseBody;
  162. if (response.Successed)
  163. {
  164. if (response.ResponseMessage.IsSuccessStatusCode)
  165. {
  166. Match match = Regex.Match(responseBody, desiredUrlPattern);
  167. if (match.Success)
  168. {
  169. result = match.Groups[1].Value; // 返回匹配的 URL
  170. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  171. return (true, result);
  172. }
  173. }
  174. else
  175. {
  176. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  177. }
  178. }
  179. else
  180. {
  181. throw response.ResponseException;
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. result = $"{ex.Message}\n{ex.StackTrace}";
  187. }
  188. return (false, result);
  189. }
  190. /// <summary>
  191. /// 从http获取跳转的url
  192. /// </summary>
  193. /// <param name="url"></param>
  194. /// <returns></returns>
  195. (bool, string) GetDesiredUrl(string url)
  196. {
  197. string result;
  198. try
  199. {
  200. Uri uri = new(url);
  201. if (!uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase)) return (false, "不是淘宝短网址");
  202. string desiredUrlPattern = "var url = '(.*?)'";
  203. WebClientUtility client = new()
  204. {
  205. Proxy = _proxy,
  206. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  207. };
  208. #if DEBUG
  209. client.Proxy = null;
  210. #endif
  211. client.Timeout = GetRequestTimeout(0);
  212. var response = client.Request(url);
  213. var responseBody = response.Body();
  214. result = responseBody;
  215. if (response.Successed)
  216. {
  217. if (response.ResponseMessage.IsSuccessStatusCode)
  218. {
  219. Match match = Regex.Match(responseBody, desiredUrlPattern);
  220. if (match.Success)
  221. {
  222. result = match.Groups[1].Value; // 返回匹配的 URL
  223. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  224. return (true, result);
  225. }
  226. }
  227. else
  228. {
  229. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  230. }
  231. }
  232. else
  233. {
  234. throw response.ResponseException;
  235. }
  236. }
  237. catch (Exception ex)
  238. {
  239. result = $"{ex.Message}\n{ex.StackTrace}";
  240. }
  241. return (false, result);
  242. }
  243. private void InternalTextProcessing(string content, ref TkDataDTO result)
  244. {
  245. //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result);
  246. bool success;
  247. string resultText;
  248. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  249. string url = GetLink(content);
  250. if (!string.IsNullOrEmpty(url))
  251. {
  252. //排除已知的淘客链接
  253. if (IsAffLink(url))
  254. {
  255. result.success = false;
  256. result.content = "排除淘客链接";
  257. result.link_type = LinkTypeEnum.other_aff;
  258. result.shortLinkurl = url;
  259. return;
  260. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  261. }
  262. //直接提取
  263. (success, resultText) = IsTaobaoUrl(url);
  264. if (success)
  265. {
  266. result.success = true;
  267. result.content = resultText;
  268. result.link_type = LinkTypeEnum.goods;
  269. result.shortLinkurl = resultText;
  270. return;
  271. //return (true, result, LinkTypeEnum.goods, result);
  272. }
  273. //从http获取跳转的url
  274. Stopwatch stopwatch = Stopwatch.StartNew();
  275. stopwatch.Start();
  276. (success, resultText) = GetDesiredUrl(url);
  277. stopwatch.Stop();
  278. // 获取执行时间
  279. result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
  280. if (success)
  281. {
  282. //排除已知的淘客链接
  283. if (IsAffLink(resultText))
  284. {
  285. result.success = false;
  286. result.content = "排除淘客链接";
  287. result.link_type = LinkTypeEnum.other_aff;
  288. result.shortLinkurl = url;
  289. return;
  290. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  291. }
  292. (success, resultText) = IsTaobaoUrl(resultText);
  293. if (success)
  294. {
  295. result.success = true;
  296. result.content = resultText;
  297. result.link_type = LinkTypeEnum.goods;
  298. result.shortLinkurl = resultText;
  299. return;
  300. //return (true, resultText, LinkTypeEnum.goods, resultText);
  301. }
  302. link_type = GetLinkType(resultText);
  303. }
  304. else
  305. {
  306. link_type = GetLinkType(url);
  307. }
  308. result.success = false;
  309. result.content = resultText;
  310. result.link_type = link_type;
  311. result.shortLinkurl = url;
  312. return;
  313. //return (false, result, link_type, url);
  314. }
  315. //判断淘客的¥羊角符号
  316. if (ContainsSpecialFormat(content))
  317. {
  318. result.success = false;
  319. result.content = "排除淘口令";
  320. result.link_type = LinkTypeEnum.other_aff;
  321. result.shortLinkurl = null;
  322. return;
  323. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  324. }
  325. //todo 临时应急 0614
  326. //return (false, "纯口令 没链接", link_type, null);
  327. result.success = false;
  328. result.content = "纯口令 没链接";
  329. result.link_type = LinkTypeEnum.other_aff;
  330. result.shortLinkurl = null;
  331. }
  332. private async Task<TkDataDTO> InternalTextProcessingAsync(string content, TkDataDTO result)
  333. {
  334. //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result);
  335. bool success;
  336. string resultText;
  337. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  338. string url = GetLink(content);
  339. if (!string.IsNullOrEmpty(url))
  340. {
  341. //排除已知的淘客链接
  342. if (IsAffLink(url))
  343. {
  344. result.success = false;
  345. result.content = "排除淘客链接";
  346. result.link_type = LinkTypeEnum.other_aff;
  347. result.shortLinkurl = url;
  348. return result;
  349. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  350. }
  351. //直接提取
  352. (success, resultText) = IsTaobaoUrl(url);
  353. if (success)
  354. {
  355. result.success = true;
  356. result.content = resultText;
  357. result.link_type = LinkTypeEnum.goods;
  358. result.shortLinkurl = resultText;
  359. return result;
  360. //return (true, result, LinkTypeEnum.goods, result);
  361. }
  362. //从http获取跳转的url
  363. Stopwatch stopwatch = Stopwatch.StartNew();
  364. stopwatch.Start();
  365. (success, resultText) = await GetDesiredUrlAsync(url);
  366. stopwatch.Stop();
  367. // 获取执行时间
  368. result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
  369. if (success)
  370. {
  371. //排除已知的淘客链接
  372. if (IsAffLink(resultText))
  373. {
  374. result.success = false;
  375. result.content = "排除淘客链接";
  376. result.link_type = LinkTypeEnum.other_aff;
  377. result.shortLinkurl = url;
  378. return result;
  379. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  380. }
  381. (success, resultText) = IsTaobaoUrl(resultText);
  382. if (success)
  383. {
  384. result.success = true;
  385. result.content = resultText;
  386. result.link_type = LinkTypeEnum.goods;
  387. result.shortLinkurl = resultText;
  388. return result;
  389. //return (true, resultText, LinkTypeEnum.goods, resultText);
  390. }
  391. link_type = GetLinkType(resultText);
  392. }
  393. else
  394. {
  395. link_type = GetLinkType(url);
  396. }
  397. result.success = false;
  398. result.content = resultText;
  399. result.link_type = link_type;
  400. result.shortLinkurl = url;
  401. return result;
  402. //return (false, result, link_type, url);
  403. }
  404. //判断淘客的¥羊角符号
  405. if (ContainsSpecialFormat(content))
  406. {
  407. result.success = false;
  408. result.content = "排除淘口令";
  409. result.link_type = LinkTypeEnum.other_aff;
  410. result.shortLinkurl = null;
  411. return result;
  412. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  413. }
  414. //todo 临时应急 0614
  415. //return (false, "纯口令 没链接", link_type, null);
  416. result.success = false;
  417. result.content = "纯口令 没链接";
  418. result.link_type = LinkTypeEnum.other_aff;
  419. result.shortLinkurl = null;
  420. return result;
  421. }
  422. private (bool, string, LinkTypeEnum, string) InternalTextProcessing222(string content)
  423. {
  424. bool success;
  425. string result;
  426. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  427. string url = GetLink(content);
  428. if (!string.IsNullOrEmpty(url))
  429. {
  430. //排除已知的淘客链接
  431. if (IsAffLink(url)) return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  432. //直接提取
  433. (success, result) = IsTaobaoUrl(url);
  434. if (success) return (true, result, LinkTypeEnum.goods, result);
  435. //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
  436. //从http获取跳转的url
  437. (success, result) = GetDesiredUrl(url);
  438. if (success)
  439. {
  440. //排除已知的淘客链接
  441. if (IsAffLink(result)) return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  442. (success, result) = IsTaobaoUrl(result);
  443. if (success) return (true, result, LinkTypeEnum.goods, result);
  444. link_type = GetLinkType(result);
  445. }
  446. else
  447. {
  448. link_type = GetLinkType(url);
  449. }
  450. return (false, result, link_type, url);
  451. }
  452. //判断淘客的¥羊角符号
  453. if (ContainsSpecialFormat(content)) return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  454. //todo 临时应急 0614
  455. //return (false, "纯口令 没链接", link_type, null);
  456. return (false, "纯口令 没链接", LinkTypeEnum.other_aff, null);
  457. }
  458. private LinkTypeEnum GetLinkType(string url)
  459. {
  460. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  461. if (url.StartsWith("https://huodong.m.taobao.com/act/talent/live.html")) return LinkTypeEnum.live;
  462. if (url.StartsWith("https://web.m.taobao.com/app/tnode/web/index")) return LinkTypeEnum.video;
  463. string pattern = @"^https://shop\d+\.m\.taobao\.com";
  464. if (Regex.IsMatch(url, pattern))
  465. {
  466. return LinkTypeEnum.profile;
  467. }
  468. return LinkTypeEnum.unknown;
  469. }
  470. private async Task<TkDataDTO> alimamaParseAsync(string content, TkDataDTO result)
  471. {
  472. string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
  473. Random random = new();
  474. double randomNumber = random.NextDouble();
  475. string randomString = randomNumber.ToString()[2..];
  476. string cna = _cookies.GetContentPart("cna=", ";");
  477. string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
  478. var variableMap = new
  479. {
  480. url = content,
  481. union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
  482. lensScene = "PUB",
  483. spmB = "_portal_v2_tool_links_page_home_index_htm"
  484. }.Convert2Json(true).UrlEncode();
  485. variableMap = variableMap.Replace(" ", "%20");
  486. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  487. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  488. Stopwatch stopwatch = Stopwatch.StartNew();
  489. stopwatch.Start();
  490. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  491. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  492. .AddHeaders("Cookie", _cookies);
  493. client.Proxy = _proxy;
  494. #if DEBUG
  495. client.Proxy = null;
  496. #endif
  497. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  498. client.Timeout = GetRequestTimeout(result.elapsedTime2);
  499. //var response = client.Request(url);
  500. var response = await client.RequestAsync(url);
  501. stopwatch.Stop();
  502. result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
  503. var body = string.Empty;
  504. try
  505. {
  506. if (!response.Successed)
  507. {
  508. result.channel = TkChannelEnum.tb;
  509. result.accountId = _accountId;
  510. result.accountName = _accountName;
  511. result.link_type = LinkTypeEnum.unknown;
  512. result.success = false;
  513. result.message = "fail";
  514. if (response.ResponseException != null)
  515. {
  516. _ = new LoggerLibrary("api_error", "fail")
  517. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  518. .SaveAsync();
  519. throw response.ResponseException;
  520. }
  521. return result;
  522. }
  523. body = response.Body();
  524. JsonElement root = body.Convert2JsonElement();
  525. bool success = root.Read<bool>("success", false);
  526. string message = root.Read("message", string.Empty);
  527. string info_message = root.PathRead("info.message", string.Empty);
  528. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  529. string taoToken = root.PathRead("data.taoToken", string.Empty);
  530. string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
  531. string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
  532. string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
  533. decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
  534. string itemId = root.PathRead("data.itemId", string.Empty);
  535. string itemName = root.PathRead("data.itemName", string.Empty);
  536. decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
  537. string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
  538. string shopTitle = root.PathRead("data.shopTitle", string.Empty);
  539. string pic = root.PathRead("data.pic", string.Empty);
  540. string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
  541. if (response.ResponseMessage != null)
  542. {
  543. switch (response.ResponseMessage.StatusCode)
  544. {
  545. case System.Net.HttpStatusCode.OK:
  546. {
  547. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  548. {
  549. taoToken = couponLinkTaoToken;
  550. shortLinkurl = couponShortLinkUrl;
  551. }
  552. string shortLink = GetLink(taoToken);
  553. content = ReplaceUrls(content, shortLink);
  554. }
  555. break;
  556. case System.Net.HttpStatusCode.Found:
  557. string location = response.ResponseMessage.Headers.Location.OriginalString;
  558. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  559. {
  560. success = false;
  561. message = "nologin";
  562. }
  563. break;
  564. default:
  565. {
  566. success = false;
  567. message = "other";
  568. }
  569. break;
  570. }
  571. }
  572. if (!success)
  573. {
  574. shortLinkurl = GetLink(content);
  575. switch (message)
  576. {
  577. case "该链接不支持转化,请更换链接尝试":
  578. case "网络错误":
  579. break;
  580. default:
  581. var _headers = response.ResponseMessage.Headers;
  582. var headers = "";
  583. foreach (var h in _headers)
  584. {
  585. foreach (var Value in h.Value)
  586. {
  587. headers += $"{h.Key}: {Value}\n";
  588. }
  589. }
  590. _ = new LoggerLibrary("api_error", "fail")
  591. .Info(message, content)
  592. .Info(headers, body)
  593. .SaveAsync();
  594. break;
  595. }
  596. }
  597. string deeplink_url = GetDeeplink(shortLinkurl);
  598. result.channel = TkChannelEnum.tb;
  599. result.accountId = _accountId;
  600. result.accountName = _accountName;
  601. result.link_type = success ? LinkTypeEnum.goods : result.link_type;
  602. result.success = success;
  603. result.message = message;
  604. result.content = content;
  605. result.couponAmount = couponAmount;
  606. result.taoToken = taoToken;
  607. result.shortLinkurl = shortLinkurl;
  608. result.deeplink_url = deeplink_url;
  609. result.itemId = itemId;
  610. result.itemName = itemName;
  611. result.promotionPrice = promotionPrice;
  612. result.sellerNickName = sellerNickName;
  613. result.shopTitle = shopTitle;
  614. result.pic = pic;
  615. result.qrCodeUrl = qrCodeUrl;
  616. }
  617. catch
  618. {
  619. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  620. {
  621. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  622. {
  623. throw new APIException("抱歉!页面无法访问……");
  624. }
  625. }
  626. throw;
  627. }
  628. return result;
  629. }
  630. private TkDataDTO alimamaParse(string content, TkDataDTO result)
  631. {
  632. string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
  633. Random random = new();
  634. double randomNumber = random.NextDouble();
  635. string randomString = randomNumber.ToString()[2..];
  636. string cna = _cookies.GetContentPart("cna=", ";");
  637. string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
  638. var variableMap = new
  639. {
  640. url = content,
  641. union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
  642. lensScene = "PUB",
  643. spmB = "_portal_v2_tool_links_page_home_index_htm"
  644. }.Convert2Json(true).UrlEncode();
  645. variableMap = variableMap.Replace(" ", "%20");
  646. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  647. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  648. Stopwatch stopwatch = Stopwatch.StartNew();
  649. stopwatch.Start();
  650. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  651. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  652. .AddHeaders("Cookie", _cookies);
  653. client.Proxy = _proxy;
  654. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  655. #if DEBUG
  656. client.Proxy = null;
  657. url = "https://47.246.177.226/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  658. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  659. client.Headers["Host"] = "pub.alimama.com";
  660. #endif
  661. client.Timeout = GetRequestTimeout(result.elapsedTime2);
  662. //var response = client.Request(url);
  663. var response = client.Request(url);
  664. stopwatch.Stop();
  665. result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
  666. var body = string.Empty;
  667. try
  668. {
  669. if (!response.Successed)
  670. {
  671. result.channel = TkChannelEnum.tb;
  672. result.accountId = _accountId;
  673. result.accountName = _accountName;
  674. result.link_type = LinkTypeEnum.unknown;
  675. result.success = false;
  676. result.message = "fail";
  677. if (response.ResponseException != null)
  678. {
  679. _ = new LoggerLibrary("api_error", "fail")
  680. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  681. .SaveAsync();
  682. throw response.ResponseException;
  683. }
  684. return result;
  685. }
  686. body = response.Body();
  687. //{"code":601,"info":{"ok":false,"message":"nologin"}}
  688. JsonElement root = body.Convert2JsonElement();
  689. bool success = root.Read<bool>("success", false);
  690. string message = root.Read("message", string.Empty);
  691. string info_message = root.PathRead("info.message", string.Empty);
  692. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  693. string taoToken = root.PathRead("data.taoToken", string.Empty);
  694. string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
  695. string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
  696. string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
  697. decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
  698. string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
  699. string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
  700. string itemId = root.PathRead("data.itemId", string.Empty);
  701. string itemName = root.PathRead("data.itemName", string.Empty);
  702. decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
  703. string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
  704. string shopTitle = root.PathRead("data.shopTitle", string.Empty);
  705. string pic = root.PathRead("data.pic", string.Empty);
  706. string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
  707. if (response.ResponseMessage != null)
  708. {
  709. switch (response.ResponseMessage.StatusCode)
  710. {
  711. case System.Net.HttpStatusCode.OK:
  712. {
  713. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  714. {
  715. taoToken = couponLinkTaoToken;
  716. shortLinkurl = couponShortLinkUrl;
  717. }
  718. string shortLink = GetLink(taoToken);
  719. content = ReplaceUrls(content, shortLink);
  720. }
  721. break;
  722. case System.Net.HttpStatusCode.Found:
  723. string location = response.ResponseMessage.Headers.Location.OriginalString;
  724. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  725. {
  726. success = false;
  727. message = "nologin";
  728. }
  729. break;
  730. default:
  731. {
  732. success = false;
  733. message = "other";
  734. }
  735. break;
  736. }
  737. }
  738. if (!success)
  739. {
  740. shortLinkurl = GetLink(content);
  741. switch (message)
  742. {
  743. case "该链接不支持转化,请更换链接尝试":
  744. case "网络错误":
  745. break;
  746. default:
  747. var _headers = response.ResponseMessage.Headers;
  748. var headers = "";
  749. foreach (var h in _headers)
  750. {
  751. foreach (var Value in h.Value)
  752. {
  753. headers += $"{h.Key}: {Value}\n";
  754. }
  755. }
  756. _ = new LoggerLibrary("api_error", "fail")
  757. .Info(message, content)
  758. .Info(headers, body)
  759. .SaveAsync();
  760. break;
  761. }
  762. }
  763. if (success)
  764. {
  765. if (string.IsNullOrEmpty(itemName))
  766. {
  767. itemName = GetTitle(taoToken);
  768. }
  769. }
  770. else
  771. {
  772. itemName = GetTitle(content);
  773. }
  774. string deeplink_url = GetDeeplink(shortLinkurl);
  775. result.channel = TkChannelEnum.tb;
  776. result.accountId = _accountId;
  777. result.accountName = _accountName;
  778. if (success)
  779. {
  780. switch (result.link_type)
  781. {
  782. case LinkTypeEnum.profile:
  783. case LinkTypeEnum.goods:
  784. break;
  785. default:
  786. result.link_type = LinkTypeEnum.goods;
  787. break;
  788. }
  789. }
  790. result.success = success;
  791. result.message = message;
  792. result.content = content;
  793. result.couponAmount = couponAmount;
  794. result.couponEffectiveEndTime = couponEffectiveEndTime;
  795. result.couponEffectiveStartTime = couponEffectiveStartTime;
  796. result.taoToken = taoToken;
  797. result.shortLinkurl = shortLinkurl;
  798. result.deeplink_url = deeplink_url;
  799. result.itemId = itemId;
  800. result.itemName = itemName;
  801. result.promotionPrice = promotionPrice;
  802. result.sellerNickName = sellerNickName;
  803. result.shopTitle = shopTitle;
  804. result.pic = pic;
  805. result.qrCodeUrl = qrCodeUrl;
  806. }
  807. catch
  808. {
  809. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  810. {
  811. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  812. {
  813. throw new APIException("抱歉!页面无法访问……");
  814. }
  815. }
  816. throw;
  817. }
  818. return result;
  819. }
  820. }
  821. }