示例#1
0
        /// <summary>
        /// 確認需求範圍是否有未取得的資料
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        public void CreateStockInfoData(DateTime startDate, DateTime endDate)
        {
            DateTime now = DateTime.UtcNow.AddHours(8);
            string   url = "https://www.twse.com.tw/exchangeReport/BWIBBU_d?response=json&selectType=ALL";

            int insertDateLogsType = (int)InsertDateLogsTypeEnum.StockInfo;
            var insertDateLogDic   =
                _InsertDateLogRepository
                .GetListBy(e => e.Date >= startDate && e.Date <= endDate && e.Type == insertDateLogsType)
                .ToDictionary(e => e.Date, e => e);

            for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
            {
                List <StockInfo>     stockInfos     = new List <StockInfo>();
                List <InsertDateLog> insertDateLogs = new List <InsertDateLog>();
                if (insertDateLogDic.ContainsKey(date))
                {
                    continue;
                }
                //一個日期呼叫一次API
                StockInfoJsonModel result = null;

                try
                {
                    result = RestRequestHelper.Request(url)
                             .Get(e => e
                                  .AddParameter("date", date.ToString("yyyyMMdd"))
                                  )
                             .Response <StockInfoJsonModel>();

                    //API 一直打會被鎖,故隨機延遲500~700毫秒
                    Task.Delay(GetRandom(500, 700)).Wait();
                }
                catch (Exception ex)
                {
                    //錯誤就跳出迴圈
                    break;
                }
                if (result == null)
                {
                    //不紀錄該日期已經取得資料
                    continue;
                }
                //紀錄 API日期
                insertDateLogs.Add(new InsertDateLog
                {
                    Type       = insertDateLogsType,
                    Date       = date,
                    CreateTime = now
                });
                //為 null 可能是假日或沒開市所以查無資料
                if (result.Data != null)
                {
                    foreach (var item in result.Data)
                    {
                        /*["證券代號","證券名稱","殖利率(%)","股利年度","本益比","股價淨值比","財報年/季"]*/
                        stockInfos.Add(new StockInfo
                        {
                            Code            = item[0]?.ToString(),
                            Name            = item[1]?.ToString(),
                            YieldRate       = item[2]?.ToString().TryToDecimal(),
                            DividendYear    = (item[3]?.ToString()).TryToInt().GetValueOrDefault(),
                            PE              = item[4]?.ToString().TryToDecimal(),
                            PB              = item[5]?.ToString().TryToDecimal(),
                            FinancialReport = item[6]?.ToString(),
                            Date            = date
                        });
                    }
                }

                _StockInfoRepository.Create(stockInfos, insertDateLogs);
            }
        }