Java 雅虎财经API

Java 雅虎财经API,java,yahoo-finance,Java,Yahoo Finance,Q.雅虎是否提供任何财务API?如果是,那么该API的链接是什么。这里是我在c#中创建的一个简单刮板,用于将流式报价数据打印到控制台。它应该很容易转换成java。根据以下职位: 不太花哨(即没有正则表达式等),只是一个快速而肮脏的解决方案 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using Sy

Q.雅虎是否提供任何财务API?如果是,那么该API的链接是什么。

这里是我在c#中创建的一个简单刮板,用于将流式报价数据打印到控制台。它应该很容易转换成java。根据以下职位:

不太花哨(即没有正则表达式等),只是一个快速而肮脏的解决方案

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;

namespace WebDataAddin
{
    public class YahooConstants
    {
        public const string AskPrice = "a00";
        public const string BidPrice = "b00";
        public const string DayRangeLow = "g00";
        public const string DayRangeHigh = "h00";
        public const string MarketCap = "j10";
        public const string Volume = "v00";
        public const string AskSize = "a50";
        public const string BidSize = "b60";
        public const string EcnBid = "b30";
        public const string EcnBidSize = "o50";
        public const string EcnExtHrBid = "z03";
        public const string EcnExtHrBidSize = "z04";
        public const string EcnAsk = "b20";
        public const string EcnAskSize = "o40";
        public const string EcnExtHrAsk = "z05";
        public const string EcnExtHrAskSize = "z07";
        public const string EcnDayHigh = "h01";
        public const string EcnDayLow = "g01";
        public const string EcnExtHrDayHigh = "h02";
        public const string EcnExtHrDayLow = "g11";
        public const string LastTradeTimeUnixEpochformat = "t10";
        public const string EcnQuoteLastTime = "t50";
        public const string EcnExtHourTime = "t51";
        public const string RtQuoteLastTime = "t53";
        public const string RtExtHourQuoteLastTime = "t54";
        public const string LastTrade = "l10";
        public const string EcnQuoteLastValue = "l90";
        public const string EcnExtHourPrice = "l91";
        public const string RtQuoteLastValue = "l84";
        public const string RtExtHourQuoteLastValue = "l86";
        public const string QuoteChangeAbsolute = "c10";
        public const string EcnQuoteAfterHourChangeAbsolute = "c81";
        public const string EcnQuoteChangeAbsolute = "c60";
        public const string EcnExtHourChange1 = "z02";
        public const string EcnExtHourChange2 = "z08";
        public const string RtQuoteChangeAbsolute = "c63";
        public const string RtExtHourQuoteAfterHourChangeAbsolute = "c85";
        public const string RtExtHourQuoteChangeAbsolute = "c64";
        public const string QuoteChangePercent = "p20";
        public const string EcnQuoteAfterHourChangePercent = "c82";
        public const string EcnQuoteChangePercent = "p40";
        public const string EcnExtHourPercentChange1 = "p41";
        public const string EcnExtHourPercentChange2 = "z09";
        public const string RtQuoteChangePercent = "p43";
        public const string RtExtHourQuoteAfterHourChangePercent = "c86";
        public const string RtExtHourQuoteChangePercent = "p44";

        public static readonly IDictionary<string, string> CodeMap = typeof(YahooConstants).GetFields().
            Where(field => field.FieldType == typeof(string)).
            ToDictionary(field => ((string)field.GetValue(null)).ToUpper(), field => field.Name);
    }

    public static class StringBuilderExtensions
    {
        public static bool HasPrefix(this StringBuilder builder, string prefix)
        {
            return ContainsAtIndex(builder, prefix, 0);
        }

        public static bool HasSuffix(this StringBuilder builder, string suffix)
        {
            return ContainsAtIndex(builder, suffix, builder.Length - suffix.Length);
        }

        private static bool ContainsAtIndex(this StringBuilder builder, string str, int index)
        {
            if (builder != null && !string.IsNullOrEmpty(str) && index >= 0
                && builder.Length >= str.Length + index)
            {
                return !str.Where((t, i) => builder[index + i] != t).Any();
            }
            return false;
        }
    }

    public class WebDataAddin
    {
        public const string ScriptStart = "<script>";
        public const string ScriptEnd = "</script>";

        public const string MessageStart = "try{parent.yfs_";
        public const string MessageEnd = ");}catch(e){}";

        public const string DataMessage = "u1f(";
        public const string InfoMessage = "mktmcb(";


