Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Net
  2. Imports HtmlAgilityPack
  3. Imports System.Text
  4. Imports System.Globalization
  5.  
  6. 'http://www.codeproject.com/Tips/804660/How-to-Parse-HTML-using-Csharp
  7.  
  8. Public Class MFXHTMLFetcher
  9.     Public FxEvents As List(Of FxEvent)
  10.     Public Sub New()
  11.         FxEvents = New List(Of FxEvent)
  12.     End Sub
  13.     Public Function countryToCurrency(country As String) As String
  14.         Dim countriesMap As New Dictionary(Of String, String)
  15.         countriesMap.Add("JP", "JPY")
  16.         countriesMap.Add("CN", "CNY")
  17.         countriesMap.Add("DE", "EUR")
  18.         countriesMap.Add("FR", "EUR")
  19.         countriesMap.Add("BE", "EUR")
  20.         countriesMap.Add("ES", "EUR")
  21.         countriesMap.Add("PT", "EUR")
  22.         countriesMap.Add("IT", "EUR")
  23.         countriesMap.Add("GR", "EUR")
  24.         countriesMap.Add("NL", "EUR")
  25.         countriesMap.Add("FI", "EUR")
  26.         countriesMap.Add("AT", "EUR")
  27.         countriesMap.Add("IE", "EUR")
  28.         countriesMap.Add("SK", "EUR")
  29.         countriesMap.Add("EMU", "EUR")
  30.  
  31.         countriesMap.Add("US", "USD")
  32.         countriesMap.Add("AU", "AUD")
  33.         countriesMap.Add("UK", "GBP")
  34.         countriesMap.Add("NZ", "NZD")
  35.         countriesMap.Add("RU", "RUB")
  36.         countriesMap.Add("ZA", "ZAR")
  37.         countriesMap.Add("SG", "SGD")
  38.  
  39.         countriesMap.Add("NO", "NOK")
  40.         countriesMap.Add("SE", "SEK")
  41.         countriesMap.Add("DK", "DKK")
  42.         countriesMap.Add("PL", "PLN")
  43.         countriesMap.Add("HU", "HUF")
  44.         countriesMap.Add("CZ", "CZK")
  45.  
  46.         countriesMap.Add("MX", "MXN")
  47.         countriesMap.Add("CH", "CHF")
  48.         countriesMap.Add("BR", "BRL")
  49.         countriesMap.Add("CA", "CAD")
  50.         countriesMap.Add("IN", "INR")
  51.         countriesMap.Add("IS", "ISK")
  52.         countriesMap.Add("KR", "KRW")
  53.  
  54.  
  55.         countriesMap.Add("TR", "TRL")
  56.         countriesMap.Add("HK", "HKD")
  57.         countriesMap.Add("PH", "PHP")
  58.         countriesMap.Add("TH", "THB")
  59.         countriesMap.Add("MY", "MYR")
  60.  
  61.         countriesMap.Add("RO", "RON")
  62.         countriesMap.Add("CO", "COP")
  63.         countriesMap.Add("CL", "CLP")
  64.         countriesMap.Add("PE", "PEN")
  65.         countriesMap.Add("ID", "IDR")
  66.  
  67.         If countriesMap.ContainsKey(country) Then
  68.             Return countriesMap.Item(country)
  69.         Else
  70.             localLog("No currency mapping for country " & country, "ERROR")
  71.             Return "ERR"
  72.         End If
  73.  
  74.     End Function
  75.  
  76.     Private Sub localLog(msg As String, sev As String)
  77.         Dim main As frmMain = CType(Application.OpenForms(0), frmMain)
  78.         Dim logger As New frmMain.dlog(AddressOf main.log)
  79.         main.Invoke(logger, msg, sev)
  80.     End Sub
  81.  
  82.     Private Function parseLine(line As HtmlNode, ByRef errFlag As Boolean) As FxEvent
  83.         errFlag = False
  84.         Try
  85.             Dim tds As List(Of HtmlNode) = line.Descendants().Where(Function(x) (x.Name = "td")).ToList
  86.             Dim dateText As String = Trim(tds(0).InnerText)
  87.             Dim countryText As String = tds(2).ChildNodes(1).ChildNodes(1).GetAttributeValue("src", "")
  88.             Dim countryTexta As String() = Split(countryText, "/")
  89.             countryText = countryTexta(UBound(countryTexta)).Replace(".png", "")
  90.             Dim Name As String = Trim(tds(3).InnerText.Replace(" ", ""))
  91.             Dim Impact As String = tds(4).ChildNodes(1).GetAttributeValue("alt", "").Replace(" Impact", "")
  92.             Dim currency As String = countryToCurrency(countryText)
  93.             Dim evDateTime As DateTime = DateTime.ParseExact(dateText, "MMM dd, HH:mm", New CultureInfo("en-US"))
  94.             Return New FxEvent(Name & " - " & countryText, evDateTime, currency, Impact, "MyFxBook")
  95.         Catch ex As Exception
  96.             errFlag = True
  97.             localLog("Error parsing myfxbook line:" & ex.Message, "ERROR")
  98.             Return New FxEvent("error", Date.Now, "ERR", "High", "MyFxBook")
  99.  
  100.         End Try
  101.     End Function
  102.  
  103.  
  104.     Public Sub loadEvents()
  105.         Try
  106.             Dim t1 As DateTime = DateTime.Now
  107.             Dim webClient As New System.Net.WebClient
  108.             Dim today As Date = Date.Today
  109.             Dim url As String = "http://www.myfxbook.com/forex-economic-calendar/3"
  110.             If (today.DayOfWeek > 5) Then
  111.                 localLog("Getting Next Week Events on MyFxBook", "INFO")
  112.                 url = "http://www.myfxbook.com/forex-economic-calendar/9"
  113.             End If
  114.             Dim response As String = webClient.DownloadString(url)
  115.             ' Dim source As [String] = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1)
  116.            ' source = WebUtility.HtmlDecode(source)
  117.            Dim doc As New HtmlDocument()
  118.             doc.LoadHtml(response)
  119.             Dim table As HtmlNode = doc.GetElementbyId("calendar")
  120.             Dim lines As List(Of HtmlNode) = table.Descendants().Where(Function(x) (x.Name = "tr" And InStr(x.GetAttributeValue("class", ""), "pointer") > 0)).ToList()
  121.             FxEvents.Clear()
  122.             Dim ef As Boolean = False
  123.             For Each line As HtmlNode In lines
  124.                 Dim fxe As FxEvent = parseLine(line, ef)
  125.                 If Not ef Then FxEvents.Add(fxe)
  126.             Next
  127.             Dim t2 As DateTime = DateTime.Now
  128.             Dim interval = DateDiff(DateInterval.Second, t1, t2)
  129.             localLog(table.ChildNodes.Count() & " Events fetched in MyFxBook in " & interval & " Seconds", "INFO")
  130.         Catch ex As Exception
  131.             localLog("Error fetching myfxbook html:" & ex.Message, "ERROR")
  132.         End Try
  133.  
  134.     End Sub
  135. End Class
  136.  
  137.  
  138. Imports System
  139. <Serializable()>
  140. Public Class FxEvent
  141.     Public Property timeUTC As DateTime
  142.     Public Property Severity As String
  143.     Public Property Currency As String
  144.     Public Property Name As String
  145.     Public Property Source As String
  146.  
  147.     Public Sub New(eventName As String, eventTime As DateTime, Currency As String, Severity As String, Source As String)
  148.         Me.Source = Source
  149.         Me.timeUTC = eventTime
  150.         Me.Severity = Severity
  151.         Me.Currency = Currency
  152.         Me.Name = eventName
  153.     End Sub
  154.  
  155.     Public Function localTime()
  156.         Return TimeZoneInfo.ConvertTimeFromUtc(Me.timeUTC, TimeZoneInfo.Local)
  157.     End Function
  158. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement