static void add(DateTime date, IEnumerable <SerializablePrices> table, KdbData type) { if (!table.Any()) { return; } createDirectoryIfNotFound(date, type); string targetPath = createFileName(date, type); try { var seri = new XmlSerializer(typeof(SerializablePrices[])); using (FileStream fs = new FileStream(targetPath, FileMode.Create)) { seri.Serialize(fs, table.ToArray()); } StockPriceManager.SetMessage( DataSource.Kdb, type.ToString() + ", " + date.ToString("yyyy-MM-dd") + ", 書き込み完了" ); } catch (Exception e) { StockPriceManager.SetMessage(new ManagementMessage() { Sender = DataSource.Kdb, Signal = MessageSignal.Error, Message = type.ToString() + " " + date.ToString("yyyy-MM-dd") + " のデータの書き込みに失敗しました。", Detail = e.Message, }); } }
static void createDirectoryIfNotFound(DateTime date, KdbData type) { string s = localPath + type.ToString() + Path.DirectorySeparatorChar + date.Year.ToString(); if (!Directory.Exists(s)) { Directory.CreateDirectory(s); } }
static IEnumerable <DateTime> getExistingDataDate(KdbData type) { try { return(Directory.GetFiles(localPath + type.ToString() + Path.DirectorySeparatorChar, "*", SearchOption.AllDirectories) .Select(a => Path.GetFileNameWithoutExtension(a)) .Select(a => ResultWithValue.Of <string, DateTime>(DateTime.TryParse, a)) .Where(a => a.Result) .Select(a => a.Value) .OrderBy(a => a)); } catch (DirectoryNotFoundException) { return(new DateTime[0]); } }
static string _download(DateTime dt, KdbData type) { //try { //ダウンロード元のURL //string url = "http://k-db.com/stocks/" + dt.ToString("yyyy-MM-dd") + "?download=csv"; string url = "http://k-db.com/" + type.ToString() + "/" + dt.ToString("yyyy-MM-dd") + "?download=csv"; //データを文字列としてダウンロードする return(wc.DownloadString(url)); //} catch { // return ""; //} }
internal static IEnumerable <SerializablePrices> Acquire(KdbData kdb, DateTime since, DateTime until, Func <SerializablePrices, bool> pred) { var span = getExistingDataDate(kdb).SkipWhile(a => a < since).TakeWhile(a => a <= until); foreach (var dt in span) { var d = tryGet(dt, kdb).OfType <SerializablePrices>() .Where(a => pred(a)); foreach (var c in d) { yield return(c); } } }
static SerializablePrices[] tryGet(DateTime date, KdbData type) { if (!getExistingDataDate(type).Any(a => a == date)) { return(new SerializablePrices[0]); } try { var deseri = new XmlSerializer(typeof(SerializablePrices[])); using (FileStream fs = new FileStream(createFileName(date, type), FileMode.Open)) { return(deseri.Deserialize(fs) as SerializablePrices[]); } }catch (Exception e) { StockPriceManager.SetMessage(new ManagementMessage() { Sender = DataSource.Kdb, Signal = MessageSignal.Error, Message = type.ToString() + " " + date.ToString("yyyy-MM-dd") + " のデータの読み込みに失敗しました。", Detail = e.Message }); return(new SerializablePrices[0]); } }
static string createFileName(DateTime date, KdbData type) { return(localPath + type.ToString() + Path.DirectorySeparatorChar + date.Year.ToString() + Path.DirectorySeparatorChar + date.ToString("yyyy-MM-dd") + ".xml"); }
static IEnumerable <Tuple <DateTime, IEnumerable <SerializablePrices> > > download(DateTime start, DateTime end, KdbData type) { Func <DateTime, DateTime, DateTime> nxt = (c, e) => { return((c < e) ? c.AddDays(1) : (c > e) ? c.AddDays(-1) : c); }; DateTime current = start; do { if (current.DayOfWeek != DayOfWeek.Saturday && current.DayOfWeek != DayOfWeek.Sunday) { Thread.Sleep(2000); var s = _download(current, type); StockPriceManager.SetMessage( DataSource.Kdb, type.ToString() + ", " + current.ToString("yyyy-MM-dd") + ", ダウンロード完了" ); if (!string.IsNullOrEmpty(s)) { using (var str = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(s)))) using (var csv = new CsvReader(str)) { switch (type) { case KdbData.stocks: csv.Configuration.RegisterClassMap <StockCsvMap>(); break; case KdbData.indices: csv.Configuration.RegisterClassMap <IndexCsvMap>(); break; } var rec = csv.GetRecords <SerializablePrices>().ToArray(); foreach (var r in rec) { r.Date = current; } yield return(new Tuple <DateTime, IEnumerable <SerializablePrices> >(current, rec)); } } Thread.Sleep(3000); } current = nxt(current, end); } while (current != end); }