AS3: Yahoo Stocks, Flash and ColdFusion

01/02/2010 in RIA View Comments

After searching high and low for a good resource of semi-up-to-the-minute updated stock feeds I came to the conclusion that there aren’t any, for free anyway. I dug into the Google Finance and Data API’s, and made a lot of headway there writing the authentication methods in JavaScript, then in PHP, then in .NET…but was not able to pull something together (quickly) . I plan on revisiting that path, but I digress. I turned to the Yahoo file download that you access via http and it returns a .csv full of stock data from opening trade, current change, volume, etc. This was perfect but how do I convert that to a format that Flash can read?

I used the URLLoader method to load the data, converted it to an Array by splitting at the commas, and then loaded the different values I need by accessing a certain part of the array. This worked perfectly, locally, but then I placed it on our intranet (final resting place), and nothing showed up. Come to find out, the cross domain policies were preventing access to the file on Yahoo’s server. So to make things easier in the long run I wrote a proxy in ColdFusion that the Flash app calls to obtain the stock data. Enough of my explanations and on to the meat…

Click here to download the source files.

I set this project up in FDT and created my normal environment, custom classes, etc. I will include my source files as a zip so you can use it…but will try to explain as best as possible. The files I used are detailed below:

  1. /assets/stockSource.xml
    1. Contains company information and path to the CF Proxy.
  2. com/connatserdev/Stock_Ticker_DC.as
  3. com/connatserdev/events/TickerEvents.as
  4. com/connatserdev/model/Ticker.as
    1. Main class that sets up the XMLSource and YahooStock
  5. com/connatserdev/model/XMLSource.as
    1. Calls the config xml service
  6. com/connatserdev/model/YahooStock.as
    1. Calls the Yahoo service on each loop.

But…basically to access a FREE non web service based stock ticker source from Yahoo, you would need:

  1. ColdFusion page to deliver the feed results from Yahoo, via CFHTTP.FileContent.
  2. Flash file to access the ColdFusion proxy, access the result, parse through it and create an array by splitting the string at “,”.

Click here to download the source files.

As a note, I am also using Grant Skinner’s GTween class and Ted Patrick’s FDOT classes. For some reason the FDOT class would not access the XML on our Dev server, but accessed the dynamic XML classes fine. FDOT would access the XML file locally…just not on our DEV server, so I am assuming it’s just sandbox issue.

Below is the ColdFusion proxy source code:

<cfset urlAddress_Dow="http://download.finance.yahoo.com/d/quotes.csv?s=^DJI&f=sl1d1t1c1ohgv&e=.csv" />
<cfset urlAddress_Google="http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=sl1d1t1c1ohgv&e=.csv" />
 
<cfif #url.stock# is 1>
	<cfhttp url="#urlAddress_Dow#" method="GET" resolveurl="Yes" throwOnError="Yes"/>
<cfelseif #url.stock# is 2>
	<cfhttp url="#urlAddress_Google#" method="GET" resolveurl="Yes" throwOnError="Yes"/>
</cfif>
 
<cfset stockInfo = CFHTTP.FileContent> 	
 
<cfprocessingdirective suppresswhitespace="Yes">
<cfcontent type="text/xml; charset=utf-16">
 
<cfxml variable="xmlobject">
<stocks>
	<company>
		<cfoutput>#stockInfo#</cfoutput>
	</company>
</stocks>
</cfxml>
 
<cfset baseStock=toString(xmlobject)>
<cfset finalStock=replace(baseStock, "UTF-8", "utf-16")>
 
<cfoutput>#finalStock#</cfoutput>
 
</cfprocessingdirective>

Below is the xml feed to determine what stocks to call. Also note the “stockTimer” config. This sets the time each stock is shown.

<?xml version="1.0" encoding="UTF-8"?>
 
<stocks>
	<config>
		<stockTimer>15000</stockTimer>
	</config>
 
	<company name="DJI">
		<path>http://localhost:8500/stockticker_cf/stockproxy.cfm?stock=1</path>
	</company>
	<company name="Google">
		<path>http://localhost:8500/stockticker_cf/stockproxy.cfm?stock=2</path>
	</company>
</stocks>

Other posts you may find helpful:

,

Leave a Reply

Gravatars are enabled. Register for free!

blog comments powered by Disqus