/// <summary> /// 获取新闻具体内容 /// </summary> /// <param name="token"></param> /// <param name="model"></param> public void GetNewsContnt(JToken token, Domain.Coin.CoinArticel.CoinArticel model) { try { var htmlWeb = new HtmlWeb(); var document = htmlWeb.Load(token["topic_url"].ToString()); var node = document.DocumentNode.SelectSingleNode("//div[@class='js-article-detail']") ?? document.DocumentNode.SelectSingleNode("//div[@class='js-article']"); if (node != null) { foreach (var item in node.ChildNodes) { if (item.InnerText.Contains("文|") || item.InnerText.Contains("编辑|")) { item.InnerHtml = ""; } } model.Content = node.InnerHtml; } model.CreateTime = DateTimeConvert.ConvertDateTime(token["published_at"].ToString()); var logo = "http://pic.51yuansu.com/pic3/cover/02/61/11/59fc30d0b8598_610.jpg"; if (!string.IsNullOrWhiteSpace(token["thumbnail_pic"].ToString())) { logo = token["thumbnail_pic"].ToString(); } model.Logo = logo; model.Author = token["author"].ToString(); } catch (Exception ex) { _logger.LogError(new EventId(ex.HResult), ex, "---GetNewsContnt Exception---"); } }
/// <summary> /// 获取K线 /// </summary> /// <param name="time"></param> /// <param name="size"></param> /// <returns></returns> public List <CoinKLineData> GetKLine(string coin, string quote, string time, int size) { try { var http = new WebClient(); var result = http.DownloadString($"https://api.huobiasia.vip/market/history/kline?period={time}&size={size}&symbol={coin + quote}"); if (string.IsNullOrWhiteSpace(result)) { return(null); } var jobject = JObject.Parse(result); var json = jobject?["data"]; var kline = json?.Select(item => new CoinKLineData { Id = Guid.NewGuid().ToString(), IsDelete = 0, Platform = (int)Platform.Huobi, Coin = coin, DataTime = DateTimeConvert.ConvertDateTime(item["id"].ToString()), Open = Convert.ToDouble(item["open"]), Close = Convert.ToDouble(item["close"]), Low = Convert.ToDouble(item["low"]), High = Convert.ToDouble(item["high"]), CreateTime = DateTime.Now }).ToList(); return(kline); } catch (WebException ex) { _logger.LogError(new EventId(ex.HResult), ex, $"---GetKLine {DateTime.Now}---"); throw; } }
public void AddKLineData(string coin) { try { using var scope = _scopeFactory.CreateScope(); using var db_context = scope.ServiceProvider.GetRequiredService <CoinDbContext>(); var new_list = new List <Domain.Coin.CoinKLineData.CoinKLineData>(); var old_datatime = new List <DateTime>(); foreach (CoinTime time in Enum.GetValues(typeof(CoinTime))) { var max = db_context.CoinKLineData.Where(x => x.Coin.Equals(coin) && x.IsDelete == 0 && x.TimeType == (int)time && x.Platform == (int)Platform.Huobi).OrderByDescending(x => x.DataTime) .FirstOrDefault(); var size = 2000; if (max != null) { size = GetSize(max.DataTime, time); } var kline = ""; var retry = Policy.Handle <WebException>().Retry(3, (ex, count, text) => { _logger.LogError(new EventId(ex.HResult), ex, $"---CoinKLineDataJob GetKLine Exception,进行重试 {count}次---"); Thread.Sleep(100); }); retry.Execute(() => { kline = _huobi.GetKLine(coin + "usdt", time.ToString().Split('_')[1], size); }); if (string.IsNullOrWhiteSpace(kline)) { continue; } var jobject = JObject.Parse(kline); var json = jobject?["data"]; if (json == null) { continue; } var list = new List <Domain.Coin.CoinKLineData.CoinKLineData>(); foreach (var item in json) { var data_time = DateTimeConvert.ConvertDateTime(item["id"].ToString()); var model = new Domain.Coin.CoinKLineData.CoinKLineData { Id = Guid.NewGuid().ToString(), IsDelete = 0, Platform = (int)Platform.Huobi, Coin = coin, TimeType = (int)time, DataTime = data_time, Open = Convert.ToDouble(item["open"]), Close = Convert.ToDouble(item["close"]), Low = Convert.ToDouble(item["low"]), High = Convert.ToDouble(item["high"]), CreateTime = DateTime.Now }; old_datatime.Add(data_time); list.Add(model); } var old_list = db_context.CoinKLineData.Where(x => old_datatime.Contains(x.DataTime) && x.TimeType == (int)time && x.Coin.Equals(coin) && x.Platform == (int)Platform.Huobi && x.IsDelete == 0).ToList(); if (old_list.Count > 0) { db_context.CoinKLineData.RemoveRange(old_list); } new_list.AddRange(list); } db_context.CoinKLineData.AddRange(new_list); db_context.SaveChanges(); } catch (Exception ex) { _logger.LogError(new EventId(ex.HResult), ex, "---AddKLineData Exception---"); } }
/// <summary> /// 公告频道 /// </summary> public void GetNotice() { Console.WriteLine("---GetNotice---"); try { var web = new WebClient(); var json = web.DownloadString("https://api.jinse.com/v4/live/list?limit=50&reading=false&source=web&sort=5&flag=down&id=0"); JToken data = json.GetJsonValue("list"); var token = new List <JToken>(); // data.First.Last.AsJEnumerable().Values(); for (var i = 0; i < data.Count(); i++) { var temp = data[i].Last.AsJEnumerable().Values().ToList(); token.AddRange(temp); } using var scope = _scopeFactory.CreateScope(); using var db_context = scope.ServiceProvider.GetRequiredService <CoinDbContext>(); var source = db_context.CoinArticel.ToList(); var list = new List <Domain.Coin.CoinArticel.CoinArticel>(); Parallel.ForEach(token, item => { var title = GetTitleByContent(item["content"].ToString()); Console.WriteLine("---GetNotice---" + title); if (source.Any(x => x.Title.Equals(title))) { return; } if (list.Any(x => x.Title.Equals(title))) { return; } var model = new Domain.Coin.CoinArticel.CoinArticel { Id = Guid.NewGuid().ToString(), Content = GetDetail(item["content"].ToString()), CreateTime = DateTimeConvert.ConvertDateTime(item["created_at"].ToString()), Title = title, Category = 4, Logo = "" }; list.Add(model); }); if (!list.Any()) { return; } db_context.CoinArticel.AddRange(list); db_context.SaveChanges(); Console.WriteLine("---GetNotice SaveChanges---"); } catch (Exception ex) { _logger.LogError(new EventId(ex.HResult), ex, "---GetNotice Exception---"); } }