' ******************************************************************************
' **
' ** Yahoo Finance Managed
' ** Written by Marius Häusler 2010
' ** It would be pleasant, if you contact me when you are using this code.
' ** Contact: YahooFinanceManaged@gmail.com
' ** Project Home: http://code.google.com/p/yahoo-finance-managed/
' **
' ******************************************************************************
' **
' ** Copyright 2010 Marius Häusler
' **
' ** Licensed under the Apache License, Version 2.0 (the "License");
' ** you may not use this file except in compliance with the License.
' ** You may obtain a copy of the License at
' **
' ** http://www.apache.org/licenses/LICENSE-2.0
' **
' ** Unless required by applicable law or agreed to in writing, software
' ** distributed under the License is distributed on an "AS IS" BASIS,
' ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' ** See the License for the specific language governing permissions and
' ** limitations under the License.
' **
' ******************************************************************************
Namespace Finance.API
''' <summary>
''' Provides methods for downloading market quotes data.
''' </summary>
''' <remarks></remarks>
Public Class MarketQuotesDownload
Inherits Base.StringDownload
''' <summary>
''' Raises if an asynchronous download of market quotes completes.
''' </summary>
''' <param name="sender">The event raising object</param>
''' <param name="ea">The event args of the asynchronous download</param>
''' <remarks></remarks>
Public Event AsyncDownloadMarketQuotesCompleted(ByVal sender As Base.Download, ByVal ea As MarketQuotesDownloadCompletedEventArgs)
Friend ReadOnly mFinanceHelper As New FinanceHelper
Private mCsvParser As New ImportExport.CSV
''' <summary>
''' Downloads all available sector market quotes.
''' </summary>
''' <param name="rankedBy">The property, the list is ranked by</param>
''' <param name="rankDir">The direction of ranking</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function DownloadAllSectorQuotes(Optional ByVal rankedBy As MarketQuoteProperty = MarketQuoteProperty.Name, Optional ByVal rankDir As System.ComponentModel.ListSortDirection = ListSortDirection.Ascending) As MarketQuotesResponse
Return Me.ToResponse(MyBase.Download(Me.SectorDownloadURL(rankedBy, rankDir)))
End Function
''' <summary>
''' Downloads all available industries of a special sector.
''' </summary>
''' <param name="sector">The sector of the industries</param>
''' <param name="rankedBy">The property the list is ranked by</param>
''' <param name="rankDir">The direction of ranking</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function DownloadIndustryQuotes(ByVal sector As Sector, Optional ByVal rankedBy As MarketQuoteProperty = MarketQuoteProperty.Name, Optional ByVal rankDir As System.ComponentModel.ListSortDirection = ListSortDirection.Ascending) As MarketQuotesResponse
Return Me.ToResponse(MyBase.Download(Me.IndustryDownloadURL(sector, rankedBy, rankDir)))
End Function
''' <summary>
''' Downloads all available company quotes of a special industry.
''' </summary>
''' <param name="industyID">The ID of the industry</param>
''' <param name="rankedBy">The property the list is ranked by</param>
''' <param name="rankDir">The direction of ranking</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function DownloadCompanyQuotes(ByVal industyID As Integer, Optional ByVal rankedBy As MarketQuoteProperty = MarketQuoteProperty.Name, Optional ByVal rankDir As System.ComponentModel.ListSortDirection = System.ComponentModel.ListSortDirection.Ascending) As MarketQuotesResponse
Return Me.ToResponse(MyBase.Download(Me.CompanyDownloadURL(industyID, rankedBy, rankDir)))
End Function
''' <summary>
''' Starts an asynchronous download of all available sector market quotes.
''' </summary>
''' <param name="rankedBy">The property the list is ranked by</param>
''' <param name="rankDir">The direction of ranking</param>
''' <param name="userArgs">Individual user argument</param>
''' <remarks></remarks>
Public Sub DownloadAllSectorQuotesAysnc(Optional ByVal rankedBy As MarketQuoteProperty = MarketQuoteProperty.Name, Optional ByVal rankDir As System.ComponentModel.ListSortDirection = ListSortDirection.Ascending, Optional ByVal userArgs As Object = Nothing)
Dim args As New AsyncDownloadArgs(userArgs, rankedBy, rankDir)
MyBase.DownloadAsync(Me.SectorDownloadURL(args.RankedBy, args.RankDirection), args)
End Sub
''' <summary>
''' Starts an asynchronous download of all available industries of a special sector.
''' </summary>
''' <param name="sector">The sector of the industries</param>
''' <param name="rankedBy">The property the list is ranked by</param>
''' <param name="rankDir">The direction of ranking</param>
''' <param name="userArgs">Individual user argument</param>
''' <remarks></remarks>
Public Sub DownloadIndustryQuotesAsync(ByVal sector As Sector, Optional ByVal rankedBy As MarketQuoteProperty = MarketQuoteProperty.Name, Optional ByVal rankDir As System.ComponentModel.ListSortDirection = ListSortDirection.Ascending, Optional ByVal userArgs As Object = Nothing)
Dim args As New AsyncDownloadArgs(userArgs, rankedBy, rankDir)
MyBase.DownloadAsync(Me.IndustryDownloadURL(sector, args.RankedBy, args.RankDirection), args)
End Sub
''' <summary>
''' Starts an asynchronous download of all available company quotes of a special industry.
''' </summary>
''' <param name="industyID">The ID of the industry</param>
''' <param name="rankedBy">The property the list is ranked by</param>
''' <param name="rankDir">The direction of ranking</param>
''' <param name="userArgs">Individual user argument</param>
''' <remarks></remarks>
Public Sub DownloadCompanyQuotesAsync(ByVal industyID As Integer, Optional ByVal rankedBy As MarketQuoteProperty = MarketQuoteProperty.Name, Optional ByVal rankDir As System.ComponentModel.ListSortDirection = System.ComponentModel.ListSortDirection.Ascending, Optional ByVal userArgs As Object = Nothing)
Dim args As New AsyncDownloadArgs(userArgs, rankedBy, rankDir)
MyBase.DownloadAsync(Me.CompanyDownloadURL(industyID, args.RankedBy, args.RankDirection), args)
End Sub
''' <summary>
''' Default constructor
''' </summary>
''' <remarks></remarks>
Public Sub New()
End Sub
Private Sub DownloadAsync_Completed(ByVal sender As Base.Download, ByVal ba As Base.StringDownloadCompletedEventArgs) Handles MyBase.AsyncStringDownloadCompleted
If ba IsNot Nothing AndAlso ba.UserArgs IsNot Nothing AndAlso TypeOf ba.UserArgs Is AsyncDownloadArgs Then
Dim dlArgs As AsyncDownloadArgs = DirectCast(ba.UserArgs, AsyncDownloadArgs)
Dim args As New MarketQuotesDownloadCompletedEventArgs(dlArgs.UserArgs, Me.ToResponse(ba.Response), dlArgs.RankedBy, dlArgs.RankDirection)
RaiseEvent AsyncDownloadMarketQuotesCompleted(Me, args)
End If
End Sub
Private Function SectorDownloadURL(ByVal rankedBy As MarketQuoteProperty, ByVal rankDir As System.ComponentModel.ListSortDirection) As String
Return "http://biz.yahoo.com/p/csv/" & "s_" & mFinanceHelper.MarketQuotesRankingTypeString(rankedBy) & mFinanceHelper.MarketQuotesRankingDirectionString(rankDir) & ".csv"
End Function
Private Function IndustryDownloadURL(ByVal sector As Sector, ByVal rankedBy As MarketQuoteProperty, ByVal rankDir As System.ComponentModel.ListSortDirection) As String
Return "http://biz.yahoo.com/p/csv/" & (sector + 1).ToString & mFinanceHelper.MarketQuotesRankingTypeString(rankedBy) & mFinanceHelper.MarketQuotesRankingDirectionString(rankDir) & ".csv"
End Function
Private Function CompanyDownloadURL(ByVal industryID As Integer, ByVal rankedBy As MarketQuoteProperty, ByVal rankDir As System.ComponentModel.ListSortDirection) As String
If industryID <= 0 Then : Throw New NotSupportedException("An invalid id will not be supported.")
Else : Return "http://biz.yahoo.com/p/csv/" & industryID.ToString & mFinanceHelper.MarketQuotesRankingTypeString(rankedBy) & mFinanceHelper.MarketQuotesRankingDirectionString(rankDir) & ".csv"
End If
End Function
Private Function ToResponse(ByVal resp As Base.StringResponse) As MarketQuotesResponse
Return New MarketQuotesResponse(resp.Connection, mCsvParser.ToMarketQuotesData(resp.Result, mFinanceHelper.DefaultYqlCulture))
End Function
Private Class AsyncDownloadArgs
Inherits Base.DownloadEventArgs
Public RankedBy As MarketQuoteProperty
Public RankDirection As ListSortDirection
Public Sub New(ByVal userArgs As Object, ByVal rankBy As MarketQuoteProperty, ByVal rankDir As ListSortDirection)
MyBase.New(userArgs)
Me.RankedBy = rankBy
Me.RankDirection = rankDir
End Sub
End Class
End Class
''' <summary>
''' Provides information and response of an asynchronous market quote download.
''' </summary>
''' <remarks></remarks>
Public Class MarketQuotesDownloadCompletedEventArgs
Inherits Base.DownloadCompletedEventArgs
Private mRankedBy As MarketQuoteProperty
Private mRankDirection As ListSortDirection
''' <summary>
''' The ranking of the received list ordered by a specific property.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property RankedBy() As MarketQuoteProperty
Get
Return mRankedBy
End Get
End Property
''' <summary>
''' The rank direction of the received list.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property RankDirection() As System.ComponentModel.ListSortDirection
Get
Return mRankDirection
End Get
End Property
''' <summary>
''' Gets the response with market quotes.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Overloads ReadOnly Property Response() As MarketQuotesResponse
Get
Return DirectCast(MyBase.Response, MarketQuotesResponse)
End Get
End Property
Friend Sub New(ByVal userArgs As Object, ByVal resp As MarketQuotesResponse, ByVal rankBy As MarketQuoteProperty, ByVal rankDir As ListSortDirection)
MyBase.New(userArgs, resp)
mRankedBy = rankBy
mRankDirection = rankDir
End Sub
End Class
''' <summary>
''' Provides connection information and market quotes.
''' </summary>
''' <remarks></remarks>
Public Class MarketQuotesResponse
Inherits Base.Response
''' <summary>
''' The received list of market quotes.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Overloads ReadOnly Property Result() As MarketQuoteData()
Get
Return TryCast(MyBase.Result, MarketQuoteData())
End Get
End Property
Friend Sub New(ByVal info As Base.ConnectionInfo, ByVal result As MarketQuoteData())
MyBase.New(info, result)
End Sub
End Class
End Namespace