ks.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using dodohold.core;
  2. using CSRedis;
  3. using System.Data;
  4. namespace molilian.core
  5. {
  6. public partial class TkLogCore
  7. {
  8. static string queue_parse_ks_key = "queue:parse_logs:ks";
  9. public static int task_insert_parse_ks_logs(int limit, CSRedisClient redis)
  10. {
  11. int total = 0;
  12. using var connection = DBContext.GetOpenConnection();
  13. connection.Open();
  14. using var transaction = connection.BeginTransaction();
  15. try
  16. {
  17. for (int i = 0; i < limit; i++)
  18. {
  19. var data = redis.LPop<KsDataDTO>(queue_parse_ks_key);
  20. if (data == null) break;
  21. if (_test_oaid.Equals(data.oaid) || data.ip.StartsWith("127.0.0"))
  22. {
  23. save_ks_parse_logs(data, "ks_parse_logs_test", connection, transaction);
  24. }
  25. else
  26. {
  27. //save_ks_parse_logs(data, "ks_parse_logs", connection, transaction);
  28. //if (save_dailys_log)
  29. {
  30. string daily_table = $"ks_parse_logs_{data.create_time:yyyyMMdd}";
  31. save_ks_parse_logs(data, daily_table, connection, transaction);
  32. }
  33. if (data.success)
  34. {
  35. save_ks_parse_logs(data, "ks_parse_logs_success", connection, transaction);
  36. }
  37. }
  38. total++;
  39. }
  40. transaction.Commit();
  41. }
  42. catch (Exception ex)
  43. {
  44. transaction.Rollback();
  45. new LoggerLibrary("TkLogCore_error", "task_insert_parse_ks_logs")
  46. .Info(ex.Message, ex.StackTrace)
  47. .SaveAsync();
  48. throw;
  49. }
  50. finally
  51. {
  52. connection.Close();
  53. }
  54. return total;
  55. }
  56. public static async Task ParseLogAsync(KsDataDTO response)
  57. {
  58. try
  59. {
  60. var ts = DateTime.Now - response.create_time;
  61. response.elapsedTime = (int)ts.TotalMilliseconds;
  62. _ = RedisHelper.RPushAsync(queue_parse_ks_key, response);
  63. if (!response.ip.StartsWith("127.0.0"))
  64. {
  65. saveParseCache(response.channel.ToString(), response.accountId,
  66. response.accountName, response.success, response.message, response.reason,
  67. response.deeplink_url);
  68. }
  69. //if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);
  70. if (response.success || response.message.Equals("转链失败"))
  71. {
  72. RiskControlCore.CallsIncrBy(response.channel, response.accountId);
  73. saveClientRequestTotal(response.channel, response.ip, response.oaid);
  74. }
  75. if (!response.success && ("nologin".Equals(response.reason) ||
  76. "方法不存在".Equals(response.reason) ||
  77. "未登录".Equals(response.reason)))
  78. {
  79. await Task.Run(() =>
  80. {
  81. KsPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  82. });
  83. }
  84. if ("没有匹配账号".Equals(response.reason))
  85. {
  86. KsPoolCore.AccountExhausted();
  87. }
  88. if ("TOKEN过期".Equals(response.reason))
  89. {
  90. KsPoolCore.NotifyInterfaceError(response.reason);
  91. }
  92. }
  93. catch (Exception ex) { }
  94. }
  95. private static int save_ks_parse_logs(KsDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  96. {
  97. return new DBContext.Table(connection, tablename)
  98. .Add("end_point", data.end_point)
  99. .Add("channel", (int)data.channel)
  100. .Add("accountId", data.accountId)
  101. .Add("accountName", data.accountName)
  102. .Add("proxy_node", data.proxy_node)
  103. .Add("rawContent", data.rawContent)
  104. .Add("success", data.success)
  105. .Add("message", data.message)
  106. .Add("reason", data.reason)
  107. .Add("content", data.content)
  108. .Add("itemId", data.itemId)
  109. .Add("itemName", data.itemName)
  110. .Add("pic", data.pic)
  111. .Add("couponAmount", data.couponAmount)
  112. .Add("promotionPrice", data.promotionPrice)
  113. .Add("taoToken", data.taoToken)
  114. .Add("shortLinkurl", data.shortLinkurl)
  115. .Add("deeplink_url", data.deeplink_url)
  116. .Add("elapsedTime", data.elapsedTime)
  117. .Add("subCode", data.subCode)
  118. .Add("ip", data.ip)
  119. .Add("oaid", data.oaid)
  120. .Add("create_time", data.create_time)
  121. .Create(DBContext.InsertType.NORMAL, transaction);
  122. }
  123. }
  124. }