parse_2.cs 38 KB

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