public async Task <string> GetOptionLegalCsvByTaifexAsync(OptionLegalQuery optionLegalQuery)
        {
            using (var client = _clientFactory.CreateClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
                var response = await client.PostAsync(
                    "https://www.taifex.com.tw/cht/3/dlFutContractsDateDown",
                    new FormUrlEncodedContent(
                        new [] {
                    new KeyValuePair <string, string>("firstDate", DateTime.Now.AddYears(-3).ToString("yyyy/MM/dd HH:mm")),
                    new KeyValuePair <string, string>("lastDate", DateTime.Now.ToString("yyyy/MM/dd HH:mm")),
                    new KeyValuePair <string, string>("queryStartDate", optionLegalQuery.queryStartDate.ToString("yyyy/MM/dd")),
                    new KeyValuePair <string, string>("queryEndDate", optionLegalQuery.queryEndDate.ToString("yyyy/MM/dd")),
                    new KeyValuePair <string, string>("commodityId", optionLegalQuery.optionType.ToString()),
                }
                        )
                    );

                var bytes = await response.Content.ReadAsByteArrayAsync();

                var result = Encoding.GetEncoding(950).GetString(bytes);
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new PlatformNotSupportedException($"目前無法爬取期貨法人買賣超資料...,{response.StatusCode},{result}");
                }
                return(result);
            }
        }
Esempio n. 2
0
        public IActionResult RunOptionLegalClawer([FromBody] OptionLegalQuery optionLegalQuery)
        {
            Task clawerTask = _reportFcatory.GetOptionLegalClawer().ExecuteAsync(optionLegalQuery);

            // await _reportFcatory.GetOptionLegalClawer().ExecuteAsync(optionLegalQuery);
            return(new JsonResult(new { Success = true }));
        }
        public async Task ExecuteAsync(OptionLegalQuery optionLegalQuery)
        {
            var csvString = await GetOptionLegalCsvByTaifexAsync(optionLegalQuery);

            var csvList = ReadCsv(csvString).Where(csv => !_optionLegalRepository.IsExist(csv));

            _optionLegalRepository.Insert(csvList);
        }