【问题标题】:How to get the most profitable currency from a JSON?如何从 JSON 中获得最有利可图的货币?
【发布时间】:2018-02-11 21:26:05
【问题描述】:

我正在尝试编写一个 bash 脚本,该脚本可以适应最有利可图的加密货币来挖掘并即时更改正在挖掘的内容。

我使用curl 使用curl 脚本从http://whattomine.com/coins.json 获取API 信息:

curl whattomine.com/coins.json -O | > coins.json

示例 API:

{"coins":{"Nicehash-Ethash":{"id":15,"tag":"NICEHASH","algorithm":"Ethash","block_time":1,"block_reward":1,"block_reward24":1,"last_block":0,"difficulty":1,"difficulty24":1,"nethash":5595177528830,"exchange_rate":0.0084,"exchange_rate24":0.0086,"exchange_rate_vol":38.522578574534,"exchange_rate_curr":"BTC","market_cap":"$0.00","estimated_rewards":"0.00071","estimated_rewards24":"0.00072","btc_revenue":"0.0007056","btc_revenue24":"0.0007224","profitability":95,"profitability24":101,"lagging":false,"timestamp":1518385271},"EthereumClassic":{"id":162,"tag":"ETC","algorithm":"Ethash","block_time":"14.5606","block_reward":3.88,"block_reward24":3.88000000000005,"last_block":5372259,"difficulty":119798042603096.0,"difficulty24":118354683854950.0,"nethash":8227548494093,"exchange_rate":0.003034,"exchange_rate24":0.00285318393234672,"exchange_rate_vol":2318.70107084,"exchange_rate_curr":"BTC","market_cap":"$2,525,050,483.01","estimated_rewards":"0.23506","estimated_rewards24":"0.23792","btc_revenue":"0.00071316","btc_revenue24":"0.00072186","profitability":96,"profitability24":101,"lagging":false,"timestamp":1518385186},"Ethereum":{"id":151,"tag":"ETH","algorithm":"Ethash","block_time":"14.529","block_reward":2.91,"block_reward24":2.91000000000001,"last_block":5073173,"difficulty":2.86396845523669e+15,"difficulty24":2.96145362923571e+15,"nethash":197120824229932,"exchange_rate":0.100691,"exchange_rate24":0.100200274841438,"exchange_rate_vol":12443.48347244,"exchange_rate_curr":"BTC","market_cap":"$81,928,528,426.15","estimated_rewards":"0.00737","estimated_rewards24":"0.00713","btc_revenue":"0.00074252","btc_revenue24":"0.00071808","profitability":100,"profitability24":100,"lagging":false,"timestamp":1518385171},"Expanse":{"id":154,"tag":"EXP","algorithm":"Ethash","block_time":"45.0","block_reward":4.0,"block_reward24":4.0,"last_block":975425,"difficulty":13101309242779.0,"difficulty24":14838722424377.6,"nethash":291140205395,"exchange_rate":0.00033176,"exchange_rate24":0.000334064524312897,"exchange_rate_vol":10.94411548,"exchange_rate_curr":"BTC","market_cap":"$21,877,350.59","estimated_rewards":"2.2152","estimated_rewards24":"1.9559","btc_revenue":"0.00073491","btc_revenue24":"0.00064889","profitability":99,"profitability24":90,"lagging":false,"timestamp":1518383559},"Pirl":{"id":211,"tag":"PIRL","algorithm":"Ethash","block_time":"14.0","block_reward":10.0,"block_reward24":10.0,"last_block":825434,"difficulty":16734197347723.0,"difficulty24":16762811072321.5,"nethash":1195299810551,"exchange_rate":0.00014626,"exchange_rate24":0.000155109957716702,"exchange_rate_vol":12.9604288790783,"exchange_rate_curr":"BTC","market_cap":"$12,081,862.82","estimated_rewards":"4.33668","estimated_rewards24":"4.32928","btc_revenue":"0.00063428","btc_revenue24":"0.0006332","profitability":85,"profitability24":88,"lagging":false,"timestamp":1518385184},"Musicoin":{"id":178,"tag":"MUSIC","algorithm":"Ethash","block_time":"14.0","block_reward":246.2,"block_reward24":246.199999999998,"last_block":1953827,"difficulty":6613543703874.0,"difficulty24":6686438484420.37,"nethash":472395978848,"exchange_rate":2.34e-06,"exchange_rate24":2.29601694915254e-06,"exchange_rate_vol":17.78104309,"exchange_rate_curr":"BTC","market_cap":"$11,918,018.41","estimated_rewards":"270.12802","estimated_rewards24":"267.18363","btc_revenue":"0.0006321","btc_revenue24":"0.00062521","profitability":85,"profitability24":87,"lagging":false,"timestamp":1518385185},"Ellaism":{"id":221,"tag":"ELLA","algorithm":"Ethash","block_time":"14.0","block_reward":4.91,"block_reward24":4.91000000000001,"last_block":945964,"difficulty":4376186239122.0,"difficulty24":4319302576114.62,"nethash":312584731365,"exchange_rate":7.501e-05,"exchange_rate24":7.6147420718816e-05,"exchange_rate_vol":1.27998339143897,"exchange_rate_curr":"BTC","market_cap":"$2,958,733.31","estimated_rewards":"8.14071","estimated_rewards24":"8.24789","btc_revenue":"0.00061063","btc_revenue24":"0.00061867","profitability":82,"profitability24":86,"lagging":false,"timestamp":1518385172}

我想使用profitability24 字段。

现在我有了一个包含所需信息的 json,我在弄清楚如何获取最有利可图的货币方面遇到了很多问题(它始终是 json 中的第一个)。我的计划是使用 jq,但我对想法持开放态度。我的下一步是对最有利可图的货币进行变量化,然后编写适当的命令让矿工开始挖掘该货币。

【问题讨论】:

  • 包含一个简短的数据结构片段可以防止用户点击链接。您还应该将代码包含为文本,而不是图像。
  • 我有点新,所以请耐心等待。我进行了请求的更改。
  • 管道 (|) 是一种重定向,而> 是另一种输出重定向。但是您使用的是-O 选项,因此您应该只需要curl -O whattomine.com/coins.json 将文件保存到本地目录。下面的答案寻找您问题的jq 部分。祝你好运。

标签: json bash curl max jq


【解决方案1】:

既然你指出你想要的项目是“永远是第一个”,你可以简单地写:

first(.coins[])

如果您想确保始终选择最大对象之一,您可以这样写:

[.coins[]] | max_by(.profitability24)

(对于样本数据,这两种方法给出了不同的答案,因为有两个最大的对象。)

如果您想要所有个最大对象,那么一种方法是定义和使用maximals_by(s; f),如下所示:

# Emit a stream of the maximal entities in the stream, s:
def maximals_by(s; f):
  reduce s as $x ({v:null, a:[]}; 
    ($x|f) as $y
    | if $y == .v then .a += [$x] elif $y > .v then {v:$y, a: [$x]} else . end)
  | .a[];


maximals_by(.coins[]; .profitability24)

对于给定的数据,最后一种方法产生两个最大的项目:

{
  "id": 15,
  "tag": "NICEHASH",
  "algorithm": "Ethash",
  "block_time": 1,
  "block_reward": 1,
  "block_reward24": 1,
  "last_block": 0,
  "difficulty": 1,
  "difficulty24": 1,
  "nethash": 5595177528830,
  "exchange_rate": 0.0084,
  "exchange_rate24": 0.0086,
  "exchange_rate_vol": 38.522578574534,
  "exchange_rate_curr": "BTC",
  "market_cap": "$0.00",
  "estimated_rewards": "0.00071",
  "estimated_rewards24": "0.00072",
  "btc_revenue": "0.0007056",
  "btc_revenue24": "0.0007224",
  "profitability": 95,
  "profitability24": 101,
  "lagging": false,
  "timestamp": 1518385271
}
{
  "id": 162,
  "tag": "ETC",
  "algorithm": "Ethash",
  "block_time": "14.5606",
  "block_reward": 3.88,
  "block_reward24": 3.88000000000005,
  "last_block": 5372259,
  "difficulty": 119798042603096,
  "difficulty24": 118354683854950,
  "nethash": 8227548494093,
  "exchange_rate": 0.003034,
  "exchange_rate24": 0.00285318393234672,
  "exchange_rate_vol": 2318.70107084,
  "exchange_rate_curr": "BTC",
  "market_cap": "$2,525,050,483.01",
  "estimated_rewards": "0.23506",
  "estimated_rewards24": "0.23792",
  "btc_revenue": "0.00071316",
  "btc_revenue24": "0.00072186",
  "profitability": 96,
  "profitability24": 101,
  "lagging": false,
  "timestamp": 1518385186
}

【讨论】:

    【解决方案2】:

    假设你的数据结构是这样的:

    $ cat test.json
    {"coins" : { "coin1": { "id": 4,  "profitability24": 3 },
                 "coin2": { "id": 5, "profitability24": 2 },
                 "coin3": { "id": 6, "profitability24": 1 } } }
    

    如果您想提取 'profitability24 的最大值,我只需将字典过滤到一个列表中,然后通过管道将其发送到 max_by(您是否阅读了 the jq docs?):

    $ cat test.json | jq '[.coins[]]|max_by(.profitability24)'
    {
       "id": 4,
       "profitability24": 3
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-07
      • 2012-07-17
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多