parse_2.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.Found)
  524. {
  525. _ = new LoggerLibrary("api_error", "fail")
  526. .Info($"302:{response.ResponseMessage.Headers.Location}")
  527. .SaveAsync();
  528. throw new Exception("302 Found, was canceled");
  529. }
  530. JsonElement root;
  531. try
  532. {
  533. body = response.Body();
  534. root = body.Convert2JsonElement();
  535. }
  536. catch (Exception ex)
  537. {
  538. throw new Exception(body);
  539. }
  540. bool success = root.Read<bool>("success", false);
  541. string message = root.Read("message", string.Empty);
  542. string info_message = root.PathRead("info.message", string.Empty);
  543. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  544. string taoToken = root.PathRead("data.taoToken", string.Empty);
  545. string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
  546. string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
  547. string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
  548. decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
  549. string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
  550. string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
  551. string itemId = root.PathRead("data.itemId", string.Empty);
  552. string itemName = root.PathRead("data.itemName", string.Empty);
  553. decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
  554. string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
  555. string shopTitle = root.PathRead("data.shopTitle", string.Empty);
  556. string pic = root.PathRead("data.pic", string.Empty);
  557. string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
  558. if (response.ResponseMessage != null)
  559. {
  560. switch (response.ResponseMessage.StatusCode)
  561. {
  562. case System.Net.HttpStatusCode.OK:
  563. {
  564. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  565. {
  566. taoToken = couponLinkTaoToken;
  567. shortLinkurl = couponShortLinkUrl;
  568. }
  569. string shortLink = GetLink(taoToken);
  570. content = ReplaceUrls(content, shortLink);
  571. }
  572. break;
  573. case System.Net.HttpStatusCode.Found:
  574. string location = response.ResponseMessage.Headers.Location.OriginalString;
  575. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  576. {
  577. success = false;
  578. message = "nologin";
  579. }
  580. break;
  581. default:
  582. {
  583. success = false;
  584. message = "other";
  585. }
  586. break;
  587. }
  588. }
  589. if (!success)
  590. {
  591. shortLinkurl = GetLink(content);
  592. switch (message)
  593. {
  594. case "该商品已经下架或未加入淘宝客":
  595. case "该链接不支持转化,请更换链接尝试":
  596. case "网络错误":
  597. break;
  598. default:
  599. var _headers = response.ResponseMessage.Headers;
  600. var headers = "";
  601. foreach (var h in _headers)
  602. {
  603. foreach (var Value in h.Value)
  604. {
  605. headers += $"{h.Key}: {Value}\n";
  606. }
  607. }
  608. _ = new LoggerLibrary("api_error", "fail")
  609. .Info(message, content)
  610. .Info(headers, body)
  611. .SaveAsync();
  612. break;
  613. }
  614. }
  615. string deeplink_url = GetDeeplink(shortLinkurl);
  616. result.channel = TkChannelEnum.tb;
  617. result.accountId = _accountId;
  618. result.accountName = _accountName;
  619. result.link_type = success ? LinkTypeEnum.goods : result.link_type;
  620. result.success = success;
  621. result.message = message;
  622. result.content = content;
  623. result.couponAmount = couponAmount;
  624. result.couponEffectiveEndTime = couponEffectiveEndTime;
  625. result.couponEffectiveStartTime = couponEffectiveStartTime;
  626. result.taoToken = taoToken;
  627. result.shortLinkurl = shortLinkurl;
  628. result.deeplink_url = deeplink_url;
  629. result.itemId = itemId;
  630. result.itemName = itemName;
  631. result.promotionPrice = promotionPrice;
  632. result.sellerNickName = sellerNickName;
  633. result.shopTitle = shopTitle;
  634. result.pic = pic;
  635. result.qrCodeUrl = qrCodeUrl;
  636. }
  637. catch
  638. {
  639. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  640. {
  641. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  642. {
  643. throw new APIException("抱歉!页面无法访问……");
  644. }
  645. }
  646. throw;
  647. }
  648. return result;
  649. }
  650. private TkDataDTO alimamaParse(string content, TkDataDTO result)
  651. {
  652. string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
  653. Random random = new();
  654. double randomNumber = random.NextDouble();
  655. string randomString = randomNumber.ToString()[2..];
  656. string cna = _cookies.GetContentPart("cna=", ";");
  657. string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
  658. var variableMap = new
  659. {
  660. url = content,
  661. union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
  662. lensScene = "PUB",
  663. spmB = "_portal_v2_tool_links_page_home_index_htm"
  664. }.Convert2Json(true).UrlEncode();
  665. variableMap = variableMap.Replace(" ", "%20");
  666. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  667. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  668. Stopwatch stopwatch = Stopwatch.StartNew();
  669. stopwatch.Start();
  670. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  671. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  672. .AddHeaders("Cookie", _cookies);
  673. client.Proxy = _proxy;
  674. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  675. #if DEBUG
  676. client.Proxy = null;
  677. url = "https://47.246.177.226/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  678. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  679. client.Headers["Host"] = "pub.alimama.com";
  680. #endif
  681. client.Timeout = GetRequestTimeout(result.elapsedTime2);
  682. //var response = client.Request(url);
  683. var response = client.Request(url);
  684. stopwatch.Stop();
  685. result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
  686. var body = string.Empty;
  687. try
  688. {
  689. if (!response.Successed)
  690. {
  691. result.channel = TkChannelEnum.tb;
  692. result.accountId = _accountId;
  693. result.accountName = _accountName;
  694. result.link_type = LinkTypeEnum.unknown;
  695. result.success = false;
  696. result.message = "fail";
  697. if (response.ResponseException != null)
  698. {
  699. _ = new LoggerLibrary("api_error", "fail")
  700. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  701. .SaveAsync();
  702. throw response.ResponseException;
  703. }
  704. return result;
  705. }
  706. body = response.Body();
  707. //{"code":601,"info":{"ok":false,"message":"nologin"}}
  708. JsonElement root = body.Convert2JsonElement();
  709. bool success = root.Read<bool>("success", false);
  710. string message = root.Read("message", string.Empty);
  711. string info_message = root.PathRead("info.message", string.Empty);
  712. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  713. string taoToken = root.PathRead("data.taoToken", string.Empty);
  714. string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
  715. string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
  716. string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
  717. decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
  718. string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
  719. string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
  720. string itemId = root.PathRead("data.itemId", string.Empty);
  721. string itemName = root.PathRead("data.itemName", string.Empty);
  722. decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
  723. string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
  724. string shopTitle = root.PathRead("data.shopTitle", string.Empty);
  725. string pic = root.PathRead("data.pic", string.Empty);
  726. string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
  727. if (response.ResponseMessage != null)
  728. {
  729. switch (response.ResponseMessage.StatusCode)
  730. {
  731. case System.Net.HttpStatusCode.OK:
  732. {
  733. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  734. {
  735. taoToken = couponLinkTaoToken;
  736. shortLinkurl = couponShortLinkUrl;
  737. }
  738. string shortLink = GetLink(taoToken);
  739. content = ReplaceUrls(content, shortLink);
  740. }
  741. break;
  742. case System.Net.HttpStatusCode.Found:
  743. string location = response.ResponseMessage.Headers.Location.OriginalString;
  744. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  745. {
  746. success = false;
  747. message = "nologin";
  748. }
  749. break;
  750. default:
  751. {
  752. success = false;
  753. message = "other";
  754. }
  755. break;
  756. }
  757. }
  758. if (!success)
  759. {
  760. shortLinkurl = GetLink(content);
  761. switch (message)
  762. {
  763. case "该商品已经下架或未加入淘宝客":
  764. case "该链接不支持转化,请更换链接尝试":
  765. case "网络错误":
  766. break;
  767. default:
  768. var _headers = response.ResponseMessage.Headers;
  769. var headers = "";
  770. foreach (var h in _headers)
  771. {
  772. foreach (var Value in h.Value)
  773. {
  774. headers += $"{h.Key}: {Value}\n";
  775. }
  776. }
  777. _ = new LoggerLibrary("api_error", "fail")
  778. .Info(message, content)
  779. .Info(headers, body)
  780. .SaveAsync();
  781. break;
  782. }
  783. }
  784. if (success)
  785. {
  786. if (string.IsNullOrEmpty(itemName))
  787. {
  788. itemName = GetTitle(taoToken);
  789. }
  790. }
  791. else
  792. {
  793. itemName = GetTitle(content);
  794. }
  795. string deeplink_url = GetDeeplink(shortLinkurl);
  796. result.channel = TkChannelEnum.tb;
  797. result.accountId = _accountId;
  798. result.accountName = _accountName;
  799. if (success)
  800. {
  801. switch (result.link_type)
  802. {
  803. case LinkTypeEnum.profile:
  804. case LinkTypeEnum.goods:
  805. break;
  806. default:
  807. result.link_type = LinkTypeEnum.goods;
  808. break;
  809. }
  810. }
  811. result.success = success;
  812. result.message = message;
  813. result.content = content;
  814. result.couponAmount = couponAmount;
  815. result.couponEffectiveEndTime = couponEffectiveEndTime;
  816. result.couponEffectiveStartTime = couponEffectiveStartTime;
  817. result.taoToken = taoToken;
  818. result.shortLinkurl = shortLinkurl;
  819. result.deeplink_url = deeplink_url;
  820. result.itemId = itemId;
  821. result.itemName = itemName;
  822. result.promotionPrice = promotionPrice;
  823. result.sellerNickName = sellerNickName;
  824. result.shopTitle = shopTitle;
  825. result.pic = pic;
  826. result.qrCodeUrl = qrCodeUrl;
  827. }
  828. catch
  829. {
  830. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  831. {
  832. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  833. {
  834. throw new APIException("抱歉!页面无法访问……");
  835. }
  836. }
  837. throw;
  838. }
  839. return result;
  840. }
  841. }
  842. }