internal OrderbookResponse GetOrderBook(int limit=10) { string url = String.Format("{0}/orderbook/{1}{2}?limit={3}", _dataBaseUrl, _baseAsset, _counterAsset, limit); var webClient = new WebClient2(_config, _logger, DATA_TIMEOUT); OrderbookResponse orderBook = webClient.DownloadObject<OrderbookResponse>(url); if (null == orderBook || null == orderBook.asks || null == orderBook.bids) { return null; } return orderBook; }
internal OrderBookResponse GetMarketDepth(string baseAsset, string counterAsset) { string url = DATA_BASE_URL + "/depth/" + baseAsset.ToUpper() + "/" + counterAsset + "/15"; //e.g. /depth/BTC/USD/15 var webClient = new WebClient2(_config, _logger, 30000/*30sec web request timeout*/); OrderBookResponse orderBook = webClient.DownloadObject<OrderBookResponse>(url); if (null == orderBook || null == orderBook.asks || null == orderBook.bids) { return null; } return orderBook; }
/// <summary> /// Translates a string into another language using Google's translate API JSON calls. /// <seealso>Class TranslationServices</seealso> /// </summary> /// <param name="Text">Text to translate. Should be a single word or sentence.</param> /// <param name="FromCulture"> /// Two letter culture (en of en-us, fr of fr-ca, de of de-ch) /// </param> /// <param name="ToCulture"> /// Two letter culture (as for FromCulture) /// </param> public string TranslateGoogle(string text, string fromCulture, string toCulture) { fromCulture = fromCulture.ToLower(); toCulture = toCulture.ToLower(); // normalize the culture in case something like en-us was passed // retrieve only en since Google doesn't support sub-locales string[] tokens = fromCulture.Split('-'); if (tokens.Length > 1) { fromCulture = tokens[0]; } // normalize ToCulture tokens = toCulture.Split('-'); if (tokens.Length > 1) { toCulture = tokens[0]; } string url = string.Format(@"http://translate.google.com/translate_a/t?client=j&text={0}&hl=en&sl={1}&tl={2}", System.Web.HttpUtility.UrlEncode(text), fromCulture, toCulture); // Retrieve Translation with HTTP GET call string html = null; try { WebClient2 web = new WebClient2(); // MUST add a known browser user agent or else response encoding doen't return UTF-8 (WTF Google?) web.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0"); web.Headers.Add(HttpRequestHeader.AcceptCharset, "UTF-8"); // Make sure we have response encoding to UTF-8 web.Encoding = Encoding.UTF8; html = web.DownloadString(url); } catch { return(null); } // Extract out trans":"...[Extracted]...","from the JSON string return(Regex.Match(html, "trans\":(\".*?\"),\"", RegexOptions.IgnoreCase).Groups[1].Value.Trim(new char[] { '"' })); }
private static bool ResourceExists(WebClient2 client, Uri uri) { try { client.Method = "HEAD"; // HEAD is special, it's not supported by standard WebClient because it must have a null body but the Upload method don't accept it client.DownloadString(uri); return(true); } catch (Exception e) { if (!IsNotFound(e)) { throw; } return(false); } }
private string sendGetRequest(string url) { WebException exc = null; var delay = RETRY_DELAY; for (int i = 1; i <= RETRY_COUNT; i++) { var client = new WebClient2(_config, _logger, 20000); try { var text = client.DownloadString(url); _logger.LastResponse = text; return text; } catch (WebException we) { var text = String.Format("(ATTEMPT {0}/{1}) Web request failed with exception={2}; status={3}. Retry in {4} ms", i, RETRY_COUNT, we.Message, we.Status, delay); _logger.Warning(text); exc = we; Thread.Sleep(delay); delay *= 2; } } throw new Exception(String.Format("Web request failed {0} times in a row with error '{1}'. Giving up.", RETRY_COUNT, exc.Message)); }
internal List<Candle> GetCandles() { var client = new WebClient2(_config, _logger, DATA_TIMEOUT); var data = client.DownloadStringSafe("http://market.huobi.com/staticmarket/btc_kline_001_json.js"); if (null == data) return null; //NOTE: add auxiliary root variable to avoid tricky JSON parsing data = "{\"candleData\":" + data + "}"; var candles = Helpers.DeserializeJSON<CandlesResponse>(data); return candles.MergeAsCandles(5); }
internal TradeStatisticsResponse GetTradeStatistics() { var client = new WebClient2(_config, _logger, DATA_TIMEOUT); var trades = client.DownloadObject<TradeStatisticsResponse>(TRADE_STATS_URL); return trades; }
internal DateTime GetServerTime() { var client = new WebClient2(_config, _logger, DATA_TIMEOUT); //TODO: why do I create new client in every other method? Should reuse one instance. var ticker = client.DownloadObject<TickerResponse>(TICKER_URL); return null==ticker ? DateTime.MinValue : ticker.ServerTime; }
/// <summary>Get market depth for given fiat currency and BTC</summary> /// <param name="currencyCode">One of "USD", "CNY"</param> internal MarketDepthResponse GetMarketDepth(string currencyCode) { currencyCode = currencyCode.ToLower(); string marketUrl; if ("usd" == currencyCode) marketUrl = "http://api.huobi.com/usdmarket/depth_btc_30.js"; else if ("cny" == currencyCode) marketUrl = "http://api.huobi.com/staticmarket/depth_btc_30.js"; else { throw new ArgumentException("Invalid currency code. Expected one of [USD, CNY]", "currencyCode"); } var client = new WebClient2(_config, _logger, DATA_TIMEOUT); var depth = client.DownloadObject<MarketDepthResponse>(marketUrl); return depth; }
internal MarketDepthResponse GetMarketDepth() { var client = new WebClient2(_config, _logger, DATA_TIMEOUT); var depth = client.DownloadObject<MarketDepthResponse>(MARKET_URL); return depth; }
public static void Run(PeriodicalList periodicalList) { var project = periodicalList.Project; var journalUrl = @"http://ftp.ncbi.nih.gov/pubmed/J_Entrez.txt"; // URL for journal list text file var journalCollection = new List <Periodical>(); string completeList; try { Cursor.Current = Cursors.WaitCursor; using (var webClient = new WebClient2() { Timeout = 60000 }) { using (var stream = webClient.OpenRead(journalUrl)) { using (var streamReader = new StreamReader(stream)) { completeList = streamReader.ReadToEnd(); } } } } catch (Exception e) { Cursor.Current = Cursors.Default; MessageBox.Show(periodicalList, ImportJournalsResources.PubMedMacroReadErrorMessage.FormatString(journalUrl, e.Message), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { Cursor.Current = Cursors.WaitCursor; var entrySplitters = new string[] { @"--------------------------------------------------------" }; var individualJournalEntries = completeList.Split(entrySplitters, StringSplitOptions.RemoveEmptyEntries).ToList(); var counter = 0; var splitEntry = new Regex(@"^(?:JrId: )(?<JournalId>\d+?)(?:\nJournalTitle: )(?<JournalTitle>.*?)(?:\nMedAbbr: )(?<Abbreviation2>.*?)(?:\nISSN \(Print\): )(?<IssnPrint>.*?)(?:\nISSN \(Online\): )(?<IssnOnline>.*?)(?:\nIsoAbbr: )(?<Abbreviation1>.*?)(?:\nNlmId: )(?<NlmId>.*?)$", RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.Multiline); foreach (string journalEntry in individualJournalEntries) { counter++; string journalTitle; string abbreviation1; string abbreviation2; string abbreviation3; string issnPrint; string issnOnline; string nlmId; var match = splitEntry.Match(journalEntry); journalTitle = match.Groups["JournalTitle"].Value; abbreviation1 = match.Groups["Abbreviation1"].Value; abbreviation2 = match.Groups["Abbreviation2"].Value; issnPrint = match.Groups["IssnPrint"].Value; issnOnline = match.Groups["IssnOnline"].Value; nlmId = match.Groups["NlmId"].Value; if (string.IsNullOrEmpty(abbreviation1)) { abbreviation1 = abbreviation2; } if (!abbreviation1.Contains(".") && !String.IsNullOrEmpty(abbreviation1)) { var journalTitleWords = journalTitle.ToLowerInvariant().Split(new char[] { ' ', '.', ';', ',', ':', '&', '-' }, StringSplitOptions.RemoveEmptyEntries); var abbreviation1Words = abbreviation1.Split(' '); var abbreviation1WithFullStops = new List <string>(); foreach (var word in abbreviation1Words) { if (word.StartsWith("(") || word.EndsWith(")")) { abbreviation1WithFullStops.Add(word); } else if (!Array.Exists(journalTitleWords, x => x == word.ToLowerInvariant())) { abbreviation1WithFullStops.Add(word + "."); } else { abbreviation1WithFullStops.Add(word); } } abbreviation1 = string.Join(" ", abbreviation1WithFullStops); } abbreviation3 = Regex.Match(journalTitle, @"(?:: )[A-Z]{2,6}$").ToString(); var journal = new Periodical(project, journalTitle); if (!string.IsNullOrEmpty(abbreviation1)) { journal.StandardAbbreviation = abbreviation1; } if (!string.IsNullOrEmpty(abbreviation2)) { journal.UserAbbreviation1 = abbreviation2; } if (!string.IsNullOrEmpty(abbreviation3)) { journal.UserAbbreviation2 = abbreviation3; } if (!string.IsNullOrEmpty(issnPrint) && IssnValidator.IsValid(issnPrint)) { journal.Issn = issnPrint; } else if (!string.IsNullOrEmpty(issnOnline) && IssnValidator.IsValid(issnOnline)) { journal.Issn = issnOnline; } if (!string.IsNullOrEmpty(issnPrint) && IssnValidator.IsValid(issnPrint) && !string.IsNullOrEmpty(issnOnline) && IssnValidator.IsValid(issnOnline)) { journal.Notes = "ISSN (Online): " + issnOnline; } if (!string.IsNullOrEmpty(nlmId)) { journal.Notes = journal.Notes + "\nNlmID: " + nlmId; } journalCollection.Add(journal); } project.Periodicals.AddRange(journalCollection); } catch (Exception exception) { Cursor.Current = Cursors.Default; journalCollection = null; MessageBox.Show(periodicalList, ImportJournalsResources.MacroImportingErrorMessage.FormatString(exception.Message), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { Cursor.Current = Cursors.Default; if (journalCollection != null) { MessageBox.Show(periodicalList, ImportJournalsResources.PubMedMacroResultMessage.FormatString(journalCollection.Count.ToString()), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); journalCollection = null; } } }
//TODO: this method is repeated so many times that it's ridiculous. REFACTOR! private string sendGetRequest(string url) { WebException exc = null; var delay = RETRY_DELAY; for (int i = 1; i <= RETRY_COUNT; i++) { var client = new WebClient2(_config, _logger, 20000); if (null != _webProxy) { client.Proxy = _webProxy; } try { string text = client.DownloadString(url); _logger.LastResponse = text; return text; } catch (WebException we) { var text = $"(ATTEMPT {i}/{RETRY_COUNT}) Web request failed with exception={we.Message}; status={we.Status}. Retry in {delay} ms"; _logger.Warning(text); exc = we; Thread.Sleep(delay); delay *= 2; } } throw new Exception($"Web request failed {RETRY_COUNT} times in a row with error '{exc.Message}'. Giving up."); }
//TODO: we'll probably need to drop this one because data API seems to give old data (probably last closed ledger) private AccountOrdersResponse getActiveOrders2() { var webClient = new WebClient2(_config, _logger, DATA_TIMEOUT); var url = String.Format("{0}/accounts/{1}/orders", _dataApiUrl, _walletAddress); AccountOrdersResponse data = webClient.DownloadObject<AccountOrdersResponse>(url); if (null == data || null == data.orders) //TODO: better info when server returns error? { return null; } return data; }
/// <summary>Get recent trades of asset pair</summary> internal ExchangeHistoryResponse GetTradeStatistics(string baseAssetCode, string gaseAssetGateway, string counterAsset, string counterAssetGateway) { var webClient = new WebClient2(_config, _logger, DATA_TIMEOUT); string assetDef1 = baseAssetCode; if (!String.IsNullOrEmpty(gaseAssetGateway)) { assetDef1 += "+" + gaseAssetGateway; } string assetDef2 = counterAsset; if (!String.IsNullOrEmpty(counterAssetGateway)) { assetDef2 += "+" + counterAssetGateway; } const int limit = 10; var url = String.Format("{0}/exchanges/{1}/{2}?descending=true&limit={3}&result=tesSUCCESS&type=OfferCreate", _dataApiUrl, assetDef1, assetDef2, limit); var data = webClient.DownloadObject<ExchangeHistoryResponse>(url); return data; }
/// <summary>Get underlying account's balances</summary> /// <remarks> /// Uses data API (REST-like). Sometimes seems not synchronised with web-socket output of a validator. /// </remarks> internal AccountBalances GetBalances() { var url = String.Format("{0}/accounts/{1}/balances", _dataApiUrl, _walletAddress); var webClient = new WebClient2(_config, _logger, DATA_TIMEOUT); AccountBalances balanceData = webClient.DownloadObject<AccountBalances>(url); if (null == balanceData || balanceData.IsError || null == balanceData.balances) { return null; } return balanceData; }
async private Task <string> UploadData(Stream imageStream, string fileName, Uri uploadUri, Dictionary <string, string> parameters, IProgress <UploadProgressChangedEventArgs> progress) { //string boundary = "FLICKR_MIME_" + DateTime.Now.ToString("yyyyMMddhhmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo); //string authHeader = FlickrResponder.OAuthCalculateAuthHeader(parameters); //byte[] dataBuffer = CreateUploadData(imageStream, fileName, parameters, boundary); //HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uploadUri); //req.Method = "POST"; //req.Timeout = HttpTimeout; //req.ContentType = "multipart/form-data; boundary=" + boundary; ////req.Expect = String.Empty; //if (!String.IsNullOrEmpty(authHeader)) //{ // req.Headers["Authorization"] = authHeader; //} //req.ContentLength = dataBuffer.Length; //using (Stream reqStream = req.GetRequestStream()) //{ // int bufferSize = 32 * 1024; // if (dataBuffer.Length / 100 > bufferSize) bufferSize = bufferSize * 2; // int uploadedSoFar = 0; // while (uploadedSoFar < dataBuffer.Length) // { // reqStream.Write(dataBuffer, uploadedSoFar, Math.Min(bufferSize, dataBuffer.Length - uploadedSoFar)); // uploadedSoFar += bufferSize; // if (OnUploadProgress != null) // { // UploadProgressEventArgs args = new UploadProgressEventArgs(uploadedSoFar, dataBuffer.Length); // OnUploadProgress(this, args); // } // } // reqStream.Close(); //} //HttpWebResponse res = (HttpWebResponse)req.GetResponse(); //StreamReader sr = new StreamReader(res.GetResponseStream()); //string s = sr.ReadToEnd(); //sr.Close(); //return s; string boundary = "FLICKR_MIME_" + DateTime.Now.ToString("yyyyMMddhhmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo); string authHeader = FlickrResponder.OAuthCalculateAuthHeader(parameters); byte[] dataBuffer = CreateUploadData(imageStream, fileName, parameters, boundary); WebClient2 webClient = new WebClient2(); //webClient.Timeout = (int)TimeSpan.FromMinutes(10).TotalMilliseconds; webClient.UploadProgressChanged += ((a, b) => { if (progress != null) { progress.Report(b); } if (FlickrLogic.CancellationToken.IsCancellationRequested) { ((WebClient2)a).CancelAsync(); } }); webClient.ContentType = "multipart/form-data; boundary=" + boundary; if (!String.IsNullOrEmpty(authHeader)) { webClient.Headers["Authorization"] = authHeader; } webClient.ContentLength = dataBuffer.Length; FlickrLogic.UploadEventList.Add(new Notice() { Type = NoticeType.Upload, FullPath = fileName, Note = "Ready to upload.", }); byte[] responseArray = null; try { responseArray = await webClient.UploadDataTaskAsync(uploadUri, dataBuffer); } catch (Exception ex) { responseArray = null; } string s = System.Text.Encoding.UTF8.GetString(responseArray); return(s); }