EstimateRevenue.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using dodohold.core;
  2. using Sayaka.Common;
  3. using System.Drawing.Printing;
  4. using System.Text.Json;
  5. namespace molilian.core
  6. {
  7. public partial class PddUnionPlus
  8. {
  9. public async Task queryEstimateRevenueList(DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default)
  10. {
  11. // 隐藏号不同步任何收益数据
  12. if (_account.is_hide) return;
  13. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  14. string cookies = _account.cookies;
  15. bool hasNext = true;
  16. int pageNo = 1;
  17. int pageSize = 100;
  18. while (hasNext)
  19. {
  20. var args = new
  21. {
  22. startTime = startTime.ToString("yyyy-MM-dd"),
  23. endTime = endTime.ToString("yyyy-MM-dd"),
  24. orderPromotionType = 0,
  25. pageNumber = pageNo,
  26. pageSize
  27. };
  28. DateTime stime = DateTime.Now;
  29. string url = "https://jinbao.pinduoduo.com/network/api/order/stat/overall/list";
  30. var root = await this.RequestAsync(url, args.Convert2Json());
  31. TimeSpan ts2 = DateTime.Now - stime;
  32. string xx = ts2.TotalSeconds.ToString();
  33. var success = root.Read<bool>("success");
  34. if (!success) break;
  35. var list = root.ElementRead("result").ElementRead("list");
  36. string filter = "accountId=@accountId AND report_date=@report_date AND pid=@pid";
  37. var len = list.GetArrayLength();
  38. if (len == 0) break;
  39. foreach (var item in list.EnumerateArray())
  40. {
  41. var report = item.GetRawText().Convert2Object<PddEstimateRevenueDTO>();
  42. var exist = new DBContext.Table("pdd_estimate_revenue")
  43. .Fields("id")
  44. .Get<dynamic>(filter, new { report_date = report.calcDate, accountId = _account.id, report.pid });
  45. var update = new DBContext.Table("pdd_estimate_revenue")
  46. .Add("accountId", _account.id)
  47. .Add("accountName", _account.name)
  48. .Add("report_date", report.calcDate)
  49. .Add("pid", report.pid)
  50. .Add("pidName", report.pidName)
  51. .Add("orderNum", report.orderNum)
  52. .Add("orderAmount", report.orderAmount)
  53. .Add("feeAmount", report.feeAmount)
  54. .Add("avgPrice", report.avgPrice)
  55. .Add("mallRelFeeAmount", report.mallRelFeeAmount)
  56. .Add("lockFeeAmount", report.lockFeeAmount)
  57. .Add("last_time", DateTime.Now);
  58. if (exist == null)
  59. {
  60. update.Add("report_date", report.calcDate)
  61. .Add("create_time", DateTime.Now)
  62. .Create(DBContext.InsertType.REPLACE);
  63. }
  64. else
  65. {
  66. update.Where("id=@id", new { exist.id }).Update();
  67. }
  68. }
  69. pageNo++;
  70. }
  71. }
  72. public async Task<int> queryMallBalance(CancellationToken cancellationToken = default)
  73. {
  74. try
  75. {
  76. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  77. string cookies = _account.cookies;
  78. DateTime stime = DateTime.Now;
  79. string url = "https://jinbao.pinduoduo.com/network/api/finance/mallBalance";
  80. var root = await this.RequestAsync(url);
  81. TimeSpan ts2 = DateTime.Now - stime;
  82. string xx = ts2.TotalSeconds.ToString();
  83. var success = root.Read<bool>("success");
  84. if (!success) throw new Exception(root.Convert2Json());
  85. var balanceAmount = root.PathRead<int>("result.balanceAmount");
  86. var withdrawalAmount = root.PathRead<int>("result.withdrawalAmount");
  87. return new DBContext.Table("pdd_pool")
  88. .Add("draw_balance", withdrawalAmount * 0.001)
  89. .Where("id=@id", new { _account.id })
  90. .Update();
  91. }
  92. catch (Exception ex)
  93. {
  94. if (ex.Message.Contains("43001:会话已过期"))
  95. {
  96. PddPoolCore.CookieDisabled(_account.id);
  97. }
  98. string message = $"【PDD报表接口异常】{_account.id}:{_account.name}\tmallBalance\n{ex.Message}\n{ex.StackTrace}";
  99. NotifyCore.Notify(new NifyMessage
  100. {
  101. message = message,
  102. priority = NifyMessagePriority.high,
  103. tags = ["red_circle"]
  104. });
  105. }
  106. return 0;
  107. }
  108. public async Task<JsonElement> RequestAsync(string url, string data = null, CancellationToken cancellationToken = default)
  109. {
  110. string cookies = _account.cookies.Trim();
  111. string useragent = ProviderFakeUserAgent.RandomComputer;
  112. WebClientUtility client = new WebClientUtility();
  113. client.Proxy = _proxy;
  114. #if DEBUG
  115. client.Proxy = null;
  116. #endif
  117. client.SetContentType("application/json");
  118. client.AddHeaders("Referer", "https://jinbao.pinduoduo.com/data/data-analysis");
  119. client.AddHeaders("Origin", "https://jinbao.pinduoduo.com");
  120. client.UserAgent = useragent;
  121. client.AddHeaders("Cookie", cookies);
  122. if (!string.IsNullOrEmpty(data)) client.Post(data);
  123. var response = await client.RequestAsync(url, string.IsNullOrEmpty(data) ? "GET" : "POST", cancellationToken);
  124. string body = response.Body();
  125. var root = body.Convert2JsonElement();
  126. //string message = string.Empty;
  127. // todo 这里我会做一些 日志 处理
  128. var success = root.Read<bool>("success");
  129. if (!success)
  130. {
  131. var errorCode = root.Read<int>("errorCode");
  132. var errorMsg = root.Read<string>("errorMsg");
  133. _ = new LoggerLibrary("PddUnion", "unsuccessful").Info(url, data).Info(body).SaveAsync();
  134. throw new APIException($"{errorCode}:{errorMsg}");
  135. }
  136. return root;
  137. }
  138. }
  139. }