Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using HtmlAgilityPack; /* http://htmlagilitypack.codeplex.com/ */
- using System.Net;
- using System.IO;
- using System.Globalization;
- /*
- FXstreet.com www.fxstreet.com
- Offers real-time exchange rates, forex news, currency converter, market forecasts and charts.
- */
- /* Author: sztuczne_dot_konto_at_gmail_dot_com */
- namespace Forex
- {
- public class FXStreetQuote
- {
- public string Name { get; set; }
- public decimal Last { get; set; }
- public decimal Open { get; set; }
- public decimal High { get; set; }
- public decimal Low { get; set; }
- public decimal NetChg { get; set; }
- public decimal PerChg { get; set; }
- public FXStreetQuote()
- {
- }
- public FXStreetQuote(string name, decimal last, decimal open, decimal high, decimal low, decimal netchg, decimal perchg)
- {
- this.Name = name;
- this.Last = last;
- this.Open = open;
- this.High = high;
- this.Low = low;
- this.NetChg = netchg;
- this.PerChg = perchg;
- }
- }
- public class FXStreet
- {
- public Uri url;
- protected string _glue = "%3b";
- protected string _url = @"http://www.fxstreet.com/rates-charts/currency-rates/?id=majors%3b";
- protected CultureInfo ci_provider = CultureInfo.InvariantCulture;
- public FXStreet(List<string> currencies)
- {
- if(currencies.Count < 1)
- {
- throw new Exception("error");
- }
- StringBuilder sb = new StringBuilder(_url);
- foreach(string item in currencies)
- {
- sb.Append(item.ToLower());
- sb.Append(_glue);
- }
- sb.Remove(sb.Length - 3, 3);
- url = new Uri(sb.ToString(), UriKind.Absolute);
- }
- public List<FXStreetQuote> Download()
- {
- List<FXStreetQuote> list = new List<FXStreetQuote>();
- WebClient wb = new WebClient();
- HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
- html.Load( new MemoryStream(wb.DownloadData(url)));
- HtmlNodeCollection currencies_nodes = html.DocumentNode.SelectNodes("//div[@class='table-ttpushrates table-rates it-TTPushRates']/table/tbody/tr");
- if (currencies_nodes != null)
- {
- foreach (HtmlNode tr_node in currencies_nodes)
- {
- HtmlNodeCollection tds = tr_node.SelectNodes("td");
- if (tds != null)
- {
- decimal last, open, high, low, change, perchg = 0;
- string currency = tds[0].InnerText;
- decimal.TryParse(tds[1].InnerText, NumberStyles.Any, ci_provider, out last);
- decimal.TryParse(tds[2].InnerText, NumberStyles.Any, ci_provider, out open);
- decimal.TryParse(tds[3].InnerText, NumberStyles.Any, ci_provider, out high);
- decimal.TryParse(tds[4].InnerText, NumberStyles.Any, ci_provider, out low);
- decimal.TryParse(tds[5].InnerText, NumberStyles.Any, ci_provider, out change);
- decimal.TryParse(tds[6].InnerText, NumberStyles.Any, ci_provider, out perchg);
- list.Add(new FXStreetQuote(currency, last, open, high, low, change, perchg));
- }
- }
- }
- return list;
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- List<string> list = new List<string>();
- list.Add("USDJPY");
- list.Add("GBPUSD");
- list.Add("USDCHF");
- list.Add("AUDUSD");
- list.Add("USDCAD");
- list.Add("NZDUSD");
- list.Add("USDCNY");
- list.Add("USDCNH");
- list.Add("USDHKD");
- list.Add("USDSGD");
- list.Add("USDNOK");
- list.Add("USDDKK");
- list.Add("USDSEK");
- list.Add("EURNOK");
- list.Add("EURDKK");
- list.Add("EURSEK");
- list.Add("EURCHF");
- list.Add("EURJPY");
- list.Add("EURGBP");
- list.Add("EURAUD");
- list.Add("GBPCHF");
- list.Add("GBPJPY");
- list.Add("GBPCAD");
- list.Add("GBPAUD");
- list.Add("AUDJPY");
- list.Add("CADJPY");
- list.Add("CHFJPY");
- list.Add("NZDJPY");
- list.Add("AUDCAD");
- list.Add("AUDCHF");
- list.Add("AUDSGD");
- FXStreet fx = new FXStreet(list);
- List<FXStreetQuote> result = new List<FXStreetQuote>();
- result = fx.Download();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement