このエントリーをはてなブックマークに追加


PowerShellでCoinExchange.ioのgetmarkets APIにアクセスしてみる

CoinExchange.iogetmarkets APIをPowerShelld呼び出す方法の記事になります。

追記

  • 2018/7/5
    「サーバーによってプロトコル違反が発生しました. Section=ResponseHeader Detail=CR の後には LF を指定しなければなりません。」
    が出力されるようであれば、以下リンク記事を参考にしてください。
  • Section=ResponseHeader Detail=CR の後には LF...の対処方法

関連資料・記事


CoinExchange.io へのリンク

CoinExchange.io

getmarketsの仕様

以下、CoinExchange.io API v1 Referenceのgetmarkets APIの抜粋です。

Get Markets

 This endpoint retrieves all markets.
HTTP Request

GET https://www.coinexchange.io/api/v1/getmarkets
‘getmarkets’ returns JSON structured like this:

{
   "success": "1",
   "request": "/api/v1/public/getmarkets",
   "message": "",
   "result": [
   {

       "MarketID": "1",
       "MarketAssetName": "Megacoin",
       "MarketAssetCode": "MEC",
       "MarketAssetID": "3",
       "MarketAssetType": "currency",
       "BaseCurrency": "Bitcoin",
       "BaseCurrencyCode": "BTC",
       "BaseCurrencyID": "1",
       "Active": true

   },
   {

       "MarketID": "3",
       "MarketAssetName": "Litecoin",
       "MarketAssetCode": "LTC",
       "MarketAssetID": "2",
       "MarketAssetType": "currency",
       "BaseCurrency": "Bitcoin",
       "BaseCurrencyCode": "BTC",
       "BaseCurrencyID": "1",
       "Active": true

   }
   ]
}

getmarkets APIをブラウザでアクセスしてみる

以下のURLにブラウザでアクセスしてみます。

FirefoxだとJSON形式で確認することができます。

JSON生データ
01.png02.png

PowerShellのInvoke-RestMethodを使用してAPIを呼び出してみる