        protected static T ParseJson<T>(string json)
        {
            // parse json - max acceptable value retrieved from 
            //http://forums.asp.net/t/1343461.aspx
            var deserializer = new JavaScriptSerializer { MaxJsonLength = 2147483647 };
            return deserializer.Deserialize<T>(json);
        }

        public static void Main()
        {
            const string symbols = "GBPUSD=X,SPY,MSFT,BAC,QQQ,GOOG";
            // these are constants in the YahooConstants enum above
            const string attrs = "b00,b60,a00,a50";
            const string url = "http://streamerapi.finance.yahoo.com/streamer/1.0?s={0}&k={1}&r=0&callback=parent.yfs_u1f&mktmcb=parent.yfs_mktmcb&gencallback=parent.yfs_gencb&region=US&lang=en-US&localize=0&mu=1";

            var req = WebRequest.Create(string.Format(url, symbols, attrs));
            req.Proxy.Credentials = CredentialCache.DefaultCredentials;
            var missingCodes = new HashSet<string>();
            var response = req.GetResponse();
            if(response != null)
            {
                var stream = response.GetResponseStream();
                if (stream != null)
                {
                    using (var reader = new StreamReader(stream))
                    {
                        var builder = new StringBuilder();
                        var initialPayloadReceived = false;
                        while (!reader.EndOfStream)
                        {
                            var c = (char)reader.Read();
                            builder.Append(c);
                            if(!initialPayloadReceived)
                            {
                                if (builder.HasSuffix(ScriptStart))
                                {
                                    // chop off the first part, and re-append the
                                    // script tag (this is all we care about)
                                    builder.Clear();
                                    builder.Append(ScriptStart);
                                    initialPayloadReceived = true;
                                }
                            }
                            else
                            {
                                // check if we have a fully formed message
                                // (check suffix first to avoid re-checking 
                                // the prefix over and over)
                                if (builder.HasSuffix(ScriptEnd) &&
                                    builder.HasPrefix(ScriptStart))
                                {
                                    var chop = ScriptStart.Length + MessageStart.Length;
                                    var javascript = builder.ToString(chop,
                                        builder.Length - ScriptEnd.Length - MessageEnd.Length - chop);

                                    if (javascript.StartsWith(DataMessage))
                                    {
                                        var json = ParseJson<Dictionary<string, object>>(
                                            javascript.Substring(DataMessage.Length));

                                        // parse out the data. key should be the symbol

                                        foreach(var symbol in json)
                                        {
                                            Console.WriteLine("Symbol: {0}", symbol.Key);
                                            var symbolData = (Dictionary<string, object>) symbol.Value;
                                            foreach(var dataAttr in symbolData)
                                            {
                                                var codeKey = dataAttr.Key.ToUpper();
                                                if (YahooConstants.CodeMap.ContainsKey(codeKey))
                                                {
                                                    Console.WriteLine("\t{0}: {1}", YahooConstants.
                                                        CodeMap[codeKey], dataAttr.Value);
                                                } else
                                                {
                                                    missingCodes.Add(codeKey);
                                                    Console.WriteLine("\t{0}: {1} (Warning! No Code Mapping Found)", 
                                                        codeKey, dataAttr.Value);
                                                }
                                            }
                                            Console.WriteLine();
                                        }

                                    } else if(javascript.StartsWith(InfoMessage))
                                    {
                                        var json = ParseJson<Dictionary<string, object>>(
                                            javascript.Substring(InfoMessage.Length));

                                        foreach (var dataAttr in json)
                                        {
                                            Console.WriteLine("\t{0}: {1}", dataAttr.Key, dataAttr.Value);
                                        }
                                        Console.WriteLine();
                                    } else
                                    {
                                        throw new Exception("Cannot recognize the message type");
                                    }
                                    builder.Clear();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
Net系统;
使用系统文本;
使用System.Web.Script.Serialization;
命名空间WebDataAddin
{
公共类yahoo常量
{
公共常量字符串AskPrice=“a00”;
公共常量字符串BidPrice=“b00”;
public const string DayRangeLow=“g00”;
public const string dayrangehight=“h00”;
public const string MarketCap=“j10”;
public const string Volume=“v00”;
公用常量字符串AskSize=“a50”;
公共常量字符串BidSize=“b60”;
公共常量字符串EcnBid=“b30”;
公用常量字符串EcnBidSize=“o50”;
公共常量字符串EcnExtHrBid=“z03”;
public const字符串EcnExtHrBidSize=“z04”;
公共常量字符串EcnAsk=“b20”;
public const字符串EcnAskSize=“o40”;
公用常量字符串ecnextrask=“z05”;
公用常量字符串ECNEXTRASKSIZE=“z07”;
public const string EcnDayHigh=“h01”;
公共常量字符串EcnDayLow=“g01”;
公共常量字符串EcnExtHrDayHigh=“h02”;
公共常量字符串EcnExtHrDayLow=“g11”;
public const string LastTradeTimeUnixEpochformat=“t10”;
public const字符串ecnquettelasttime=“t50”;
公共常量字符串EcnExtHourTime=“t51”;
公共常量字符串RtQuoteLastTime=“t53”;
public const string RtExtHourQuoteLastTime=“t54”;
public const string LastTrade=“l10”;
public const字符串ecnquettelastValue=“l90”;
公共常量字符串EcnExtHourPrice=“l91”;
公共常量字符串RtQuoteLastValue=“l84”;
公共常量字符串RtExtHourQuoteLastValue=“l86”;
公用常量字符串QuoteChangeAbsolute=“c10”;
public const string ecnquetteafterhourchangeAbsolute=“c81”;
public const string ecnquettechangeAbsolute=“c60”;
公共常量字符串EcnExtHourChange1=“z02”;
公共常量字符串EcnExtHourChange2=“z08”;
public const字符串RtQuoteChangeAbsolute=“c63”;
public const string RtExtHourQuoteAfterHourChangeAbsolute=“c85”;
public const string RtExtHourQuoteChangeAbsolute=“c64”;
公用常量字符串QuoteChangePercent=“p20”;
public const字符串ecnquetteafterhourChangePercent=“c82”;
公共常量字符串ECNQUOTECHANGERCENT=“p40”;
公共常量字符串EcnExtHourPercentChange1=“p41”;
公共常量字符串EcnExtHourPercentChange2=“z09”;
公共常量字符串RtQuoteChangePercent=“p43”;
公共常量字符串RtExtHourQuoteAfterHourChangePercent=“c86”;
public const字符串RtExtHourQuoteChangePercent=“p44”;
公共静态只读IDictionary CodeMap=typeof(YahooConstants).GetFields()。
其中(field=>field.FieldType==typeof(string))。
ToDictionary(field=>((string)field.GetValue(null)).ToUpper(),field=>field.Name);
}
公共静态类StringBuilderExtensions
{
公共静态bool HasPrefix(此StringBuilder,字符串前缀)
{
返回containsIndex(生成器,前缀,0);
}
公共静态bool HasSuffix(此StringBuilder,字符串后缀)
{
返回containsIndex(builder,后缀,builder.Length-suffix.Length);
}
私有静态bool containsIndex(此StringBuilder、字符串str、int索引)
{
如果(builder!=null&&!string.IsNullOrEmpty(str)&&index>=0
&&builder.Length>=str.Length+索引)
{
return!str.Where((t,i)=>builder[index+i]!=t).Any();
}
返回false;
}
}
公共类WebDataAddin
{
public const字符串ScriptStart=“”;
public const字符串ScriptEnd=“”;
public const string MessageStart=“try{parent.yfs"”;
public const string MessageEnd=“);}catch(e){}”;
public const string DataMessage=“u1f(”;
public const string InfoMessage=“mktmcb(”;
受保护的静态T ParseJson(字符串json)
{
//parse json-从中检索的最大可接受值
//http://forums.asp.net/t/1343461.aspx
var反序列化程序=新的JavaScriptSerializer{MaxJsonLength=2147483647};
返回反序列化程序。反序列化(json);
}
公共静态void Main()
{
const string symbols=“GBPUSD=X,SPY,MSFT,BAC,QQQ,GOOG”;
//这些是上面YahooConstants枚举中的常量
常量字符串attrs=“b00、b60、a00、a50”;
常量字符串url=”http://streamerapi.finance.yahoo.com/streamer/1.0?s={0}&k={1}&r=0&callback=parent.yfs_u1f&mktmcb=parent.yfs_mktmcb&gencallback=parent.yfs_gencb®ion=US&lang=en-US&localize=0&mu=1”;
var req=WebRequest.Create(string.Format(url、符号、属性));
req.Proxy.Credentials=CredentialCache.DefaultCredentials;
var missingCodes=new HashSet();
var response=req.GetResponse();
if(响应!=null)
{
var stream=response.GetResponseStream();
if(流!=null)
{
使用(变量读取器=新的流读取器(流))
{
var builder=新的StringBuilder();
var initialPayloadReceived=false;
而(!reader.EndOfStream)
{
var c=(char)re