moose_share.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using dodohold.core;
  2. using System.Diagnostics;
  3. using System.Text.Json;
  4. namespace molilian.core
  5. {
  6. public partial class AlimamaPlus
  7. {
  8. public async Task<TkDataDTO> endpoint_moose_share(string content, TkDataDTO result, CancellationToken cancellationToken = default)
  9. {
  10. string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
  11. Random random = new();
  12. double randomNumber = random.NextDouble();
  13. string randomString = randomNumber.ToString()[2..];
  14. string cna = _cookies.GetContentPart("cna=", ";");
  15. string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
  16. //var variableMap = new
  17. //{
  18. // url = content,
  19. // union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
  20. // lensScene = "PUB",
  21. // spmB = "_portal_v2_tool_links_page_home_index_htm"
  22. //}.Convert2Json(true).UrlEncode();
  23. string item_id = content.GetContentPart("item.htm?id=", "");
  24. var bizParam = new
  25. {
  26. itemId = item_id,
  27. pid = _account.refpid,
  28. creativeType = "creativeAll"
  29. }.Convert2Json(true).UrlEncode();
  30. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/union.moose.share.entry";
  31. string post = $"bizType=share&bizParam={bizParam}";
  32. Stopwatch stopwatch = Stopwatch.StartNew();
  33. stopwatch.Start();
  34. var client = new WebClientUtility().SetContentType("application/x-www-form-urlencoded")
  35. .AddHeaders("Origin", "https://pub.alimama.com")
  36. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  37. .AddHeaders("Cookie", _cookies);
  38. client.Proxy = _proxy;
  39. #if DEBUG
  40. client.Proxy = null;
  41. #endif
  42. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  43. client.Timeout = GetRequestTimeout2(result.elapsedTime2);
  44. //var response = client.Request(url);
  45. #if DEBUG
  46. var response = await client.Post(post).RequestAsync(url, "POST");
  47. #else
  48. var response = await client.Post(post).RequestAsync(url, "POST", cancellationToken);
  49. #endif
  50. stopwatch.Stop();
  51. result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
  52. var body = string.Empty;
  53. try
  54. {
  55. if (!response.Successed)
  56. {
  57. result.channel = TkChannelEnum.tb;
  58. result.link_type = LinkTypeEnum.unknown;
  59. result.success = false;
  60. result.message = "fail";
  61. if (response.ResponseException != null)
  62. {
  63. _ = new LoggerLibrary("api_error", "fail")
  64. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  65. .SaveAsync();
  66. throw response.ResponseException;
  67. }
  68. return result;
  69. }
  70. if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.Found)
  71. {
  72. _ = new LoggerLibrary("api_error", "fail")
  73. .Info($"302:{response.ResponseMessage.Headers.Location}")
  74. .SaveAsync();
  75. throw new Exception("302 Found, was canceled");
  76. }
  77. JsonElement root;
  78. try
  79. {
  80. body = response.Body();
  81. //{"success":false,"message":"result empty"}
  82. root = body.Convert2JsonElement();
  83. }
  84. catch (Exception ex)
  85. {
  86. _ = new LoggerLibrary("api_error", "fail")
  87. .Info(ex.Message, ex.StackTrace)
  88. .SaveAsync();
  89. if (body.Contains("https://g.alicdn.com/sd/punish/waf_block.html"))
  90. {
  91. await TkEndpointManager.SuspendAsync(_accountId, "moose_share", TkEndpointManager.SuspendReason.Passive);
  92. result.channel = TkChannelEnum.tb;
  93. result.link_type = LinkTypeEnum.unknown;
  94. result.subCode = TkSubCodeEnum.ParseDeny;
  95. result.success = false;
  96. result.message = "waf_block";
  97. result.reason = "接口风控";
  98. return result;
  99. }
  100. throw new Exception(body);
  101. }
  102. bool success = root.Read<bool>("success", false);
  103. string err_url = root.Read("url", string.Empty);
  104. if (!success)
  105. {
  106. //{"rgv587_flag": "sm", "url": "https://bixi.alicdn.com/punish/punish:resource:template:baba:default_35969158.html?qrcode=oPTbncSdl-B_dxCchZVrhw|aBW9wQ|MmZvng_0|BrNc-A&uuid=a0f4db9dc49d97e07f77109c85956b87&action=deny&origin=https%3A%2F%2Fpub.alimama.com%2Fopenapi%2Fparam2%2F1%2Fgateway.unionpub%2Funion.moose.share.entry&pureDenyWait="}
  107. if (err_url.Contains("bixi.alicdn.com"))
  108. {
  109. await TkEndpointManager.SuspendAsync(_accountId, "moose_share", TkEndpointManager.SuspendReason.Passive);
  110. result.channel = TkChannelEnum.tb;
  111. result.link_type = LinkTypeEnum.unknown;
  112. result.subCode = TkSubCodeEnum.ParseDeny;
  113. result.success = false;
  114. result.message = "waf_block";
  115. result.reason = "接口风控";
  116. return result;
  117. }
  118. }
  119. string message = root.Read("message", string.Empty);
  120. string info_message = root.PathRead("msgInfo", string.Empty);
  121. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  122. switch (message)
  123. {
  124. case "detail result is empty itemId={}":
  125. case "itemId params error":
  126. message = "result empty";
  127. result.subCode = TkSubCodeEnum.NoConvert;
  128. break;
  129. }
  130. JsonElement item = default;
  131. JsonElement creativityCopy = default;
  132. if (success)
  133. {
  134. var data = root.ElementRead("model");
  135. var resultList = data.ElementRead("resultList");
  136. if (resultList.ValueKind != JsonValueKind.Array)
  137. {
  138. _ = new LoggerLibrary("api_error", "fail")
  139. .Info("resultList Undefined", body)
  140. .SaveAsync();
  141. throw new Exception("resultList Undefined");
  142. }
  143. item = resultList[0];
  144. var creativityCopyArr = item.ElementRead("creativityCopy");
  145. creativityCopy = creativityCopyArr[0];
  146. }
  147. string cpsShortToken = creativityCopy.Read("cpsShortToken", string.Empty);
  148. string cpsShortLinkUrl = creativityCopy.Read("cpsShortLinkUrl", string.Empty);
  149. string itemName = creativityCopy.Read("itemName", string.Empty);
  150. string taoToken = $"{cpsShortToken} {cpsShortLinkUrl} {itemName}";
  151. string shortLinkurl = creativityCopy.Read("cpsShortLinkUrl", string.Empty);
  152. string couponLinkTaoToken = item.Read("couponLinkTaoToken", string.Empty);
  153. string couponShortLinkUrl = item.Read("couponShortLinkUrl", string.Empty);
  154. decimal couponAmount = item.Read<decimal>("couponAmount", 0);
  155. string couponEffectiveEndTime = item.Read("couponEffectiveEndTime", string.Empty);
  156. string couponEffectiveStartTime = item.Read("couponEffectiveStartTime", string.Empty);
  157. string itemId = item.Read("itemId", string.Empty);
  158. itemId = item_id;//用数字id覆盖
  159. string mktId = item.Read("outputMktId", string.Empty);
  160. decimal promotionPrice = item.Read<decimal>("promotionPrice", 0);
  161. string sellerNickName = item.Read("sellerNickName", string.Empty);
  162. string shopTitle = item.Read("shopTitle", string.Empty);
  163. string pic = item.Read("pic", string.Empty);
  164. if (pic.StartsWith("//")) pic = "https:" + pic;
  165. string qrCodeUrl = item.Read("qrCodeUrl", string.Empty);
  166. if (response.ResponseMessage != null)
  167. {
  168. switch (response.ResponseMessage.StatusCode)
  169. {
  170. case System.Net.HttpStatusCode.OK:
  171. {
  172. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  173. {
  174. taoToken = couponLinkTaoToken;
  175. shortLinkurl = couponShortLinkUrl;
  176. }
  177. string shortLink = GetLink(taoToken);
  178. content = ReplaceUrls(content, shortLink);
  179. }
  180. break;
  181. case System.Net.HttpStatusCode.Found:
  182. string location = response.ResponseMessage.Headers.Location.OriginalString;
  183. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  184. {
  185. success = false;
  186. message = "nologin";
  187. }
  188. break;
  189. default:
  190. {
  191. success = false;
  192. message = "other";
  193. }
  194. break;
  195. }
  196. }
  197. if (!success)
  198. {
  199. shortLinkurl = GetLink(content);
  200. switch (message)
  201. {
  202. case "该商品已经下架或未加入淘宝客":
  203. case "该链接不支持转化,请更换链接尝试":
  204. case "result empty":
  205. result.subCode = TkSubCodeEnum.NoConvert;
  206. break;
  207. case "网络错误":
  208. result.subCode = TkSubCodeEnum.NetError;
  209. break;
  210. default:
  211. var _headers = response.ResponseMessage.Headers;
  212. var headers = "";
  213. foreach (var h in _headers)
  214. {
  215. foreach (var Value in h.Value)
  216. {
  217. headers += $"{h.Key}: {Value}\n";
  218. }
  219. }
  220. _ = new LoggerLibrary("api_error", "fail")
  221. .Info(message, content)
  222. .Info(headers, body)
  223. .SaveAsync();
  224. break;
  225. }
  226. }
  227. shortLinkurl = GetLink(taoToken);
  228. string deeplink_url = GetDeeplink(shortLinkurl);
  229. result.channel = TkChannelEnum.tb;
  230. if (success)
  231. {
  232. result.subCode = TkSubCodeEnum.Success;
  233. switch (result.link_type)
  234. {
  235. case LinkTypeEnum.profile:
  236. case LinkTypeEnum.goods:
  237. case LinkTypeEnum.video:
  238. case LinkTypeEnum.live:
  239. break;
  240. default:
  241. result.link_type = LinkTypeEnum.goods;
  242. break;
  243. }
  244. }
  245. result.success = success;
  246. result.message = message;
  247. result.reason = string.Empty;
  248. result.content = content;
  249. result.couponAmount = couponAmount;
  250. result.couponEffectiveEndTime = couponEffectiveEndTime;
  251. result.couponEffectiveStartTime = couponEffectiveStartTime;
  252. result.taoToken = taoToken;
  253. if (!string.IsNullOrEmpty(shortLinkurl))
  254. {
  255. result.shortLinkurl = shortLinkurl;
  256. result.deeplink_url = deeplink_url;
  257. }
  258. result.mktId = mktId;
  259. result.itemId = itemId;
  260. result.itemName = itemName;
  261. result.promotionPrice = promotionPrice;
  262. result.sellerNickName = sellerNickName;
  263. result.shopTitle = shopTitle;
  264. result.pic = pic;
  265. result.qrCodeUrl = qrCodeUrl;
  266. }
  267. catch (Exception ex)
  268. {
  269. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  270. {
  271. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  272. {
  273. throw new APIException("抱歉!页面无法访问……");
  274. }
  275. }
  276. throw;
  277. }
  278. return result;
  279. }
  280. }
  281. }