Bitcoin Forum

Bitcoin => Project Development => Topic started by: salfter on December 11, 2012, 06:25:45 AM



Title: Working with data sources for Bitcoin exchanges
Post by: salfter on December 11, 2012, 06:25:45 AM
I was knocking together a page of links today to use as a homepage on my computers, as part of migrating back to Firefox from Chrome. One thing I thought would be useful in this page would be a readout of the current MtGox ask price.  There are two sources I know of that I could pull in with an XMLHttpRequest:

  • http://blockchain.info/q/24hrprice returns a 24-hour average of MtGox and another exchange as plain text...easy to use, but not quite live
  • https://mtgox.com/api/0/data/ticker.php looks like it has the data I want, but is wrapped in JSON...another earlier attempt at parsing JSON returned by MtGox ran into problems

Ideally, something that returned the "sell" item from the MtGox source as plain text would do what I want. Is such a source already out there, or should I try again to get it myself? I've used PHP to parse JSON, but not JavaScript, and eval() and JSON.Parse() (or whatever it's called) each had their problems working with the data I received from MtGox.

Beyond this somewhat immediate need, perhaps this thread would be useful for gathering info on what other data sources are already available.


Title: Re: Working with data sources for Bitcoin exchanges
Post by: bitcoin02 on December 11, 2012, 07:25:49 PM
btcticker.appspot.com


Title: Re: Working with data sources for Bitcoin exchanges
Post by: salfter on December 11, 2012, 10:45:54 PM
btcticker.appspot.com

Interesting...it pulls the information I'd want to use, but it only appears to offer to return the result as text rendered into a PNG.

I've been trying to wrap my head around handling JSON data inside JavaScript.  You'd think this would be easy, but most browsers throw up a roadblock to keep a page from accessing JSON data from other sources.  While this fends off a potential source of XSS attacks, it makes working with data a bit of a pain.  Consider this, based on this demo (http://saltybeagle.com/2009/09/cross-origin-resource-sharing-demo/):

Code:
<html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="http://saltybeagle.com/cors/cors.js"></script>
</head>
<body onload="javascript:OnLoad();">
<div><span id="foo"></span></div>
<script type="text/javascript">
  function OnLoad()
  {
    getCORS("http://ucommbieber.unl.edu/CORS/cors.php", null, function(data) { document.getElementById("foo").innerHTML=data; });
  }
</script>
</body>
</html>

http://ucommbieber.unl.edu/CORS/cors.php returns a plaintext message when you send it a GET request.  getCORS() is supposed to use cross-origin resource sharing (http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) (CORS) to enable fetching data from remote sources.  When you point it at any of the blockchain.info API URLs (even plaintext ones like http://blockchain.info/q/hashrate), though, it fails to pull anything through.  I suspect there's something not implemented server-side.  JSONP fails pretty miserably, too. Is there any method to access JSON data on a remote site from JavaScript in a browser that doesn't rely on you running your own webserver or proxy and that doesn't rely on anything special being set up on the remote server?  Basically, it should be possible to write some HTML and JavaScript into a file and have a browser run it.  I'll be damned if I can figure it out, though.  I've done plenty of JSON manipulation with PHP, so it's not like I'm completely unfamiliar with it.