parse_2.cs 38 KB

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