実際に、CoinExchange.iogetmarketsをInvoke-RestMethodコマンドレットを使用して呼び出してみます。

  1. CoinExchange.ioのhttpsがTLS1.2のみなっているので、以下のコマンドでTLS1.2(のみ)を指定します。
    PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  2. 呼び出し元のURL情報を各変数に設定しています。
    PS C:\> $base_url = "https://www.coinexchange.io/api/"
    PS C:\> $version  = "v1"
    PS C:\> $method   = "getmarkets"
  3. getmarkets APIのURLをInvoke-RestMethodの引数にして実行します。
    PS C:\> $response = Invoke-RestMethod -UseBasicParsing "${base_url}${version}/$method"
  4. getmarketsから返却された値を表示しています。
    PS C:\> $response
    
    success request            message result
    ------- -------            ------- ------
    1       /api/v1/getmarkets         {@{MarketID=18; MarketAssetName=Litecoin; MarketAssetCode=LTC; MarketAssetID=2; M...

取得した値から各種値を抽出する

上記で取得し、$responseに格納した値を操作してみます。

  1. 取得したレスポンスは以下のようになっています。
    PS C:\> $response
    
    success request            message result
    ------- -------            ------- ------
    1       /api/v1/getmarkets         {@{MarketID=18; MarketAssetName=Litecoin; MarketAssetCode=LTC; MarketAssetID=2; M...
  2. success, request, messageを取得し表示しています。
    PS C:\> $response.success
    1
    PS C:\> $response.request
    /api/v1/getmarkets
    PS C:\> $response.message
    
  3. 返却されたマッケート情報の数を表示しています。
    PS C:\> $response.result.count
    823
  4. 0番目と823番目を表示しています。
    PS C:\> $response.result[0]
    
    MarketID         : 18
    MarketAssetName  : Litecoin
    MarketAssetCode  : LTC
    MarketAssetID    : 2
    MarketAssetType  : currency
    BaseCurrency     : Bitcoin
    BaseCurrencyCode : BTC
    BaseCurrencyID   : 1
    Active           : True
     
    PS C:\> $response.result[822]
    
    MarketID         : 849
    MarketAssetName  : Sophos
    MarketAssetCode  : SOPH
    MarketAssetID    : 623
    MarketAssetType  : ethereum_asset
    BaseCurrency     : Ethereum
    BaseCurrencyCode : ETH
    BaseCurrencyID   : 70
    Active           : False
  5. MarketAssetNameのみ抽出し、ソート、ユニーク、先頭から10銘柄を表示しています。
    PS C:\> $response.result | % {$_.MarketAssetName} | Sort-Object | Get-Unique | Select-Object -First 10
     imbrex
    Accelerator
    AcesCoin
    ACoin
    Adcoin
    Adshares
    Adzcoin
    AgriNovusCoin
    AkuyaCoin
    ALIS

HTTPステータスなどを一緒に取得したい場合はInvoke-WebRequestが便利!

上記では、Invoke-RestMethodにてJSONをPowerShellで扱いやすい状態に変換してくれますが、
Webサーバから返却された値を変換せずに取得したい場合は、Invoke-WebRequestコマンドレットを使用します。
また、取得後にJSONをPowerShellで扱いやすいオブジェクトに変換するには、ConvertFrom-Jsonコマンドレットを使用します。
以下に実行例を記します。

  1. CoinExchange.ioのhttpsがTLS1.2のみなっているので、以下のコマンドでTLS1.2(のみ)を指定します。
    PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  2. APIのURL作成用の文字列を変数に格納しています。
    PS C:\> $base_url = "https://www.coinexchange.io/api/"
    PS C:\> $version  = "v1"
    PS C:\> $method   = "getmarkets"
  3. Invoke-WebRequestコマンドレットでAPIを呼び出し返却された情報を変数に格納しています。
    PS C:\> $response = Invoke-WebRequest -UseBasicParsing "${base_url}${version}/$method"
  4. 返却された情報を表示してみます。
    PS C:\> $response
    
    StatusCode        : 200
    StatusDescription : OK
    Content           : {"success":"1","request":"\/api\/v1\/getmarkets","message":"","result":[{"MarketID":"18","MarketAss
                        etName":"Litecoin","MarketAssetCode":"LTC","MarketAssetID":"2","MarketAssetType":"currency","BaseCu
                        rr...
    RawContent        : HTTP/1.1 200 OK
                        Transfer-Encoding: chunked
                        Connection: keep-alive
                        x-frame-options: SAMEORIGIN
                        Cache-Control: no-cache
                        Content-Type: application/json
                        Date: Wed, 30 May 2018 14:08:28 GMT
                        Set-Cook...
    Forms             :
    Headers           : {[Transfer-Encoding, chunked], [Connection, keep-alive], [x-frame-options, SAMEORIGIN], [Cache-Cont
                        rol, no-cache]...}
    Images            : {}
    InputFields       : {}
    Links             : {}
    ParsedHtml        :
    RawContentLength  : 174298
  5. HTTPステータスやContent-Typeを簡単に取得することができます。
    PS C:\> $response.StatusCode
    200
    
    PS C:\> $response.Headers.'Content-Type'
    application/json
  6. 取得したJSONをPowerShellで扱うためのオブジェクトに変換する''''を実行してみます。
    PS C:\> $json = $response.Content | ConvertFrom-Json
    PS C:\> $json
    
    success request            message result
    ------- -------            ------- ------
    1       /api/v1/getmarkets         {@{MarketID=18; MarketAssetName=Litecoin; MarketAssetCode=LTC; MarketAssetID=2; M...
    上記のInvoke-RestMethodと同じ状態となります。

Webの応答を読み取っています。(Waiting for response)を非表示にする方法

上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。

03.png

これを非表示にしたい場合は、以下のようにしてください。

非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。
変更前は以下のように Continue が設定されています。

PS C:\> $ProgressPreference
Continue

非表示にするには、以下のように変更します。

PS C:\> $ProgressPreference = "SilentlyContinue"

以上、PowerShellを使って、coinexchang.io.html is not found or not readable.の getmarkets APIを呼び出す方法でした。


CoinExchange.io

CoinExchange.io

添付ファイル: file03.png 219件 [詳細] file02.png 229件 [詳細] file01.png 209件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-07-05 (木) 01:38:05