Click here to Skip to main content
15,891,253 members
Articles / Mobile Apps

Yahoo! Managed

Rate me:
Please Sign up or sign in to vote.
4.87/5 (56 votes)
8 Jan 2015Apache12 min read 526.8K   25.5K   262  
Download financial data, managing online portfolio or using Search BOSS from Yahoo! with .NET
'doc:TestFile.xml

' ******************************************************************************
' ** 
' **  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 basic quotes data.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class QuotesBaseDownload
        Inherits Base.Download

        ''' <summary>
        ''' Raises if an asynchronous download of quote data 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 AsyncDownloadCompleted(ByVal sender As Base.Download, ByVal ea As QuotesBaseDownloadCompletedEventArgs)

        Private mXmlParser As New ImportExport.XML
        Friend ReadOnly mFinanceHelper As New FinanceHelper

        ''' <summary>
        ''' Downloads quotes data.
        ''' </summary>
        ''' <param name="managedID">The managed ID</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overloads Function Download(ByVal managedID As IID) As QuotesBaseResponse
            If managedID Is Nothing Then Throw New ArgumentNullException("managedID", "The passed ID is null.")
            Return Me.Download(managedID.ID)
        End Function
        ''' <summary>
        ''' Downloads quotes data.
        ''' </summary>
        ''' <param name="unmanagedID">The unmanaged ID</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overloads Function Download(ByVal unmanagedID As String) As QuotesBaseResponse
            If unmanagedID = String.Empty Then Throw New ArgumentNullException("unmanagedID", "The passed ID is empty.")
            Return Me.Download(New String() {unmanagedID})
        End Function
        ''' <summary>
        ''' Downloads quotes data.
        ''' </summary>
        ''' <param name="managedIDs">The list of managed IDs</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overloads Function Download(ByVal managedIDs As IEnumerable(Of IID)) As QuotesBaseResponse
            If managedIDs Is Nothing Then Throw New ArgumentNullException("managedIDs", "The passed list is null.")
            Return Me.Download(mFinanceHelper.IIDsToStrings(managedIDs))
        End Function
        ''' <summary>
        ''' Downloads quotes data.
        ''' </summary>
        ''' <param name="unmanagedIDs">The list of unmanaged IDs</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overloads Function Download(ByVal unmanagedIDs As IEnumerable(Of String)) As QuotesBaseResponse
            If unmanagedIDs Is Nothing Then Throw New ArgumentNullException("unmanagedIDs", "The passed list is null.")
            Return Me.ToResponse(MyBase.Download(Me.DownloadURL(mHelper.EnumToArray(unmanagedIDs))))
        End Function

        ''' <summary>
        ''' Starts an asynchronous download of quotes data.
        ''' </summary>
        ''' <param name="managedID">The managed ID</param>
        ''' <param name="userArgs">Individual user argument</param>
        ''' <remarks></remarks>
        Public Overloads Sub DownloadAsync(ByVal managedID As IID, Optional ByVal userArgs As Object = Nothing)
            If managedID Is Nothing Then Throw New ArgumentNullException("managedID", "The passed ID is null.")
            Me.DownloadAsync(managedID.ID, userArgs)
        End Sub
        ''' <summary>
        ''' Starts an asynchronous download of quotes data.
        ''' </summary>
        ''' <param name="unmanagedID">The unmanaged ID</param>
        ''' <param name="userArgs">Individual user argument</param>
        ''' <remarks></remarks>
        Public Overloads Sub DownloadAsync(ByVal unmanagedID As String, Optional ByVal userArgs As Object = Nothing)
            If unmanagedID = String.Empty Then Throw New ArgumentNullException("unmanagedID", "The passed ID is empty.")
            Me.DownloadAsync(New String() {unmanagedID}, userArgs)
        End Sub
        ''' <summary>
        ''' Starts an asynchronous download of quotes data.
        ''' </summary>
        ''' <param name="managedIDs">The list of unmanaged IDs</param>
        ''' <param name="userArgs">Individual user argument</param>
        ''' <remarks></remarks>
        Public Overloads Sub DownloadAsync(ByVal managedIDs As IEnumerable(Of IID), Optional ByVal userArgs As Object = Nothing)
            If managedIDs Is Nothing Then Throw New ArgumentNullException("managedIDs", "The passed list is null.")
            Me.DownloadAsync(mFinanceHelper.IIDsToStrings(managedIDs), userArgs)
        End Sub
        ''' <summary>
        ''' Starts an asynchronous download of quotes data.
        ''' </summary>
        ''' <param name="unmanagedIDs">The list of unmanaged IDs</param>
        ''' <param name="userArgs">Individual user argument</param>
        ''' <remarks></remarks>
        Public Overloads Sub DownloadAsync(ByVal unmanagedIDs As IEnumerable(Of String), Optional ByVal userArgs As Object = Nothing)
            If unmanagedIDs Is Nothing Then Throw New ArgumentNullException("unmanagedIDs", "The passed list is null.")
            Dim args As New AsyncDownloadArgs(userArgs, mHelper.EnumToArray(unmanagedIDs))
            MyBase.DownloadAsync(Me.DownloadURL(args.IDs), 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.StreamDownloadCompletedEventArgs) Handles MyBase.AsyncStreamDownloadCompleted
            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 QuotesBaseDownloadCompletedEventArgs(dlArgs.UserArgs, Me.ToResponse(ba.Response), dlArgs.IDs)
                RaiseEvent AsyncDownloadCompleted(Me, args)
            End If
        End Sub

        Private Function DownloadURL(ByVal unmanagedIDs As IEnumerable(Of String)) As String
            Dim lst() As String = mHelper.EnumToArray(unmanagedIDs)
            If lst.Length > 0 Then
                Dim whereClause As New Text.StringBuilder()
                whereClause.Append("symbol in (")
                For Each id As String In unmanagedIDs
                    whereClause.Append(""""c)
                    whereClause.Append(mHelper.CleanYqlParam(id))
                    whereClause.Append(""",")
                Next
                whereClause.Remove(whereClause.Length - 1, 1)
                whereClause.Append(")"c)

                Return mHelper.YqlUrl("*"c, "yahoo.finance.quoteslist", whereClause.ToString, Nothing)
            Else
                Throw New NotSupportedException("An empty id list will not be supported.")
            End If
        End Function

        Private Function ToResponse(ByVal resp As Base.StreamResponse) As QuotesBaseResponse
            Dim quotes As New List(Of QuoteBaseData)
            Dim culture As New Globalization.CultureInfo("en-US")
            Dim results() As XmlNode = mHelper.GetResultsFromXml(resp.Result, "quote")
            For Each node As XmlNode In results
                Dim q As QuoteBaseData = mXmlParser.ToQuoteBaseData(node, culture)
                If q IsNot Nothing Then quotes.Add(q)
            Next
            Return New QuotesBaseResponse(resp.Connection, quotes.ToArray)
        End Function

        Private Class AsyncDownloadArgs
            Inherits Base.DownloadEventArgs
            Public IDs() As String
            Public Sub New(ByVal userArgs As Object, ByVal id() As String)
                MyBase.New(userArgs)
                Me.IDs = id
            End Sub
        End Class

    End Class

    ''' <summary>
    ''' Provides information and response of an asynchronous quotes data download.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class QuotesBaseDownloadCompletedEventArgs
        Inherits Base.DownloadCompletedEventArgs

        Private mIDs() As String

        ''' <summary>
        ''' Gets the used IDs for downloading basic quotes data.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property IDs() As String()
            Get
                Return mIDs
            End Get
        End Property
        ''' <summary>
        ''' Gets the response with basic quotes data.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overloads ReadOnly Property Response() As QuotesBaseResponse
            Get
                Return DirectCast(MyBase.Response, QuotesBaseResponse)
            End Get
        End Property

        Friend Sub New(ByVal userArgs As Object, ByVal resp As QuotesBaseResponse, ByVal ids() As String)
            MyBase.New(userArgs, resp)
            mIDs = ids
        End Sub

    End Class

    ''' <summary>
    ''' Provides connection information and basic quotes data.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class QuotesBaseResponse
        Inherits Base.Response

        ''' <summary>
        ''' Gets the received basic quotes data.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overloads ReadOnly Property Result() As QuoteBaseData()
            Get
                Return TryCast(MyBase.Result, QuoteBaseData())
            End Get
        End Property

        Friend Sub New(ByVal info As Base.ConnectionInfo, ByVal result As QuoteBaseData())
            MyBase.New(info, result)
        End Sub

    End Class

End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions