Crawler.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using dodohold.core;
  2. using Sayaka.Common;
  3. using System.Text.RegularExpressions;
  4. namespace molilian.core
  5. {
  6. public class PddTransferUrlResult
  7. {
  8. public string multiGroupShortUrl { get; set; } = string.Empty;
  9. public string multiGroupUrl { get; set; } = string.Empty;
  10. public string shortUrl { get; set; } = string.Empty;
  11. public string url { get; set; } = string.Empty;
  12. }
  13. public class PddTransferUrlResponse
  14. {
  15. public int errorCode { get; set; }
  16. public string errorMsg { get; set; } = string.Empty;
  17. public bool success { get; set; } = false;
  18. public PddTransferUrlResult result { get; set; } = new();
  19. }
  20. public class PddCreatePromotionUrlResult
  21. {
  22. public string multiGroupShortUrl { get; set; } = string.Empty;
  23. public string multiGroupUrl { get; set; } = string.Empty;
  24. public string shortUrl { get; set; } = string.Empty;
  25. public string url { get; set; } = string.Empty;
  26. }
  27. public class PddCreatePromotionUrlResponse
  28. {
  29. public int errorCode { get; set; }
  30. public string errorMsg { get; set; } = string.Empty;
  31. public bool success { get; set; } = false;
  32. public PddCreatePromotionUrlResult result { get; set; } = new();
  33. }
  34. public partial class PddUnionPlus
  35. {
  36. public async Task<PddDataDTO> transferUrl(PddDataDTO result, string sourceUrl, CancellationToken cancellationToken = default)
  37. {
  38. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  39. string cookies = _account.cookies.Trim();
  40. #if DEBUG
  41. cookies = "_nano_fp=Xpm8nqXjl0djX5TYnC_i8vSD~GoSCs6EwSX~KQF3; api_uid=ZeIuHGnwfkePzgBjEGupAg==; jrpl=FYxQYhHDRjBKXQBaaFrEUyyNgiOx9FjB; njrpl=FYxQYhHDRjBKXQBaaFrEUyyNgiOx9FjB; dilx=u8i4bJTsT3tQaAKzP5jRQ; DDJB_PASS_ID=7752af5c1a8d94018b900b8c34d5a362; DDJB_LOGIN_SCENE=0";
  42. _account.pid = "25613623_309553545";
  43. #endif
  44. string useragent = ProviderFakeUserAgent.RandomComputer;
  45. string url = "https://jinbao.pinduoduo.com/network/api/promotion/transferUrl";
  46. //{"pid":"13848803_189356569","sourceUrl":"https://p.pinduoduo.com/1OAsct0r"}
  47. var args = new
  48. {
  49. _account.pid,
  50. sourceUrl = sourceUrl.UrlDecode(),
  51. };
  52. string data = args.Convert2Json();
  53. WebClientUtility client = new WebClientUtility();
  54. client.Proxy = _proxy;
  55. #if DEBUG
  56. client.Proxy = null;
  57. #endif
  58. client.SetContentType("application/json");
  59. client.AddHeaders("Referer", "https://jinbao.pinduoduo.com/promotion/url-promotion");
  60. client.AddHeaders("Origin", "https://jinbao.pinduoduo.com");
  61. client.UserAgent = useragent;
  62. client.AddHeaders("Cookie", cookies);
  63. client.Post(data);
  64. var response = await client.RequestAsync(url, "POST", cancellationToken);
  65. string body = response.Body();
  66. //{"success":false,"errorCode":43001,"errorMsg":"会话已过期","result":null}
  67. string statusCode = response.ResponseMessage == null
  68. ? "No response"
  69. : $"{(int)response.ResponseMessage.StatusCode} {response.ResponseMessage.ReasonPhrase}";
  70. string responseBody = string.IsNullOrWhiteSpace(body) ? "<empty>" : body;
  71. if (!response.Successed || response.ResponseMessage?.IsSuccessStatusCode != true)
  72. {
  73. string httpError = response.ResponseException?.Message ?? "<none>";
  74. string httpFailureMessage = $"PDD transferUrl HTTP request failed.\nStatus: {statusCode}\nError: {httpError}\nBody:\n{responseBody}";
  75. await new LoggerLibrary("PddUnion", "transferUrl.HttpError")
  76. .Info(httpFailureMessage, response.ResponseException?.StackTrace)
  77. .SaveAsync();
  78. throw new HttpRequestException(
  79. httpFailureMessage,
  80. response.ResponseException,
  81. response.ResponseMessage?.StatusCode);
  82. }
  83. PddTransferUrlResponse root;
  84. try
  85. {
  86. root = body.Convert2Object<PddTransferUrlResponse>();
  87. if (root == null)
  88. {
  89. throw new InvalidOperationException("PDD transferUrl response deserialized to null.");
  90. }
  91. }
  92. catch (Exception ex)
  93. {
  94. string deserializeFailureMessage = $"PDD transferUrl response deserialization failed.\nStatus: {statusCode}\nBody:\n{responseBody}";
  95. await new LoggerLibrary("PddUnion", "transferUrl.DeserializeError")
  96. .Info(deserializeFailureMessage)
  97. .Info(ex.Message, ex.StackTrace)
  98. .SaveAsync();
  99. throw new InvalidOperationException(deserializeFailureMessage, ex);
  100. }
  101. string message = string.Empty;
  102. if (!root.success)
  103. {
  104. if ("30007".Equals(root.errorCode))
  105. {
  106. PddPoolCore.AccountRisk(_account);
  107. }
  108. message = $"{root.errorCode}:{root.errorMsg}";
  109. result.success = false;
  110. result.message = "转链失败";
  111. result.reason = message;
  112. _ = new LoggerLibrary("PddUnion", "transferUrl").Info(body).SaveAsync();
  113. return result;
  114. }
  115. if (string.IsNullOrEmpty(root.result?.multiGroupShortUrl))
  116. {
  117. result.success = false;
  118. result.message = "转链失败";
  119. result.reason = "无法转链";
  120. //_ = new LoggerLibrary("PddUnion", "transferUrl").Info(body).SaveAsync();
  121. return result;
  122. }
  123. if (!string.IsNullOrEmpty(root.result?.url))
  124. {
  125. result.itemId = root.result?.url.GetContentPart("goods_id=", "&");
  126. }
  127. result.success = true;
  128. result.message = "OK";
  129. result.commercial = true;
  130. result.link_type = LinkTypeEnum.goods;
  131. result.shortLinkurl = root.result?.multiGroupShortUrl;
  132. result.content = result.shortLinkurl;
  133. result.deeplink_url = GetDeeplink(result.shortLinkurl);
  134. return result;
  135. }
  136. public async Task<PddDataDTO> createPromotionUrl(PddDataDTO result, string sourceUrl, CancellationToken cancellationToken = default)
  137. {
  138. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  139. string cookies = _account.cookies.Trim();
  140. string useragent = ProviderFakeUserAgent.RandomComputer;
  141. string url = "https://jinbao.pinduoduo.com/network/api/promotion/createPromotionUrl";
  142. //{"pid":"13848803_189356569","sourceUrl":"https://p.pinduoduo.com/1OAsct0r"}
  143. string goodsId = sourceUrl;
  144. var args = new
  145. {
  146. _account.pid,
  147. goodsId,
  148. generateOpenCoupon = true
  149. };
  150. /*
  151. {
  152. "mediaId": "9092702778",
  153. "pid": "13848803_189356569",
  154. "goodsId": "123",
  155. "generateOpenCoupon": true
  156. }
  157. */
  158. string data = args.Convert2Json();
  159. WebClientUtility client = new WebClientUtility();
  160. client.Proxy = _proxy;
  161. #if DEBUG
  162. client.Proxy = null;
  163. #endif
  164. client.SetContentType("application/json");
  165. client.AddHeaders("Referer", "https://jinbao.pinduoduo.com/promotion/url-promotion");
  166. client.AddHeaders("Origin", "https://jinbao.pinduoduo.com");
  167. client.UserAgent = useragent;
  168. client.AddHeaders("Cookie", cookies);
  169. client.Post(data);
  170. var response = await client.RequestAsync(url, "POST", cancellationToken);
  171. string body = response.Body();
  172. var root = body.Convert2Object<PddCreatePromotionUrlResponse>();
  173. string message = string.Empty;
  174. if (!root.success)
  175. {
  176. message = $"{root.errorCode}:{root.errorMsg}";
  177. result.success = false;
  178. result.message = "转链失败";
  179. result.reason = message;
  180. _ = new LoggerLibrary("PddUnion", "transferUrl").Info(body).SaveAsync();
  181. return result;
  182. }
  183. if (string.IsNullOrEmpty(root.result?.multiGroupShortUrl))
  184. {
  185. result.success = false;
  186. result.message = "转链失败";
  187. result.reason = "无法转链";
  188. //_ = new LoggerLibrary("PddUnion", "transferUrl").Info(body).SaveAsync();
  189. return result;
  190. }
  191. //goods_id=636365867069&pid=13848803_189356569&goods_sign=E9z2NkASdqlKuWDVwvfdqnADgCEqTQSt_JuMe2WC0G&zs_duo_id=25194414&cpsSign=CC_240807_13848803_189356569_fdbbdfa499058644becc9f8b3b7acaaf&_x_ddjb_act=
  192. if (!string.IsNullOrEmpty(root.result?.url))
  193. {
  194. result.itemId = root.result?.url.GetContentPart("goods_id=", "&");
  195. }
  196. result.success = true;
  197. result.message = "OK";
  198. result.commercial = true;
  199. result.link_type = LinkTypeEnum.goods;
  200. result.shortLinkurl = root.result?.multiGroupShortUrl;
  201. result.content = result.shortLinkurl;
  202. result.deeplink_url = GetDeeplink(result.shortLinkurl);
  203. return result;
  204. }
  205. }
  206. }