Пример #1
0
        public static async Task <CompaniesListFMP> GetAllCompaniesAsync()
        {
            CompaniesListFMP companies = new CompaniesListFMP()
            {
                SymbolsToCompanies = new Dictionary <string, CompanyFMP>()
            };

            string nasdaqData = Companies.GetFromFtpUri(Companies.NasdaqSymbolsUri);

            string[] nasdaqDataLines = nasdaqData.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            for (int i = 1; i < nasdaqDataLines.Length - 1; i++) //trim first and last row
            {
                string   line = nasdaqDataLines[i];
                string[] data = line.Split('|');
                if (data.Count() > 3)
                {
                    string symbol = data[1];
                    if (!companies.SymbolsToCompanies.ContainsKey(symbol) && !String.IsNullOrEmpty(symbol))
                    {
                        bool isNasdaq = data[0] == "Y";
                        if (isNasdaq)
                        {
                            CompanyStatsFMP stats   = FMP.GetCompanyStatsAsync(symbol).Result;
                            CompanyFMP      company = new CompanyFMP
                            {
                                Symbol   = symbol,
                                Exchange = "NASDAQ",
                                Stats    = stats
                            };
                            companies.SymbolsToCompanies.Add(symbol, company);
                        }
                    }
                }
            }

            string otcData = Companies.GetFromFtpUri(Companies.OtcSymbolsUri);

            string[] otcDataLines = otcData.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            for (int j = 1; j < otcDataLines.Length - 1; j++) //trim first and last row
            {
                string   line = otcDataLines[j];
                string[] data = line.Split('|');
                if (data.Count() > 3)
                {
                    string symbol = data[0];
                    if (!companies.SymbolsToCompanies.ContainsKey(symbol) && !String.IsNullOrEmpty(symbol))
                    {
                        CompanyStatsFMP stats   = FMP.GetCompanyStatsAsync(symbol).Result;
                        CompanyFMP      company = new CompanyFMP
                        {
                            Symbol   = symbol,
                            Exchange = "OTC",
                            Stats    = stats
                        };
                        companies.SymbolsToCompanies.Add(symbol, company);
                    }
                }
            }

            string otcMarketsData = Companies.GetFromUri(Companies.OtcMarketsUri);

            string[] otcMarketsDataLines = otcMarketsData.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            for (int k = 1; k < otcMarketsDataLines.Length; k++) //trim first row
            {
                string   line = otcMarketsDataLines[k];
                string[] data = line.Split(',');
                if (data.Count() > 3)
                {
                    string symbol = data[0];
                    if (!companies.SymbolsToCompanies.ContainsKey(symbol) && !String.IsNullOrEmpty(symbol))
                    {
                        CompanyStatsFMP stats   = FMP.GetCompanyStatsAsync(symbol).Result;
                        CompanyFMP      company = new CompanyFMP
                        {
                            Symbol   = symbol,
                            Exchange = data[2],
                            Stats    = stats
                        };
                        companies.SymbolsToCompanies.Add(symbol, company);
                    }
                }
            }
            return(await Task.FromResult(companies));
        }
Пример #2
0
        /*public void ClearAll()
         * {
         *  this.CachedUpcs = new CustomDictionary<string, HashSet<string>>();
         *  this.CachedStyles = new CustomDictionary<string, string>();
         *
         *  _cbcorporateProductsCache = new MemoryCache(new MemoryCacheOptions());
         *  _psProductsCache = new MemoryCache(new MemoryCacheOptions());
         *  _psPricesCache = new MemoryCache(new MemoryCacheOptions());
         * }
         *
         * public void ClearCache(string cacheId)
         * {
         *  cacheId = cacheId.ToLower();
         *  switch (cacheId)
         *  {
         *      case "cbcorporate-products":
         *          _cbcorporateProductsCache = new MemoryCache(new MemoryCacheOptions());
         *          break;
         *      case "promostandards-products":
         *          _psProductsCache = new MemoryCache(new MemoryCacheOptions());
         *          break;
         *      case "promostandards-prices":
         *          _psPricesCache = new MemoryCache(new MemoryCacheOptions());
         *          break;
         *      default:
         *          throw new Exception("Cache ID " + cacheId + " is not supported by PimService Cache.");
         *  }
         *  foreach (string cacheKey in CachedStyles.Keys)
         *  {
         *      if (cacheKey.StartsWith(cacheId))
         *          lock (CachedStyles) { CachedStyles.Remove(cacheKey); }
         *  }
         *  foreach (string cacheKey in CachedUpcs.Keys)
         *  {
         *      if (cacheKey.StartsWith(cacheId))
         *          lock (CachedUpcs) { CachedUpcs.Remove(cacheKey); }
         *  }
         * }*/

        public async Task LoadCacheAsync(string cacheId)
        {
            int count = 0;
            await Task.Run(() =>
            {
                string nasdaqData        = Companies.GetFromFtpUri(Companies.NasdaqSymbolsUri);
                string[] nasdaqDataLines = nasdaqData.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                Random r1 = new Random();
                string[] randomizedNasdaqLines = nasdaqDataLines.OrderBy(x => r1.Next()).ToArray();
                for (int i = 1; i < randomizedNasdaqLines.Length - 1; i++) //trim first and last row
                {
                    string line   = randomizedNasdaqLines[i];
                    string[] data = line.Split('|');
                    if (data.Count() > 3)
                    {
                        string symbol = data[1];
                        if (!string.IsNullOrEmpty(symbol) && !CachedSymbols[cacheId].Contains(symbol))
                        {
                            bool isNasdaq = data[0] == "Y";
                            if (isNasdaq)
                            {
                                string cacheKey = string.Format("{0}-{1}", cacheId, symbol);
                                Get(cacheKey);
                                count++;
                            }
                        }
                    }
                }

                string otcMarketsData        = Companies.GetFromUri(Companies.OtcMarketsUri);
                string[] otcMarketsDataLines = otcMarketsData.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                Random r3 = new Random();
                string[] randomizedOtcMarketsLines = otcMarketsDataLines.OrderBy(x => r3.Next()).ToArray();
                for (int k = 1; k < randomizedOtcMarketsLines.Length; k++) //trim first row
                {
                    string line   = randomizedOtcMarketsLines[k];
                    string[] data = line.Split(',');
                    if (data.Length > 3)
                    {
                        string symbol = data[0];
                        if (!string.IsNullOrEmpty(symbol) && !CachedSymbols[cacheId].Contains(symbol))
                        {
                            string cacheKey = string.Format("{0}-{1}", cacheId, symbol);
                            Get(cacheKey);
                            count++;
                        }
                    }
                }

                /*Parallel Edition
                 * Parallel.ForEach(ids, Common.ParallelOptions, (entityId) =>
                 * {
                 *  string cacheKey = string.Format("{0}-{1}", cacheId, entityId);
                 *  try
                 *  {
                 *      if (!this.CachedStyles.ContainsKey(cacheKey))
                 *      {
                 *          this.Get(cacheKey);
                 *          Interlocked.Increment(ref count);
                 *      }
                 *  }
                 *  catch (Exception e)
                 *  {
                 *      lock (ExceptionReport) { ExceptionReport.Add(cacheKey, "Error: " + e.Message + ", StackTrace: " + e.StackTrace); }
                 *      return;
                 *  }
                 * });*/
            }).ConfigureAwait(false);
        }