예제 #1
0
        /// <summary>
        /// gets a quandl data object directly from url
        /// </summary>
        /// <param name="qurl"></param>
        /// <param name="TryCache"></param>
        /// <param name="d"></param>
        /// <returns></returns>
        public static RootObject Get(string qurl, bool TryCache, DebugDelegate d)
        {
            if (_d == null)
            {
                _d = d;
            }
            var path = BarListImpl.GetDBLoc(PROGRAM, qurl.GetHashCode().ToString(), 7, DateTime.Now, getformatext(DefaultQuadlFormat));

            if (TryCache)
            {
                if (System.IO.File.Exists(path))
                {
                    try
                    {
                        var raw    = Util.getfile(path, d);
                        var dataok = !string.IsNullOrWhiteSpace(raw) && (raw.Length > 0);
                        if (dataok)
                        {
                            var data = isCacheCompressed ? GZip.Uncompress(raw) : raw;
                            return(json.Deserialize2Root(data, false, d));
                        }
                    }
                    catch (Exception ex)
                    {
                        debug("Will ignore cache and repull as error reading cache for: " + qurl + " , err: " + ex.Message + ex.StackTrace);
                    }
                }
            }
            // pull data
            var urldata     = Util.geturl(qurl, d);
            var isurldataok = !string.IsNullOrWhiteSpace(urldata) && (urldata.Length > 0);

            if (isurldataok)
            {
                var compdata = isCacheCompressed ? GZip.Compress(urldata) : urldata;
                if (Util.setfile(path, compdata))
                {
                    v("Cached " + urldata.Length.ToString("N0") + " bytes of qurl: " + qurl);
                }
                else
                {
                    v("Error caching: " + qurl + ", will repull next time.");
                }
            }

            return(json.Deserialize2Root(urldata, false, d));
        }
예제 #2
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);
            }
        }