Beispiel #1
0
        static string  GetData(IQuandlRequest req, string sym, string dataset, FileFormats form, bool checkcache, bool compressedcache, DebugDelegate d)
        {
            if (_d == null)
            {
                _d = d;
            }
            try
            {
                var path = BarListImpl.GetDBLoc(PROGRAM, dataset + "_" + sym, 30, DateTime.Now, getformatext(form));
                if (checkcache)
                {
                    if (System.IO.File.Exists(path))
                    {
                        try
                        {
                            var cached = Util.getfile(path, null);
                            if (compressedcache)
                            {
                                var cacheddata = GZip.Uncompress(cached);
                                v(sym + " " + dataset + " found " + cacheddata.Length.ToString("N0") + " bytes of cached data.");
                                return(cacheddata);
                            }
                        }
                        catch (Exception ex)
                        {
                            debug("Ignoring cache (will pull directly) after cache error for: " + dataset + " on " + sym + " err: " + ex.Message + ex.StackTrace);
                        }
                    }
                }
                var con      = new QuandlConnection();
                var data     = con.Request(req);
                var isdataok = !qh.isQdlUnderMaintence(data);
                if (isdataok)
                {
                    v(sym + " " + dataset + " retrieved " + data.Length.ToString("N0") + " bytes of " + dataset + " data.");
                    // save it for later caching
                    if (Util.setfile(path, compressedcache ? GZip.Compress(data) : data))
                    {
                        v(sym + " " + dataset + " cached " + data.Length.ToString("N0") + " bytes of " + dataset + " data.");
                    }
                    else
                    {
                        v(sym + " " + dataset + " error caching " + data.Length.ToString("N0") + " bytes of " + dataset + " data.  Will be re-pulled on next attempt.");
                    }
                }
                else
                {
                    debug(sym + " " + dataset + " can't be retrieved because quandl is down.");
                    return(string.Empty);
                }

                return(data);
            }
            catch (Exception ex)
            {
                if (isVerboseDebugging)
                {
                    debug("An error occurred getting data for: " + dataset + " on symbol: " + sym + " using url: " + req.ToRequestString());
                    debug("Error for " + dataset + " on symbol: " + sym + ": " + ex.Message + ex.StackTrace);
                }
                else
                {
                    debug("An error occurred getting data for: " + dataset + " on symbol: " + sym + ", err: " + ex.Message + ex.StackTrace);
                }
                return(string.Empty);
            }
        }