简体   繁体   English

将 CoinMarketCap 数据导入 Google 表格

[英]Import CoinMarketCap Data into Google Sheets

I used CRYPTOFINANCE.ai with Google Spreadsheets for some months now and I want to move on, to be able to get cryptocurrency data by "myself".我将 CRYPTOFINANCE.ai 与Google Spreadsheets一起使用了几个月,我想继续前进,以便能够“自己”获取加密货币数据。

I discover the CoinMarketCap API and that should do it.我发现CoinMarketCap API应该可以。 I succeed in import one or many quotes.我成功导入了一个或多个引号。 Now I would like to import the full listings data so I can have all the prices updated, in order to get a realistic value of my portfolio.现在我想导入完整的列表数据,这样我就可以更新所有价格,从而获得我的投资组合的实际价值。

Here is what I have now, but it isn't importing the full listings:这是我现在拥有的,但它没有导入完整的列表:

function price() {
  var sh1=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Feuille 4'); 
  var requestOptions = {
    method: 'GET',
  uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest',
  qs: {
    'start': '1',
    'limit': '5000',
    'convert': 'USD'}, 
    'headers' : {'X-CMC_PRO_API_KEY': '**********'}, 
    'json': true, 
    'gzip': true};

  var url='https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=ETH'; 
  var result = UrlFetchApp.fetch(url, requestOptions); 
  var txt= result.getContentText();
  var d=JSON.parse(txt);
  sh1.getRange(1, 2).setValue(d.data.ETH.quote.USD.price)
}

I know it has something to deal with: https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest but I couldn't figure it out by myself.我知道它有一些事情要处理: https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest但我无法自己弄清楚。

I am not familiar with this site but when I try the URL on its own it returns an error stating the API key is missing我不熟悉这个站点,但是当我自己尝试 URL 时,它返回一个错误,指出 API 密钥丢失

{
    "status": {
        "timestamp": "2021-03-31T12:29:45.203Z",
        "error_code": 1002,
        "error_message": "API key missing.",
        "elapsed": 0,
        "credit_count": 0
    }
}

Also, it looks like Yahoo Finance has crypto historical data for free, no API required, just make an appropriate web request.此外,雅虎财经似乎有免费的加密历史数据,不需要 API,只需提出适当的 web 请求即可。 This one grabs BTC from March 31, 2020 thru March 31, 2021.这个从 2020 年 3 月 31 日到 2021 年 3 月 31 日期间获取 BTC。

https://query1.finance.yahoo.com/v7/finance/download/BTC-USD?period1=1585662494&period2=1617198494&interval=1d&events=history&includeAdjustedClose=true

One would need to decipher the format for period1 and period2, I believe they are UNIX timestamps, I was able to confirm at this site:需要破译 period1 和 period2 的格式,我相信它们是 UNIX 时间戳,我可以在这个站点上确认:

https://www.unixtimestamp.com/ https://www.unixtimestamp.com/

Then this code would download the data into the current sheet:然后此代码会将数据下载到当前工作表中:

function importCSVFromWeb() {
  // Provide the full URL of the CSV file.
  var csvUrl = "function importCSVFromWeb() {
  // Provide the full URL of the CSV file.
  var csvUrl = "https://query1.finance.yahoo.com/v7/finance/download/BTC-USD?period1=1585662494&period2=1617198494&interval=1d&events=history&includeAdjustedClose=true";
  var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
  var csvData = Utilities.parseCsv(csvContent);

  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

BTW, it looks like Yahoo Finance gets their crypto data from CoinMarketCap顺便说一句,雅虎财经似乎从 CoinMarketCap 获取了他们的加密数据

I was mistaken two requests: /listings and /quotes我弄错了两个请求:/listings 和 /quotes

  • Listing gives you a list of multiple currencies.列表为您提供多种货币的列表。

  • Quotes gives you data from one coin only行情只给你一个硬币的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM