Skip to content

Instantly share code, notes, and snippets.

@ashishb
Created January 24, 2014 19:36
Show Gist options
  • Save ashishb/8604460 to your computer and use it in GitHub Desktop.
Save ashishb/8604460 to your computer and use it in GitHub Desktop.
// A fault tolerant Google apps script for fetching dividend data from Yahoo Finance (original fault intolerant script was taken from http://seekingalpha.com/article/568641-using-google-spreadsheet-as-your-watch-list
function GetDividends(symbol) {
/* Manipulate the symbol, as necessary */
if (symbol.search(/\.b/i) >= 0)
{
symbol = symbol.replace(/\.b/i, "-B");
}
var urlToFetch = sprintf('http://finance.yahoo.com/d/quotes.csv?s=%1$s&f=dq',
symbol);
var success = false;
try {
response = UrlFetchApp.fetch(urlToFetch);
success = true;
} catch (e) {
success = false;
}
var i = 0;
while ( (i < NUM_OF_ATTEMPTS) && success && (response.getResponseCode() != 200)) {
try {
Utilities.sleep(SLEEP_TIME_IN_MILLISECONDS);
response = UrlFetchApp.fetch(urlToFetch);
success = true;
} catch (e) {
}
i++;
}
if (success && (response.getResponseCode() == 200)) {
var responseData = response.getContentText();
} else {
var responseData = "-0.0001, -0.002";
}
return responseData.split(',');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment