JdPoolCore.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. using Google.Protobuf.WellKnownTypes;
  11. using System.Xml.Linq;
  12. namespace molilian.core
  13. {
  14. public partial class JdPoolCore
  15. {
  16. public enum JdAction
  17. {
  18. all,
  19. parse,
  20. coupon,
  21. promotionQuery
  22. }
  23. private static string _end_point;
  24. private static Dictionary<string, decimal> _incomeAmt = new();
  25. private static Dictionary<string, int> _calls = new();
  26. static JdPoolCore()
  27. {
  28. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  29. }
  30. private static readonly object _lockObj = new();
  31. private static IEnumerable<JdPoolDTO> _cached;
  32. public static JdPoolDTO? GetOne()
  33. {
  34. var list = List();
  35. if (!list.Any()) return null;
  36. return list.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  37. }
  38. public static JdPoolDTO? GetOne(JdAction action)
  39. {
  40. var list = List();
  41. if (!list.Any()) return null;
  42. return list.Where(e => IsNotExceedDailyIncomeLimit(e, action)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  43. }
  44. public static JdPoolDTO? GetOne(int id, JdAction action)
  45. {
  46. var list = List();
  47. if (!list.Any()) return null;
  48. return list.Where(e => e.id == id && IsNotExceedDailyIncomeLimit(e, action)).FirstOrDefault();
  49. }
  50. //internal static void SaveIncomeAmt(int accountId, decimal income_amt)
  51. //{
  52. // string key = $"{accountId}:{DateTime.Now:yyyyMMdd}";
  53. // if (_incomeAmt.ContainsKey(key))
  54. // {
  55. // _incomeAmt[key] = income_amt;
  56. // }
  57. // else
  58. // {
  59. // _incomeAmt.Add(key, income_amt);
  60. // }
  61. // string cache_key = $"cache:jd_pool:{key}:income_amt";
  62. // var result = EndPointCore.ProcessEndPointNodes<bool>(node =>
  63. // {
  64. // if (!node.is_public_api) return true;
  65. // if (string.IsNullOrEmpty(node.redis_server)) return true;
  66. // var redis = RedisClientManager.GetRedisClient(node.redis_server);
  67. // return redis.Set(cache_key, income_amt, 3 * 86400);
  68. // });
  69. //}
  70. //public static decimal GetIncomeAmt(int accountId)
  71. //{
  72. // try
  73. // {
  74. // string key = $"{accountId}:{DateTime.Now:yyyyMMdd}";
  75. // if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
  76. // string cache_key = $"cache:jd_pool:{key}:income_amt";
  77. // return RedisHelper.Get<decimal>(cache_key);
  78. // }
  79. // catch (Exception ex) { return 0; }
  80. //}
  81. internal static void CallsIncrBy(int accountId)
  82. {
  83. CallsIncrBy(accountId, DateTime.Now.ToString("yyyyMMdd"));
  84. CallsIncrBy(accountId, DateTime.Now.ToString("yyyyMMddHH"));
  85. }
  86. internal static void CallsIncrBy(int accountId, string flag)
  87. {
  88. string key = $"{accountId}:{flag}";
  89. if (_calls.ContainsKey(key))
  90. {
  91. _calls[key] = _calls[key] + 1;
  92. }
  93. else
  94. {
  95. _calls[key] = 1;
  96. }
  97. string cache_key = $"cache:jd_pool:{key}:calls:{flag}";
  98. RedisHelper.IncrBy(cache_key);
  99. RedisHelper.Expire(cache_key, 3 * 86400);
  100. }
  101. public static int SaveCalls(int accountId, string flag)
  102. {
  103. string key = $"{accountId}:{flag}";
  104. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  105. {
  106. if (!node.is_public_api) return 0;
  107. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  108. #if DEBUG
  109. switch (node.name)
  110. {
  111. case "bj":
  112. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  113. break;
  114. case "gz":
  115. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  116. break;
  117. case "coupon1":
  118. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  119. break;
  120. default: return 0;
  121. }
  122. #endif
  123. string cache_key = $"cache:jd_pool:{key}:calls:{flag}";
  124. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  125. return redis.Get<int>(cache_key);
  126. });
  127. int num = result.Sum();
  128. _calls.TryAdd(key, num);
  129. return num;
  130. }
  131. public static int GetCalls(int accountId, string flag)
  132. {
  133. try
  134. {
  135. string key = $"{accountId}:{flag}";
  136. if (_calls.ContainsKey(key)) return _calls[key];
  137. string cache_key = $"cache:jd_pool:{key}:calls:{flag}";
  138. int num = RedisHelper.Get<int>(cache_key);
  139. _calls.TryAdd(key, num);
  140. return num;
  141. }
  142. catch (Exception ex) { return 0; }
  143. }
  144. private static bool IsNotExceedDailyIncomeLimit(JdPoolDTO item, JdAction action)
  145. {
  146. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  147. {
  148. if (item.end_point != _end_point) return false;
  149. }
  150. switch (action)
  151. {
  152. case JdAction.parse:
  153. if (!item.enable_parse) return false;
  154. break;
  155. case JdAction.coupon:
  156. if (!item.enable_coupon) return false;
  157. break;
  158. }
  159. // 使用初始化参数创建工作时间表
  160. if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
  161. if (item.daily_calls_limit > 0)
  162. {
  163. int daily_num = GetCalls(item.id, DateTime.Now.ToString("yyyyMMdd"));
  164. if (daily_num >= item.daily_calls_limit) return false;
  165. }
  166. if (item.hourly_calls_limit > 0)
  167. {
  168. int hourly_num = GetCalls(item.id, DateTime.Now.ToString("yyyyMMddHH"));
  169. if (hourly_num >= item.hourly_calls_limit) return false;
  170. }
  171. return true;
  172. }
  173. public static IEnumerable<JdPoolDTO> List(bool force = false)
  174. {
  175. if (!force && _cached != null) return _cached;
  176. string cache_key = $"cache:jd_pool";
  177. var list = RedisHelper.Get<IEnumerable<JdPoolDTO>>(cache_key);
  178. if (force || list == null)
  179. {
  180. lock (_lockObj)
  181. {
  182. list = new DBContext.Table("jd_pool")
  183. .Where("status=@status", new { status = 1 })
  184. .Select<JdPoolDTO>();
  185. if (list == null) return default;
  186. _calls = new Dictionary<string, int>();
  187. foreach (var item in list)
  188. {
  189. string key = $"{item.id}:{DateTime.Now:yyyyMMddHH}";
  190. _calls.TryAdd(key, item.current_hourly_calls);
  191. key = $"{item.id}:{DateTime.Now:yyyyMMddHH}";
  192. _calls.TryAdd(key, item.current_daily_calls);
  193. }
  194. RedisHelper.Set(cache_key, list, 30 * 86400);
  195. }
  196. }
  197. _cached = list;
  198. return list;
  199. }
  200. public static void Refresh()
  201. {
  202. _ = List(true);
  203. }
  204. public static int Update(JdPoolDTO account)
  205. {
  206. return new DBContext.Table("jd_pool")
  207. .Add("current_hourly_calls", account.current_hourly_calls)
  208. .Add("current_daily_calls", account.current_daily_calls)
  209. .Add("today_clickNum", account.today_clickNum)
  210. .Add("today_cosFee", account.today_cosFee)
  211. .Add("today_cosPrice", account.today_cosPrice)
  212. .Add("today_finishCosFee", account.today_finishCosFee)
  213. .Add("today_finishCosPrice", account.today_finishCosPrice)
  214. .Add("today_finishOrderNum", account.today_finishOrderNum)
  215. .Add("today_orderNum", account.today_orderNum)
  216. .Where("id=@id", new { account.id })
  217. .Update();
  218. }
  219. public static int UpdateCookies(string cookies, string user_agent)
  220. {
  221. if (string.IsNullOrEmpty(cookies)) return 0;
  222. string pin = cookies.GetContentPart("pin=", ";");
  223. pin = pin.UrlDecode();
  224. string company = pin;
  225. int accountId = 0;
  226. if (string.IsNullOrEmpty(pin)) return 0;
  227. var exist = new DBContext.Table("jd_pool").Get<JdPoolDTO>("pin=@pin", new { pin });
  228. if (exist != null)
  229. {
  230. var status = exist.status;
  231. var work_mode = exist.work_mode;
  232. accountId = exist.id;
  233. if (work_mode == JdUnionWorkMode.Crawler) status = true;
  234. new DBContext.Table("jd_pool")
  235. .Add("pin", pin)
  236. .Add("cookies", cookies)
  237. .Add("user_agent", user_agent)
  238. .Add("status", status)
  239. .Add("last_time", DateTime.Now)
  240. .Add("login_time", DateTime.Now)
  241. .Where("id=@id", new { exist.id })
  242. .Update();
  243. if (status) _ = List(true);
  244. }
  245. else
  246. {
  247. accountId = new DBContext.Table("jd_pool")
  248. .Add("pin", pin)
  249. .Add("name", pin)
  250. .Add("company", pin)
  251. .Add("description", "由cookies上报创建此记录")
  252. .Add("cookies", cookies)
  253. .Add("user_agent", user_agent)
  254. .Add("create_time", DateTime.Now)
  255. .Add("last_time", DateTime.Now)
  256. .Add("login_time", DateTime.Now)
  257. .Add("status", 0)
  258. .Create();
  259. }
  260. NotifyCore.Notify(new NifyMessage
  261. {
  262. message = $"【京东{accountId}:{company}】cookie 上线",
  263. tags = ["green_circle"]
  264. });
  265. EndPointCore.NotifyReload(true);
  266. //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  267. return accountId;
  268. }
  269. public static void Disabled(int accountId, string name, string content)
  270. {
  271. string cache_key = $"cache:jd_pool:{name}:disabled";
  272. long count = RedisHelper.IncrBy(cache_key);
  273. RedisHelper.Expire(cache_key, 10);
  274. if (count > 1) return;
  275. var update = new DBContext.Table("jd_pool").Add("status", 0);
  276. if (accountId > 0)
  277. {
  278. update.Where("id=@accountId", new { accountId }).Update();
  279. }
  280. else
  281. {
  282. update.Where("name=@name", new { name }).Update();
  283. }
  284. _ = List(true);
  285. NotifyCore.Notify(new NifyMessage
  286. {
  287. message = $"【京东联盟{accountId}:{name}】cookie 掉线\n\n{content}",
  288. priority = NifyMessagePriority.high,
  289. tags = ["red_circle"]
  290. });
  291. NotifyCore.AnPushNotify("掉线", $"【京东{accountId}:{name}】cookie 掉线");
  292. EndPointCore.NotifyReload(true);
  293. }
  294. internal static void AccountExhausted()
  295. {
  296. string cache_key = $"cache:tk_pool:account:exhausted";
  297. long count = RedisHelper.IncrBy(cache_key);
  298. if (count > 1) return;
  299. RedisHelper.Expire(cache_key, 3600);
  300. NotifyCore.Notify(new NifyMessage
  301. {
  302. message = $"【京东联盟】没有匹配账号",
  303. priority = NifyMessagePriority.high,
  304. tags = ["red_circle"]
  305. });
  306. NotifyCore.AnPushNotify("没账号", $"【京东联盟】没有匹配账号");
  307. }
  308. }
  309. }