Bitcoin Forum

Economy => Service Discussion => Topic started by: rizzn on June 27, 2011, 05:16:53 AM



Title: Google Apps and MTGox Trade Data [resolved]
Post by: rizzn on June 27, 2011, 05:16:53 AM
Update: Wasn't able to pull in mtgox trade data into Google App Scripts (so that it could be manipulated as live data in Google Spreadsheets), so I wound up making a mirror script on my personal domain. I had to use the Curl option to set the user agent (something you can't control, to the best of my knowledge, within Google). That, inelegantly, solved my problem.

tl;dr: Google App Scripting can't grab mtgox live trade data without going through an intermediary script. Solution is in this thread.


I'm trying to write a simple script to pull in the latest MTgox Trade Data into a spreadsheet.

Here's what my function looks like:

function mtGoxTradeData() {
  var jsondata = UrlFetchApp.fetch("https://mtgox.com/code/data/ticker.php");
  var object = Utilities.jsonParse(jsondata.getContentText());
  return object.ticker.last;
  
}


For some reason - retrieval of MTGox's API data always times out. When I do the same on ExchangeBitCoins, it gets the data flawlessly.

function exchangeBitCoinsTradeData() {
  var jsondata = UrlFetchApp.fetch("https://www.exchangebitcoins.com/data/ticker");
  var object = Utilities.jsonParse(jsondata.getContentText());
  return object.ticker.last;
  
}


Not sure what the deal is. Anyone experienced with Google's app platform?


Title: Re: Google Apps and MTGox Trade Data
Post by: rizzn on June 27, 2011, 05:54:16 AM
As a follow up - I went ahead and wrote functions for all the trade APIs I could find out there, and mtGox is the only one still not resolving. Did a bit more research, found some folks who said that the best way to retrieve data was thru the WS methods (which is quite a bit more complex than I wanted to make for a simple spreadsheet).

Any help is appreciated.


Title: Re: Google Apps and MTGox Trade Data
Post by: bitfreak! on June 27, 2011, 07:02:28 AM
EDIT: I'm not sure what you're trying to do but you might want to check out this thread: http://forum.bitcoin.org/index.php?topic=22886

Mt. Gox recently changed their API and it seems a user-agent now needs to be specified.
Try using cURL:

Code:
function mtgox_trade_data($json_url) {

  // Initializing curl
  $ch = curl_init();
  
  // Setting curl options
  curl_setopt($ch, CURLOPT_URL, $json_url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_USERAGENT, "php bot");

  // Getting jSON result string
  $result = curl_exec($ch);
  
  // close curl and json file
  curl_close($ch);

  // return cURL result
  return $result;
  
}

Then you can save the result to an associative array like this:

Code:
$json_result = mtgox_trade_data($mtgox_ticker_url);

if (!empty($json_result)) {
  $mt_gox_ticker = json_decode($json_result, true);
}

// use array like this: $mt_gox_ticker['ticker']['sell'];


Title: Re: Google Apps and MTGox Trade Data
Post by: rizzn on June 27, 2011, 07:05:56 AM
That's a bit annoying - about to head off to bed.  Google Apps (from what I'm reading in the forums) doesn't allow you to set the USERAGENT, so I'll probably have to load a script up onto my server to mirror the MTGOX feed, and then try to load that in as a data source to Google Apps.

If I have time to do this tomorrow, I'll update the thread as to whether it works. I was having issues getting Curl to grab and mirror the data when using MTgox as a direct source, or using this as a source: http://btc.jalder.com/mtgox.php (which, theoretically, wouldn't be subject to the USERAGENT limitation).

Incidentally, I also had problems getting Google to not time out on the Jalder.com source as well. The only conclusion that I can reach is that Google hates MTgox.

That this is the only conclusion I can reach is a sure-fire sign I need some sleep. I'll attack it again in the morning.

Thanks for the pointers.

(incidentally, this is the simple Curl script I was using:

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //curl_setopt($ch, CURLOPT_URL, "http://btc.jalder.com/mtgox.php");
    curl_setopt($ch, CURLOPT_URL, "http://mtgox.com/code/data/ticker.php");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    echo $output;
?>


What you're saying is to simply add:

curl_setopt($ch, CURLOPT_USERAGENT, "php bot");

And I should be copacetic?)


Title: Re: Google Apps and MTGox Trade Data
Post by: rizzn on June 27, 2011, 07:13:22 AM
EDIT: I'm not sure what you're trying to do but you might want to check out this thread: http://forum.bitcoin.org/index.php?topic=22886

It's Google's App Scripting language - it's used to do fancy stuff to just about everything on their platform. I was specifically using it to import live JSON data into a spreadsheet.


Title: Re: Google Apps and MTGox Trade Data
Post by: rizzn on June 27, 2011, 07:19:50 AM
Quick update: I lied - I didn't go to sleep, solved it using your useragent CURL idea, and used my own server as Google's data source.  I'll update the thread tomorrow with the breakdown and solution in case anyone else tries to do this.

Thanks a ton.


Title: Re: Google Apps and MTGox Trade Data
Post by: bitfreak! on June 27, 2011, 07:50:39 AM
Glad to see you got it solved. It had me stumped for a a while too. I have no idea why Mt. Gox have made it so hard to use their API.