public static List<Emitent> EmitentList = new List<Emitent>(); // Список всех эмитентов #endregion Fields #region Methods // Функция по затягиванию информации об эмитентах public static bool UpdateEmitents() { bool result = false; EmitentList.Clear(); try { WebDownload downloadClient = new WebDownload(5 * 60 * 1000); string responseBody = downloadClient.DownloadString("http://www.finam.ru/cache/icharts/icharts.js"); string[] responseVars = responseBody.Split('\n'); string[] emitentIDs = { }; string[] emitentNames = { }; string[] emitentCodes = { }; string[] emitentMarkets = { }; for (int i = 0; i < responseVars.Count(); i++) { string[] str = responseVars[i].Split('='); switch (i) { case 0: emitentIDs = responseVars[i].Split(','); emitentIDs = clearBrakes(emitentIDs); break; case 1: emitentNames = responseVars[i].Split(','); emitentNames = clearBrakes(emitentNames); break; case 2: emitentCodes = responseVars[i].Split(','); emitentCodes = clearBrakes(emitentCodes); break; case 3: emitentMarkets = responseVars[i].Split(','); emitentMarkets = clearBrakes(emitentMarkets); break; default: break; } } for (int i = 0; i < emitentIDs.Count(); i++) { Emitent emitent = new Emitent() { ID = int.Parse(emitentIDs[i]), Name = emitentNames[i].Trim(new char[] {'\''}), Code = emitentCodes[i].Trim(new char[] { '\'' }), Market = int.Parse(emitentMarkets[i]) }; EmitentList.Add(emitent); } result = true; } catch (Exception ex) { Console.WriteLine(ex.Message); } return result; }
static void Main(string[] args) { var periods = GetPeriods(); Period period = null; if(args.Length != 4) { Console.WriteLine("Usage: FinamTicksDownloader.exe ticker period start_date end_date"); Console.WriteLine("Example 1:"); Console.WriteLine("\tFinamTicksDownloader.exe RTS ticks 2013.03.12 2013.05.13"); Console.WriteLine("\twill download RTS tick data since 12 March 2013 till 13 May 2013"); Console.WriteLine("Example 2:"); Console.WriteLine("\tFinamTicksDownloader.exe Si M5 2013.03.12 2013.05.13"); Console.WriteLine("\twill download Si 5 minute tick data since 12 March 2013 till 13 May 2013"); Console.WriteLine("Available periods:"); foreach(var p in periods) { Console.WriteLine("\t{0}\t-\t{1}", p.Name, p.Description); } return; } period = periods.Where(p => p.Name.ToLower() == args[1].ToLower()).FirstOrDefault(); if(period == null) { Console.WriteLine("Period with name " + args[1] + " not found"); return; } period.HowMuchDaysToDownloadAtTime = 10; DateTime startDate = DateTime.Parse(args[2]); DateTime endDate = DateTime.Parse(args[3]); Console.WriteLine("Downloading since " + startDate.ToDayString() + " till " + endDate.ToDayString()); Console.Write("Loading tickers list..."); EmitentHelper.UpdateEmitents(); Console.WriteLine("OK"); var ticker = EmitentHelper.EmitentList.Where(x => x.Name == args[0] && x.Market == 14).FirstOrDefault(); if(ticker == null) { Console.WriteLine("Ticker with name " + args[0] + " not found"); return; } Console.WriteLine("Using ticker " + ticker); string fileName = String.Format("SPFB.{0}-{1}_{2:D4}{3:D2}{4:D2}_{5:D4}{6:D2}{7:D2}.csv", ticker.Name, period.Name, startDate.Year, startDate.Month, startDate.Day, endDate.Year, endDate.Month, endDate.Day ); DateTime currentDateFrom = startDate; while (currentDateFrom <= endDate) { DateTime currentDateTo = getCurrentDateTo(currentDateFrom, period, endDate); WebDownload webClient = new WebDownload(5 * 60 * 1000); webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31"); webClient.Headers.Add("Referer", "http://www.finam.ru/analysis/profile041CA00007/default.asp"); webClient.Headers.Add("Accept", "text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1"); webClient.Headers.Add("Accept-Language", "en-US,en;q=0.9"); webClient.Headers.Add("Accept-Encoding", "gzip, deflate"); string tempFileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".txt"; string chunkFrom = String.Format("{0:D2}{1:D2}{2:D2}", currentDateFrom.Year % 100, currentDateFrom.Month, currentDateFrom.Day); string chunkTo = String.Format("{0:D2}{1:D2}{2:D2}", currentDateTo.Year % 100, currentDateTo.Month, currentDateTo.Day); string url = String.Format( "http://195.128.78.52/{6}_{0}_{0}.txt?" + "market={8}&em={7}&code={6}&df={1}&mf={2}&yf={3}&dt={10}&mt={11}&yt={12}&p={4}&" + "f={6}_{0}_{9}&e=.txt&cn={6}&dtf=1&tmf=1&MSOR=0&mstime=on&" + "mstimever=1&sep=1&sep2=1&datf={5}", chunkFrom, currentDateFrom.Day, currentDateFrom.Month - 1, currentDateFrom.Year, period.ParameterId, period.DataFormat, ticker.Code, ticker.ID, ticker.Market, chunkTo, currentDateTo.Day, currentDateTo.Month - 1, currentDateTo.Year ); if (File.Exists(tempFileName)) File.Delete(tempFileName); Console.WriteLine("Downloading from " + currentDateFrom.ToDayString() + " to " + currentDateTo.ToDayString()); try { webClient.DownloadFile(url, tempFileName); long size = new FileInfo(tempFileName).Length; long mbSize = (size >> 20); Console.WriteLine("Downloaded " + mbSize + " Mbytes"); if ((mbSize > 27) && (period.HowMuchDaysToDownloadAtTime > 0)) { Console.WriteLine("Possibility of incomplete data. Will change to 1 day increment and retry."); period.HowMuchDaysToDownloadAtTime = 0; Thread.Sleep(5 * 1000); continue; } if (size == 0) { Console.WriteLine("Skipping " + currentDateFrom.ToDayString()); currentDateFrom = currentDateFrom.AddDays(period.HowMuchDaysToDownloadAtTime + 1); Thread.Sleep(5000); continue; } if (!fileContainsStockData(tempFileName)) { Console.WriteLine("File doesn't contain stock data, trying again (file size: " + size + " bytes)"); if (size > 0 && size < 300) { Console.WriteLine("Message: "); Console.WriteLine(File.ReadAllText(tempFileName, Encoding.GetEncoding(1251))); } Thread.Sleep(5 * 1000); continue; } Thread.Sleep(5 * 1000); } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); Console.WriteLine("Trying again"); Thread.Sleep(5 * 1000); continue; } appendAndDelete(tempFileName, fileName); currentDateFrom = currentDateFrom.AddDays(period.HowMuchDaysToDownloadAtTime + 1); } }