Finance-Quote-Yahoo (empty) → 0.1
raw patch · 4 files changed
+139/−0 lines, 4 filesdep +HTTPdep +HTTP-Simpledep +MissingHbuild-type:Customsetup-changed
Dependencies added: HTTP, HTTP-Simple, MissingH, base, network
Files
- Finance-Quote-Yahoo.cabal +14/−0
- Finance/Quote/Yahoo.hs +98/−0
- LICENSE +24/−0
- Setup.lhs +3/−0
+ Finance-Quote-Yahoo.cabal view
@@ -0,0 +1,14 @@+Name: Finance-Quote-Yahoo+Version: 0.1+Description: Obtain quote data from finance.yahoo.com+Synopsis: Obtain quote data from finance.yahoo.com+Category: Web+Stability: experimental+Homepage: http://www.b7j0c.org/content/haskell-yquote.html+License: BSD3+License-file: LICENSE+Author: brad clawsie+Maintainer: haskell@fastmail.fm+Build-Depends: base, network, HTTP, HTTP-Simple, MissingH+Exposed-modules: Finance.Quote.Yahoo+ghc-options: -O
+ Finance/Quote/Yahoo.hs view
@@ -0,0 +1,98 @@+{- | Finance.Quote.Yahoo++Finance.Quote.Yahoo is a module to obtain quote information from +finance.yahoo.com, which delivers a csv file with data for various fields, +which are documented at http:\/\/www.gummy-stuff.org\/Yahoo-data.htm.++The only exported function is getQuote, which takes a list of quote symbols +(in the finance sense of \"symbol\" - YHOO,GOOG etc), a list of fields, and +returns a list of pairs for each symbol. Upon any problem, Nothing is +returned.++Here is small complete program illustrating the use of this module++@+ module Main where+ import Finance\.Quote\.Yahoo+ quoteSymbolList = \[\"YHOO\",\"^DJI\"\] :: [QuoteSymbol]+ quoteFieldsList = \[\"s\",\"l1\",\"c\"\] :: [QuoteField]+ main = do+ q <- getQuote quoteSymbolList quoteFieldsList+ case q of+ Nothing -> error \"no return\"+ Just l -> print l+ return ()+@++which outputs:++@+ \[\[(\"s\",\"YHOO\"),(\"l1\",\"26.69\"),(\"c\",\"-0.28 - -1.04%\")\],+ \[(\"s\",\"^DJI\"),(\"l1\",\"13577.87\"),(\"c\",\"+76.17 - +0.56%\")\]\]+@++-}+module Finance.Quote.Yahoo (getQuote,defaultFields,+ QuoteField,QuoteSymbol,QuoteValue) where+import qualified Network.HTTP.Simple as H (httpGet) +import qualified Data.String as S (join,replace,split)+import qualified Network.URI as U (parseURI,escapeURIString,+ isUnescapedInURI) ++{- + License info:++ The license is a simple BSD3-style license available here: + http://www.b7j0c.org/content/license.txt++-}++-- | This is the base uri to get csv quotes. +baseURI = "http://finance.yahoo.com/d/quotes.csv" :: String++type QuoteField = String+type QuoteSymbol = String+type QuoteValue = String++-- | If you just want the symbol, closing price and change, use this.+defaultFields = ["s","l1","c"] :: [QuoteField] ++-- | quoteReq will build a String representation of a Yahoo Finance CSV+-- request URI. +quoteReq :: [QuoteSymbol] -> [QuoteField] -> String+quoteReq symbols fields = + U.escapeURIString U.isUnescapedInURI + (baseURI ++ "?s=" ++ + (S.join "+" symbols) ++ "&f=" ++ (concat fields))++-- | getQuote takes two args - the symbols, and list of the fields you want. +-- The return value is a list of lookup lists, one list per symbol requested,+-- that match the fields you specified with their values.+-- For example, if you were to provide +--+-- \[\"^DJI\",\"YHOO\"\] as the symbols+--+-- \[\"s\",\"c\"\] as the fields, +--+-- the response structure would be+--+-- \[\[(\"s\",\"^DJI\"),(\"c\",\"13,500\")\],+-- \[(\"s\",\"YHOO\"),(\"c\",\"27.00\")\]\]+--+-- As this function is in the Maybe Monad, Nothing is returned on an error.+getQuote :: [QuoteSymbol] -> [QuoteField] -> + IO (Maybe [[(QuoteField,QuoteValue)]])+getQuote symbols fields = + let s = (quoteReq symbols fields) in + case U.parseURI s of+ Nothing -> return Nothing+ Just uri -> do + trycsv <- H.httpGet uri+ case trycsv of+ Nothing -> return Nothing+ Just csv ->+ return (Just (map (zip fields)+ (map (S.split ",") + (lines (S.replace "\r" "" + (S.replace "\"" "" csv)))))) +
+ LICENSE view
@@ -0,0 +1,24 @@+Copyright (c) 2007, Brad Clawsie+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:+ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.+ * Neither the name of b7j0c.org nor the+ names of its contributors may be used to endorse or promote products+ derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY BRAD CLAWSIE ``AS IS'' AND ANY+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL BRAD CLAWSIE BE LIABLE FOR ANY+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.lhs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain