Closed christafo closed 6 years ago
-- Add this into "function getData"
// Set Market Cap USD
ssRates.getMarketCap('F1').setValue(ABC); ssRates.getMarketCap('F2').setValue(BCD); ssRates.getMarketCap('F3').setValue(CDE);
-- Add this after "function getData" close
// function Market Cap USD
function getMarketCap(currencyId) { var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/'; var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true}); var json = response.getContentText(); var data = JSON.parse(json);
return (data[0]['market_cap_usd']); }
Nice! Can you open a PR for this?
PR = Pull request? Sorry, this is first time I "use" guthub, so no clue how to do it :P
A PR is a Pull Request 🙂
Tried.. but is too confusing :P If someone can just copy that code and do it? I will check tomorrow how to do that, I have couple of more ideas to add to the code
@solifugo does this look right? I can submit the PR for you, but I am not very familiar with js.
var queryString = Math.random();
function getData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
//
//IMPORTANT: Create a sheet called 'Rates'. This is where the values will be written
//
var ssRates = ss.getSheetByName('Rates');
//Grabbing values from CoinMarketCapAPI
//Change the variable name to match the trading symbol
//Change the name in the quotes (e.g. are-bees-carebears) to match the 'id' field from https://api.coinmarketcap.com/v1/ticker/
//Copy/paste to add more lines as needed
var ABC = getRate('are-bees-carebears');
var BCD = getRate('berry-cool-doge');
var CDE = getRate('coin-dank-enigma');
//Grabbing values that are on CoinMarketCap but not in the API
//Change the variable name to match the trading symbol
//Go to the CoinMarketCap page for the currency (e.g. https://coinmarketcap.com/currencies/zeeyx)
//Change the name in quotes (e.g. zeeyx) to match the end of the URL for your currency
//Copy/paste to add more lines as needed
var ZYX = getWebRate('zeeyx');
var YXW = getWebRate('yaaxw');
var XWV = getWebRate('xoowv');
//Setting values in a sheet called 'Rates' (defined at the top)
//Change the values in getRange() to match the cells in the 'Rates' sheet you want to userAgent
//Use the coin symbols from above in setValue()
ssRates.getRange('B1').setValue(ABC);
ssRates.getRange('B2').setValue(BCD);
ssRates.getRange('B3').setValue(CDE);
ssRates.getRange('B4').setValue(ZYX);
ssRates.getRange('B5').setValue(YXW);
ssRates.getRange('B6').setValue(XWV);
// Set Market Cap USD
ssRates.getMarketCap('F1').setValue(ABC);
ssRates.getMarketCap('F2').setValue(BCD);
ssRates.getMarketCap('F3').setValue(CDE);
//VTC wallet balances
//Add more as needed with different variable names
var VtcWallet = getVtcBalance("yourAddressHere");
//Change the value in getRange() to match the cell in spreadsheet
//Change the value in setValue() to match the variable above
ssRates.getRange('E3').setValue(VtcWallet);
//Ethereum Wallet Balances
//Create an account on Etherscan.io
//Create an API key at https://etherscan.io/myapikey
//Put your API key in below, replacing yourEtherscanApiKey
//Add Ethereum address, replacing yourEthAddress
var EthApiKey = "yourEtherscanApiKey";
var EthWallet = getEthBalance(EthApiKey,"yourEthAddress");
//Putting this value in spreadsheet
//Change the value in setValue() to match the variable above
ssRates.getRange('E1').setValue(EthWallet);
}
// function Market Cap USD
function getMarketCap(currencyId) {
var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/';
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
var data = JSON.parse(json);
return (data[0]['market_cap_usd']);
}
//
// DON'T TOUCH ANYTHING BELOW
// IT MAKES THE MAGIC HAPPEN
//
function getEthBalance(ethApiKey,ethAddress) {
var obj = JSON.parse (UrlFetchApp.fetch("https://api.etherscan.io/api?module=account&action=balance&address="+ethAddress+"&tag=latest&apikey="+ethApiKey));
var data = (obj.result);
return data * Math.pow(10,-18);
}
function getVtcBalance(vtcAddress) {
var obj = UrlFetchApp.fetch("http://explorer.vertcoin.info/ext/getbalance/"+vtcAddress);
return obj;
}
function getRate(currencyId) {
var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/';
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
var data = JSON.parse(json);
return parseFloat(data[0]['price_usd']);
}
function getWebRate(currencyId) {
//Example Output:
// '=IMPORTXML("https://coinmarketcap.com/currencies/zeeyx?3908288283","//span[@id=\'quote_price\']")';
var coinScrape1 = '=IMPORTXML("https://coinmarketcap.com/currencies/';
var coinScrape2 = '","//span[@id=\'quote_price\']")';
return coinScrape1 + currencyId + '?' + queryString + coinScrape2;
}
Yes, that should work.
The only problem is that if you have too many coins, you will get a service error.. I'm trying to understand if there is a way to avoid using UrlFetchApp.fetch all the time...
But you can try that for now. Hopefully saitei will see it
Getting several errors with my script - could you take a look?
Removed the wallet functionality as i don't want that... any reason why that will have caused problems?
`var queryString = Math.random();
function getData() { var ss = SpreadsheetApp.getActiveSpreadsheet();
// //IMPORTANT: Create a sheet called 'Rates'. This is where the values will be written // var ssRates = ss.getSheetByName('Rates');
//Grabbing values from CoinMarketCapAPI //Change the variable name to match the trading symbol //Change the name in the quotes (e.g. are-bees-carebears) to match the 'id' field from https://api.coinmarketcap.com/v1/ticker/ //Copy/paste to add more lines as needed
var BTC = getRate('bitcoin'); var ETH = getRate('ethereum'); var ADA = getRate('cardano'); var SC = getRate('siacoin'); var GNT = getRate ('golem-network-tokens'); var TRX = getRate ('tron');
//Grabbing values that are on CoinMarketCap but not in the API //Change the variable name to match the trading symbol //Go to the CoinMarketCap page for the currency (e.g. https://coinmarketcap.com/currencies/zeeyx) //Change the name in quotes (e.g. zeeyx) to match the end of the URL for your currency //Copy/paste to add more lines as needed
var BURST = getWebRate('burst'); var STORJ = getWebRate('storj');
//Setting values in a sheet called 'Rates' (defined at the top) //Change the values in getRange() to match the cells in the 'Rates' sheet you want to userAgent //Use the coin symbols from above in setValue()
ssRates.getRange('B2').setValue(BTC); ssRates.getRange('B3').setValue(ETH); ssRates.getRange('B4').setValue(ADA); ssRates.getRange('B5').setValue(SC); ssRates.getRange('B6').setValue(GNT); ssRates.getRange('B7').setValue(BURST); ssRates.getRange('B8').setValue(STORJ); ssRates.getRange('B9').setValue(TRX);
// Set Market Cap USD
ssRates.getMarketCap('D2').setValue(BTC); ssRates.getMarketCap('D3').setValue(ETH); ssRates.getMarketCap('D4').setValue(ADA); ssRates.getMarketCap('D5').setValue(SC); ssRates.getMarketCap('D6').setValue(GNT); ssRates.getMarketCap('D7').setValue(BURST); ssRates.getMarketCap('D8').setValue(STORJ); ssRates.getMarketCap('D9').setValue(TRX);
}
function getMarketCap(currencyId) { var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/'; var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true}); var json = response.getContentText(); var data = JSON.parse(json);
return (data[0]['market_cap_usd']); }
// // DON'T TOUCH ANYTHING BELOW // IT MAKES THE MAGIC HAPPEN //
function getRate(currencyId) {
var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/'; var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true}); var json = response.getContentText(); var data = JSON.parse(json);
return parseFloat(data[0]['price_usd']); }
function getWebRate(currencyId) { //Example Output: // '=IMPORTXML("https://coinmarketcap.com/currencies/zeeyx?3908288283","//span[@id=\'quote_price\']")';
var coinScrape1 = '=IMPORTXML("https://coinmarketcap.com/currencies/'; var coinScrape2 = '","//span[@id=\'quote_price\']")';
return coinScrape1 + currencyId + '?' + queryString + coinScrape2; }`
I replaced getWebRate from your post and got that function from the original: https://github.com/saitei/crypto-sheets/blob/master/scripteditor.js
function getWebRate(currencyId) { //Example Output: // '=IMPORTXML("https://coinmarketcap.com/currencies/zeeyx?3908288283","//span[@id=\'quote_price\']")';
var coinScrape1 = '=IMPORTXML("https://coinmarketcap.com/currencies/'; var coinScrape2 = '","//span[@id=\'quote_price\']")';
return coinScrape1 + currencyId + '?' + queryString + coinScrape2; }
I also Commented the getMarketCap function and the part where you called, and is working now.
ssRates.getMarketCap('D2').setValue(BTC); ssRates.getMarketCap('D3').setValue(ETH); ssRates.getMarketCap('D4').setValue(ADA); ssRates.getMarketCap('D5').setValue(SC); ssRates.getMarketCap('D6').setValue(GNT); ssRates.getMarketCap('D7').setValue(BURST); ssRates.getMarketCap('D8').setValue(STORJ); ssRates.getMarketCap('D9').setValue(TRX);
function getMarketCap(currencyId) {
var url = 'https://api.coinmarketcap.com/v1/ticker/' + currencyId + '/'; var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true}); var json = response.getContentText(); var data = JSON.parse(json);
return (data[0]['market_cap_usd']); }
I dont see any problem in the getMarketCap function.. trying to see why that doesnt work, but you can comment it for now
Solution to too many API requests would be to scrape API once to a var, and pass var to each "getrate" or "getmarketcap" function. I've completed this in my own version.
New function to get the full CoinMarketCap API ticker:
function getApiObj() {
var url = 'https://api.coinmarketcap.com/v1/ticker/';
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
// Logger.log(JSON.parse(json));
return JSON.parse(json);
}
Modified getRate function:
function getRate(currencyId, apiObj) {
var currencyData = apiObj.filter(function(obj) {
return obj.id == currencyId;
});
return parseFloat(currencyData[0].price_usd);
}
Use:
In getData function, before assigning rate values to coin variables:
var apiObj = getApiObj();
After that, modify the coin rate getters to be like:
var BTC_rate = getRate('bitcoin', apiObj);
var ETH_rate = getRate('ethereum', apiObj);
var LTC_rate = getRate('litecoin', apiObj);
...for example.
I tacked on the "_rate" to each variable because I'm also grabbing the Coin Market Cap.
My market cap function uses the apiObj
grabbed above:
function getMarketCap(currencyId, apiObj) {
var currencyData = apiObj.filter(function(obj) {
return obj.id == currencyId;
});
// Logger.log(currencyData[0]);
return parseFloat(currencyData[0].market_cap_usd);
}
Used in the getData function like so: var BTC_mktcap = getMarketCap('bitcoin', apiObj);
And then:
ssRates.getRange('C2').setValue(BTC_rate);
ssRates.getRange('D2').setValue(BTC_mktcap);
I also added a market cap web rate getter function:
function getWebMarketCap(currencyId) {
//Example Output:
// '=IMPORTXML("https://coinmarketcap.com/currencies/zeeyx?3908288283","//span[@id=\'quote_price\']")';
var coinScrape1 = '=IMPORTXML("https://coinmarketcap.com/currencies/';
var coinScrape2 = '","//span[@data-currency-market-cap]/@data-usd")';
return coinScrape1 + currencyId + '?' + queryString + coinScrape2;
}
And modified the regular web rate getter because it was returning an array that included the text "USD" and I don't need that. This one just returns the rate alone:
//Example Output:
// '=IMPORTXML("https://coinmarketcap.com/currencies/zeeyx?3908288283","//span[@id=\'quote_price\']")';
var coinScrape1 = '=IMPORTXML("https://coinmarketcap.com/currencies/';
var coinScrape2 = '","//span[@id=\'quote_price\']/@data-usd")';
return coinScrape1 + currencyId + '?' + queryString + coinScrape2;
}
@saitei should I do a pull request for any of these? I've never done a pull request before but happy to figure it out!
Wow!
Can't wait to try it tomorrow. Thanks for this.
I will need to find out a way to check the GBP market using this, but it is for sure a great approach.
Thanks for sharing!
Sounds great, definitely do a PR!
On Thu, Dec 28, 2017 at 19:03, Adam Piontek notifications@github.com wrote:
Solution to too many API requests would be to scrape API once to a var, and pass var to each "getrate" or "getmarketcap" function. I've completed this in my own version.
New function to get the full CoinMarketCap API ticker:
function getApiObj() { var url = 'https://api.coinmarketcap.com/v1/ticker/'; var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true}); var json = response.getContentText(); // Logger.log(JSON.parse(json)); return JSON.parse(json); }
Modified getRate function:
function getRate(currencyId, apiObj) { var currencyData = apiObj.filter(function(obj) { return obj.id == currencyId; });
return parseFloat(currencyData[0].price_usd); }
Use: In getData function, before assigning rate values to coin variables: var apiObj = getApiObj();
After that, modify the coin rate getters to be like:
var BTC_rate = getRate('bitcoin', apiObj); var ETH_rate = getRate('ethereum', apiObj); var LTC_rate = getRate('litecoin', apiObj);
...for example.
I tacked on the "_rate" to each variable because I'm also grabbing the Coin Market Cap.
My market cap function uses the apiObj grabbed above:
function getMarketCap(currencyId, apiObj) { var currencyData = apiObj.filter(function(obj) { return obj.id == currencyId; });
// Logger.log(currencyData[0]); return parseFloat(currencyData[0].market_cap_usd); }
Used in the getData function like so: var BTC_mktcap = getMarketCap('bitcoin', apiObj);
And then:
ssRates.getRange('C2').setValue(BTC_rate); ssRates.getRange('D2').setValue(BTC_mktcap);
I also added a market cap web rate getter function:
function getWebMarketCap(currencyId) { //Example Output: // '=IMPORTXML("https://coinmarketcap.com/currencies/zeeyx?3908288283","//span[@id=\'quote_price\']")';
var coinScrape1 = '=IMPORTXML("https://coinmarketcap.com/currencies/'; var coinScrape2 = '","//span[@data-currency-market-cap]/@data-usd")';
return coinScrape1 + currencyId + '?' + queryString + coinScrape2; }
And modified the regular web rate getter because it was returning an array that included the text "USD" and I don't need that. This one just returns the rate alone:
//Example Output: // '=IMPORTXML("https://coinmarketcap.com/currencies/zeeyx?3908288283","//span[@id=\'quote_price\']")';
var coinScrape1 = '=IMPORTXML("https://coinmarketcap.com/currencies/'
; var coinScrape2 = '","//span[@id=\'quote_price\']/@data-usd")';
return coinScrape1 + currencyId + '?' + queryString + coinScrape2; }
@saitei https://github.com/saitei should I do a pull request for any of these? I've never done a pull request before but happy to figure it out!
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/saitei/crypto-sheets/issues/7#issuecomment-354338800, or mute the thread https://github.com/notifications/unsubscribe-auth/AhROby1JgnQurDKgE3H5Zd61m_Zo3jZXks5tE-XrgaJpZM4RNYk- .
I got your code working with "My version" and looks great!!
I will need to make the call for every coin though, since coinmarketcap api doesn't show the prices in GBP market if you don't pass the coin name:
https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=GBP
[ { "id": "bitcoin", "name": "Bitcoin", "symbol": "BTC", "rank": "1", "price_usd": "14618.1", "price_btc": "1.0", "24h_volume_usd": "11315300000.0", "market_cap_usd": "245135304330", "available_supply": "16769300.0", "total_supply": "16769300.0", "max_supply": "21000000.0", "percent_change_1h": "-1.03", "percent_change_24h": "1.1", "percent_change_7d": "7.26", "last_updated": "1514542760", "price_gbp": "10861.7599335", "24h_volume_gbp": "8407663935.5", "market_cap_gbp": "182144110853" } ]
This is how I use your code, in case can help you to polish yours :)
var queryString = Math.random();
function getData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
//
//IMPORTANT: Create a sheet called 'Rates'. This is where the values will be written
//
var ssRates = ss.getSheetByName('Rates');
// Set Rates sheet Titles
ssRates.getRange('A1').setValue("Currency ID");
ssRates.getRange('B1').setValue("Currency Name");
ssRates.getRange('C1').setValue("Symbol");
ssRates.getRange('D1').setValue("Price USD");
ssRates.getRange('E1').setValue("Change 1h");
ssRates.getRange('F1').setValue("Change 24h");
ssRates.getRange('G1').setValue("Change 7d");
ssRates.getRange('H1').setValue("Last Updated UNIX");
ssRates.getRange('I1').setValue("Last Updated Date");
var apiObj = getApiObj();
//Grabbing values from CoinMarketCapAPI
//Setup your coins in the sheet Rates putting the names to match the 'id' field from https://api.coinmarketcap.com/v1/ticker/ in Column A Starting in Cell A2
var Coin1 = ssRates.getRange('A2').getValue();
var Coin2 = ssRates.getRange('A3').getValue();
var Coin3 = ssRates.getRange('A4').getValue();
var Coin4 = ssRates.getRange('A5').getValue();
var Coin5 = ssRates.getRange('A6').getValue();
var Coin6 = ssRates.getRange('A7').getValue();
var Coin7 = ssRates.getRange('A8').getValue();
var Coin8 = ssRates.getRange('A9').getValue();
var Coin9 = ssRates.getRange('A10').getValue();
var Coin10 = ssRates.getRange('A11').getValue();
// Set Coins names
ssRates.getRange("B2:B11").setValues
([
[getName(Coin1, apiObj)],
[getName(Coin2, apiObj)],
[getName(Coin3, apiObj)],
[getName(Coin4, apiObj)],
[getName(Coin5, apiObj)],
[getName(Coin6, apiObj)],
[getName(Coin7, apiObj)],
[getName(Coin8, apiObj)],
[getName(Coin9, apiObj)],
[getName(Coin10, apiObj)]
]);
// Set Coins Symbol
ssRates.getRange("C2:C11").setValues
([
[getSymbol(Coin1, apiObj)],
[getSymbol(Coin2, apiObj)],
[getSymbol(Coin3, apiObj)],
[getSymbol(Coin4, apiObj)],
[getSymbol(Coin5, apiObj)],
[getSymbol(Coin6, apiObj)],
[getSymbol(Coin7, apiObj)],
[getSymbol(Coin8, apiObj)],
[getSymbol(Coin9, apiObj)],
[getSymbol(Coin10, apiObj)]
]);
// Set Current Prices
ssRates.getRange("D2:D11").setValues
([
[getRate(Coin1, apiObj)],
[getRate(Coin2, apiObj)],
[getRate(Coin3, apiObj)],
[getRate(Coin4, apiObj)],
[getRate(Coin5, apiObj)],
[getRate(Coin6, apiObj)],
[getRate(Coin7, apiObj)],
[getRate(Coin8, apiObj)],
[getRate(Coin9, apiObj)],
[getRate(Coin10, apiObj)]
]);
// Set Change 1h
ssRates.getRange("E2:E11").setValues
([
[getCh1h(Coin1, apiObj)],
[getCh1h(Coin2, apiObj)],
[getCh1h(Coin3, apiObj)],
[getCh1h(Coin4, apiObj)],
[getCh1h(Coin5, apiObj)],
[getCh1h(Coin6, apiObj)],
[getCh1h(Coin7, apiObj)],
[getCh1h(Coin8, apiObj)],
[getCh1h(Coin9, apiObj)],
[getCh1h(Coin10, apiObj)]
]);
// Set Change 24h
ssRates.getRange("F2:F11").setValues
([
[getCh24h(Coin1, apiObj)],
[getCh24h(Coin2, apiObj)],
[getCh24h(Coin3, apiObj)],
[getCh24h(Coin4, apiObj)],
[getCh24h(Coin5, apiObj)],
[getCh24h(Coin6, apiObj)],
[getCh24h(Coin7, apiObj)],
[getCh24h(Coin8, apiObj)],
[getCh24h(Coin9, apiObj)],
[getCh24h(Coin10, apiObj)]
]);
// Set Change 7d
ssRates.getRange("G2:G11").setValues
([
[getCh7d(Coin1, apiObj)],
[getCh7d(Coin2, apiObj)],
[getCh7d(Coin3, apiObj)],
[getCh7d(Coin4, apiObj)],
[getCh7d(Coin5, apiObj)],
[getCh7d(Coin6, apiObj)],
[getCh7d(Coin7, apiObj)],
[getCh7d(Coin8, apiObj)],
[getCh7d(Coin9, apiObj)],
[getCh7d(Coin10, apiObj)]
]);
// Set Last Udate Time
ssRates.getRange("H2:H11").setFormula('/86400+date(1970,1,1)').setValues
([
[getLast_Updated(Coin1, apiObj)],
[getLast_Updated(Coin2, apiObj)],
[getLast_Updated(Coin3, apiObj)],
[getLast_Updated(Coin4, apiObj)],
[getLast_Updated(Coin5, apiObj)],
[getLast_Updated(Coin6, apiObj)],
[getLast_Updated(Coin7, apiObj)],
[getLast_Updated(Coin8, apiObj)],
[getLast_Updated(Coin9, apiObj)],
[getLast_Updated(Coin10, apiObj)]
]);
//Convert Unix time to date/time
//=A1/60/60/24 + DATE(1970,1,1)
//And if you want to shift it from UTC to a timestamp that is offset by some number of hours, for example UTC-8:
//=A1/60/60/24 + DATE(1970,1,1) - 8/24
ssRates.getRange("I2:I11").setFormula('=H2:H11/86400+date(1970,1,1)')
}
function getApiObj() {
var url = 'https://api.coinmarketcap.com/v1/ticker/';
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
// Logger.log(JSON.parse(json));
return JSON.parse(json);
}
function getName(currencyId, apiObj) {
var currencyData = apiObj.filter(function(obj) {
return obj.id == currencyId;
});
return (currencyData[0]['name']);
}
function getSymbol(currencyId, apiObj) {
var currencyData = apiObj.filter(function(obj) {
return obj.id == currencyId;
});
return (currencyData[0]['symbol']);
}
function getRate(currencyId, apiObj) {
var currencyData = apiObj.filter(function(obj) {
return obj.id == currencyId;
});
return parseFloat(currencyData[0].price_usd);
}
function getCh1h(currencyId, apiObj) {
var currencyData = apiObj.filter(function(obj) {
return obj.id == currencyId;
});
return parseFloat(currencyData[0].percent_change_1h);
}
function getCh24h(currencyId, apiObj) {
var currencyData = apiObj.filter(function(obj) {
return obj.id == currencyId;
});
return parseFloat(currencyData[0].percent_change_24h);
}
function getCh7d(currencyId, apiObj) {
var currencyData = apiObj.filter(function(obj) {
return obj.id == currencyId;
});
return parseFloat(currencyData[0].percent_change_7d);
}
function getLast_Updated(currencyId, apiObj) {
var currencyData = apiObj.filter(function(obj) {
return obj.id == currencyId;
});
return parseFloat(currencyData[0].last_updated);
}
For GBP conversion in the API, this works for me: https://api.coinmarketcap.com/v1/ticker/?convert=GBP
That gets the full api listing with GBP values, so you could get the data once as I did and not have to hit them for each currency.
Oh, didn't know that you can just add ? To do that, let me try later Edit: Yep, this works really great.Love the "Api call" you made man
Market cap was added. @christafo can we close the issue?
Yep, thanks guys!
addition of market cap