Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Net
- Imports HtmlAgilityPack
- Imports System.Text
- Imports System.Globalization
- 'http://www.codeproject.com/Tips/804660/How-to-Parse-HTML-using-Csharp
- Public Class MFXHTMLFetcher
- Public FxEvents As List(Of FxEvent)
- Public Sub New()
- FxEvents = New List(Of FxEvent)
- End Sub
- Public Function countryToCurrency(country As String) As String
- Dim countriesMap As New Dictionary(Of String, String)
- countriesMap.Add("JP", "JPY")
- countriesMap.Add("CN", "CNY")
- countriesMap.Add("DE", "EUR")
- countriesMap.Add("FR", "EUR")
- countriesMap.Add("BE", "EUR")
- countriesMap.Add("ES", "EUR")
- countriesMap.Add("PT", "EUR")
- countriesMap.Add("IT", "EUR")
- countriesMap.Add("GR", "EUR")
- countriesMap.Add("NL", "EUR")
- countriesMap.Add("FI", "EUR")
- countriesMap.Add("AT", "EUR")
- countriesMap.Add("IE", "EUR")
- countriesMap.Add("SK", "EUR")
- countriesMap.Add("EMU", "EUR")
- countriesMap.Add("US", "USD")
- countriesMap.Add("AU", "AUD")
- countriesMap.Add("UK", "GBP")
- countriesMap.Add("NZ", "NZD")
- countriesMap.Add("RU", "RUB")
- countriesMap.Add("ZA", "ZAR")
- countriesMap.Add("SG", "SGD")
- countriesMap.Add("NO", "NOK")
- countriesMap.Add("SE", "SEK")
- countriesMap.Add("DK", "DKK")
- countriesMap.Add("PL", "PLN")
- countriesMap.Add("HU", "HUF")
- countriesMap.Add("CZ", "CZK")
- countriesMap.Add("MX", "MXN")
- countriesMap.Add("CH", "CHF")
- countriesMap.Add("BR", "BRL")
- countriesMap.Add("CA", "CAD")
- countriesMap.Add("IN", "INR")
- countriesMap.Add("IS", "ISK")
- countriesMap.Add("KR", "KRW")
- countriesMap.Add("TR", "TRL")
- countriesMap.Add("HK", "HKD")
- countriesMap.Add("PH", "PHP")
- countriesMap.Add("TH", "THB")
- countriesMap.Add("MY", "MYR")
- countriesMap.Add("RO", "RON")
- countriesMap.Add("CO", "COP")
- countriesMap.Add("CL", "CLP")
- countriesMap.Add("PE", "PEN")
- countriesMap.Add("ID", "IDR")
- If countriesMap.ContainsKey(country) Then
- Return countriesMap.Item(country)
- Else
- localLog("No currency mapping for country " & country, "ERROR")
- Return "ERR"
- End If
- End Function
- Private Sub localLog(msg As String, sev As String)
- Dim main As frmMain = CType(Application.OpenForms(0), frmMain)
- Dim logger As New frmMain.dlog(AddressOf main.log)
- main.Invoke(logger, msg, sev)
- End Sub
- Private Function parseLine(line As HtmlNode, ByRef errFlag As Boolean) As FxEvent
- errFlag = False
- Try
- Dim tds As List(Of HtmlNode) = line.Descendants().Where(Function(x) (x.Name = "td")).ToList
- Dim dateText As String = Trim(tds(0).InnerText)
- Dim countryText As String = tds(2).ChildNodes(1).ChildNodes(1).GetAttributeValue("src", "")
- Dim countryTexta As String() = Split(countryText, "/")
- countryText = countryTexta(UBound(countryTexta)).Replace(".png", "")
- Dim Name As String = Trim(tds(3).InnerText.Replace(" ", ""))
- Dim Impact As String = tds(4).ChildNodes(1).GetAttributeValue("alt", "").Replace(" Impact", "")
- Dim currency As String = countryToCurrency(countryText)
- Dim evDateTime As DateTime = DateTime.ParseExact(dateText, "MMM dd, HH:mm", New CultureInfo("en-US"))
- Return New FxEvent(Name & " - " & countryText, evDateTime, currency, Impact, "MyFxBook")
- Catch ex As Exception
- errFlag = True
- localLog("Error parsing myfxbook line:" & ex.Message, "ERROR")
- Return New FxEvent("error", Date.Now, "ERR", "High", "MyFxBook")
- End Try
- End Function
- Public Sub loadEvents()
- Try
- Dim t1 As DateTime = DateTime.Now
- Dim webClient As New System.Net.WebClient
- Dim today As Date = Date.Today
- Dim url As String = "http://www.myfxbook.com/forex-economic-calendar/3"
- If (today.DayOfWeek > 5) Then
- localLog("Getting Next Week Events on MyFxBook", "INFO")
- url = "http://www.myfxbook.com/forex-economic-calendar/9"
- End If
- Dim response As String = webClient.DownloadString(url)
- ' Dim source As [String] = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1)
- ' source = WebUtility.HtmlDecode(source)
- Dim doc As New HtmlDocument()
- doc.LoadHtml(response)
- Dim table As HtmlNode = doc.GetElementbyId("calendar")
- Dim lines As List(Of HtmlNode) = table.Descendants().Where(Function(x) (x.Name = "tr" And InStr(x.GetAttributeValue("class", ""), "pointer") > 0)).ToList()
- FxEvents.Clear()
- Dim ef As Boolean = False
- For Each line As HtmlNode In lines
- Dim fxe As FxEvent = parseLine(line, ef)
- If Not ef Then FxEvents.Add(fxe)
- Next
- Dim t2 As DateTime = DateTime.Now
- Dim interval = DateDiff(DateInterval.Second, t1, t2)
- localLog(table.ChildNodes.Count() & " Events fetched in MyFxBook in " & interval & " Seconds", "INFO")
- Catch ex As Exception
- localLog("Error fetching myfxbook html:" & ex.Message, "ERROR")
- End Try
- End Sub
- End Class
- Imports System
- <Serializable()>
- Public Class FxEvent
- Public Property timeUTC As DateTime
- Public Property Severity As String
- Public Property Currency As String
- Public Property Name As String
- Public Property Source As String
- Public Sub New(eventName As String, eventTime As DateTime, Currency As String, Severity As String, Source As String)
- Me.Source = Source
- Me.timeUTC = eventTime
- Me.Severity = Severity
- Me.Currency = Currency
- Me.Name = eventName
- End Sub
- Public Function localTime()
- Return TimeZoneInfo.ConvertTimeFromUtc(Me.timeUTC, TimeZoneInfo.Local)
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement