Advertisement
m4ly

FXstreet.com currencies currency

Apr 8th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using HtmlAgilityPack;  /* http://htmlagilitypack.codeplex.com/  */
  5. using System.Net;
  6. using System.IO;
  7. using System.Globalization;
  8.  
  9. /*
  10. FXstreet.com www.fxstreet.com
  11. Offers real-time exchange rates, forex news, currency converter, market forecasts and charts.
  12. */
  13.  
  14. /* Author: sztuczne_dot_konto_at_gmail_dot_com */
  15. namespace Forex
  16. {
  17.   public class FXStreetQuote
  18.     {
  19.         public string Name { get; set; }
  20.         public decimal Last { get; set; }
  21.         public decimal Open { get; set; }
  22.         public decimal High { get; set; }
  23.         public decimal Low { get; set; }
  24.         public decimal NetChg { get; set; }
  25.         public decimal PerChg { get; set; }
  26.  
  27.        public FXStreetQuote()
  28.     {
  29.     }
  30.        public FXStreetQuote(string name, decimal last, decimal open, decimal high, decimal low, decimal netchg, decimal perchg)
  31.        {
  32.            this.Name = name;
  33.            this.Last = last;
  34.            this.Open = open;
  35.            this.High = high;
  36.            this.Low = low;
  37.            this.NetChg = netchg;
  38.            this.PerChg = perchg;
  39.  
  40.        }
  41.   }
  42.  
  43.  
  44.      public class FXStreet
  45.         {
  46.          public Uri url;
  47.          protected string _glue = "%3b";
  48.          protected string _url = @"http://www.fxstreet.com/rates-charts/currency-rates/?id=majors%3b";
  49.          protected CultureInfo ci_provider = CultureInfo.InvariantCulture;
  50.  
  51.          public FXStreet(List<string> currencies)
  52.          {
  53.              if(currencies.Count < 1)
  54.              {
  55.                  throw new Exception("error");
  56.  
  57.              }
  58.  
  59.              StringBuilder sb = new StringBuilder(_url);
  60.              foreach(string item in currencies)
  61.              {
  62.  
  63.                  sb.Append(item.ToLower());
  64.                  sb.Append(_glue);
  65.              }
  66.  
  67.            sb.Remove(sb.Length - 3, 3);
  68.            url = new Uri(sb.ToString(), UriKind.Absolute);
  69.  
  70.         }
  71.  
  72.          public List<FXStreetQuote> Download()
  73.          {
  74.  
  75.              List<FXStreetQuote> list = new List<FXStreetQuote>();
  76.  
  77.              WebClient wb = new WebClient();
  78.              HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
  79.  
  80.              html.Load( new MemoryStream(wb.DownloadData(url)));
  81.  
  82.              HtmlNodeCollection currencies_nodes = html.DocumentNode.SelectNodes("//div[@class='table-ttpushrates table-rates it-TTPushRates']/table/tbody/tr");
  83.  
  84.              if (currencies_nodes != null)
  85.              {
  86.                  foreach (HtmlNode tr_node in currencies_nodes)
  87.                  {
  88.                      HtmlNodeCollection tds =  tr_node.SelectNodes("td");
  89.                      if (tds != null)
  90.                      {
  91.                          decimal last, open, high, low, change, perchg = 0;
  92.  
  93.                          string currency = tds[0].InnerText;
  94.  
  95.                          decimal.TryParse(tds[1].InnerText, NumberStyles.Any, ci_provider, out last);
  96.                          decimal.TryParse(tds[2].InnerText, NumberStyles.Any, ci_provider, out open);
  97.                          decimal.TryParse(tds[3].InnerText, NumberStyles.Any, ci_provider, out high);
  98.                          decimal.TryParse(tds[4].InnerText, NumberStyles.Any, ci_provider, out low);
  99.                          decimal.TryParse(tds[5].InnerText, NumberStyles.Any, ci_provider, out change);
  100.                          decimal.TryParse(tds[6].InnerText, NumberStyles.Any, ci_provider, out perchg);
  101.  
  102.                          list.Add(new FXStreetQuote(currency, last, open, high, low, change, perchg));
  103.                      }
  104.  
  105.                  }
  106.              }
  107.              return list;
  108.          }
  109.  
  110.     }
  111. }
  112.  
  113.  
  114.  
  115.  
  116.    private void button1_Click(object sender, EventArgs e)
  117.         {
  118.             List<string> list = new List<string>();
  119.             list.Add("USDJPY");
  120.             list.Add("GBPUSD");
  121.             list.Add("USDCHF");
  122.             list.Add("AUDUSD");
  123.             list.Add("USDCAD");
  124.             list.Add("NZDUSD");
  125.             list.Add("USDCNY");
  126.             list.Add("USDCNH");
  127.             list.Add("USDHKD");
  128.             list.Add("USDSGD");
  129.             list.Add("USDNOK");
  130.             list.Add("USDDKK");
  131.             list.Add("USDSEK");
  132.             list.Add("EURNOK");
  133.             list.Add("EURDKK");
  134.             list.Add("EURSEK");
  135.             list.Add("EURCHF");
  136.             list.Add("EURJPY");
  137.             list.Add("EURGBP");
  138.             list.Add("EURAUD");
  139.             list.Add("GBPCHF");
  140.             list.Add("GBPJPY");
  141.             list.Add("GBPCAD");
  142.             list.Add("GBPAUD");
  143.             list.Add("AUDJPY");
  144.             list.Add("CADJPY");
  145.             list.Add("CHFJPY");
  146.             list.Add("NZDJPY");
  147.             list.Add("AUDCAD");
  148.             list.Add("AUDCHF");
  149.             list.Add("AUDSGD");
  150.  
  151.             FXStreet fx = new FXStreet(list);
  152.             List<FXStreetQuote> result = new List<FXStreetQuote>();
  153.  
  154.             result = fx.Download();
  155.  
  156.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement