I am using coinmarketcap api to populate google sheets with data. https://coinmarketcap.com/api/
They are making some changes that will make this difficult. The two changes that are messing me up are pagination and a change in the JSON structure.
Previously I would have a column of coin ids in google sheets. I would grab all the coin data from coinmarketcap then iterate through the javascript array to get data I needed. Here is an example
function getMarketCap(sheetname) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetname);
var assets = [];
var idRange = sheet.getRange("B1:B");
var lastRow = getLastRowOfRange(idRange);
var cellRange = sheet.getRange(1, 2, lastRow).getValues();
var mcRange = sheet.getRange(1, 3, lastRow);
var mcValues = [];
for (var i = 0; i < cellRange.length; i++) {
assets[i] = cellRange[i];
}
var CMCdata = JSON.parse(UrlFetchApp.fetch('https://api.coinmarketcap.com/v1/ticker/?limit=0'));
for (var i = 0; i < assets.length; i++) {
mcValues[i] = [];
for (var x = 0; x < CMCdata.length; x++) {
if (assets[i] == CMCdata[x]["id"]) {
mcValues[i][0] = CMCdata[x]["market_cap_usd"];
}
if (!mcValues[i][0]) {
mcValues[i][0] = 'xxx';
}
}
}
mcRange.setValues(mcValues);
}
Now with v2 they are making it so you can only get 100 results at a time instead of all at once https://api.coinmarketcap.com/v2/ticker/ vs https://api.coinmarketcap.com/v1/ticker/?limit=0.
How can I do what I was doing before with these changes? Is it possible to make 12 requests (100 coins at a time for about 1200 total), appending the results each time and then iterate through the data? How would I do this? I only have a very basic knowledge of coding and someone helped me with the code shown above.
How about this modification?
Modification points :
- From v1 to v2, there are the following modifications in your script.
id
became website_slug
.
market_cap_usd
became USD.market_cap
.
- ID number became the key of each element.
- In order to request 12 API calls, the fetchAll method is used.
Modified script :
function getMarketCap(sheetname) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetname);
var assets = [];
var idRange = sheet.getRange("B1:B");
var lastRow = getLastRowOfRange(idRange);
var cellRange = sheet.getRange(1, 2, lastRow).getValues();
var mcRange = sheet.getRange(1, 3, lastRow);
var mcValues = [];
for (var i = 0; i < cellRange.length; i++) {
assets[i] = cellRange[i];
}
// Added script --- begin
var req = [];
for (var i = 0; i < 12; i++) {
req.push({
muteHttpExceptions: true,
method: "get",
url: "https://api.coinmarketcap.com/v2/ticker/?start=" + (i * 100 + 1),
});
}
var responses = UrlFetchApp.fetchAll(req);
var res = responses.filter(function(e){return e.getResponseCode() == 200}).map(function(e){return JSON.parse(e.getContentText())});
if (responses.length != res.length) Logger.log("%s errors occurred.", responses.length - res.length);
var mcValues = [];
assets.forEach(function(e, h) {
mcValues[h] = [];
res.some(function(f) {
Object.keys(f.data).some(function(g) {
if (f.data[g].website_slug == e[0]) {
mcValues[h][0] = f.data[g].quotes.USD.market_cap;
return true;
}
});
if (mcValues[h][0]) return true;
});
if (!mcValues[h][0]) mcValues[h][0] = 'xxx';
});
// Added script --- end
mcRange.setValues(mcValues);
}
Note :
- When I tested this script, I noticed that the error due to the rate limitation sometimes occurs.
- In this case, HTML including
<title>Access denied | api.coinmarketcap.com used Cloudflare to restrict access</title>
is returned.
- In this script, when the error occurs, "errors occurred." is output using
Logger.log()
.
- When the limitation error occurs, please wait until the restriction is released. I thought that if there are the access token and tokens for each user, users might be able to adjust the API calls. But I couldn't find such tokens. I'm sorry.
Reference :
In my environment, I could confirm this modified script worked. But if this was not what you want, I'm sorry.