- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2018-07-10T09:24:12+09:00","","")
#navi(../)
* BitfinexのTickerをPowerShellで取得する(API v1) [#d4b8ae3f]
PowerShellでBitfinexのTickerを取得する例を以下に記します。~
#contents
#htmlinsert(cc-top.html)
* 参考資料 [#lde09015]
Bitfinexの通Ticker取得するAPIドキュメントURL~
API仕様は、以下のURLで確認してください。~
https://bitfinex.readme.io/v1/reference#rest-public-ticker
* 関連記事 [#nabb8ff8]
-[[Bitfinexの通貨シンボル一覧の取得(API v1)(PowerShell)>API/Bitfinex/通貨ペアのシンボル一覧の取得(API v1)(PowerShell)]]
-[[BitfinexのTickerの取得(API v1)(PowerShell)>API/Bitfinex/Tickerの取得(API v1)(PowerShell)]]
* ブラウザでアクセスしてみる [#fd7b976b]
以下、URLでTickerを取得できます。~
-BTC/USD~
https://api.bitfinex.com/v1/pubticker/btcusd
Firefoxでアクセスした時のスクリーンショットです。~
|''JSON''|''生データ''|
|&ref(01.png);|&ref(02.png);|
#br
-ETH/USD
https://api.bitfinex.com/v1/pubticker/ethusd
Firefoxでアクセスした時のスクリーンショットです。~
|''JSON''|''生データ''|
|&ref(03.png);|&ref(04.png);|
''通貨シンボル一覧(通貨ペア一覧)については、以下のリンク記事を参考にしてください。
-[[Bitfinexの通貨シンボル一覧の取得(API v1)(PowerShell)>API/Bitfinex/通貨ペアのシンボル一覧の取得(API v1)(PowerShell)]]
* PowerShellによるアクセス [#d8d368dd]
PowerShellの''Invoke-RestMethod''コマンドレットと、~
''Invoke-WebRequest''+''ConvertFrom-Json''コマンドレットを使って、\~
BitfinexのTickerを取得してみます。
** ''Invoke-RestMethod''コマンドレットでTickerを取得してみる [#ne8aa42c]
''Invoke-RestMethod''コマンドレットを使用して、BitfinexのTickerを取得する操作例になります。~
以下の操作例は、''ETH/BTC''と''XLM/JPY''の通貨ペアを取得しています。~
''通貨シンボル一覧(通貨ペア一覧)については、以下のリンク記事を参考にしてください。
-[[Bitfinexの通貨シンボル一覧の取得(API v1)(PowerShell)>API/Bitfinex/通貨ペアのシンボル一覧の取得(API v1)(PowerShell)]]
PS C:\> $v1_ticker_api = "https://api.bitfinex.com/v1/pubticker/"
PS C:\> $symbol = "ethbtc"PS C:\> $ticker = Invoke-RestMethod -UseBasicParsing ($v1_ticker_api + $symbol)
PS C:\> $ticker
mid : 0.0706165
bid : 0.070575
ask : 0.070658
last_price : 0.070658
low : 0.070411
high : 0.072696
volume : 12980.205281710008
timestamp : 1531181760.7016923
PS C:\> $symbol = "xlmjpy"
PS C:\> $ticker = Invoke-RestMethod -UseBasicParsing ($v1_ticker_api + $symbol)
PS C:\> $ticker
mid : 22.7525
bid : 22.698
ask : 22.807
last_price : 22.69807138
low : 22.69807138
high : 23.54399956
volume : 10046.78230199
timestamp : 1531181831.9344094
** ''Invoke-WebRequest''+''ConvertFrom-Json''コマンドレットでTickerを取得する [#p36d5ebd]
''Invoke-WebRequest''使うことにより、HTTPステータスや、Content-Typeなどを取得することができます。~
取得したContent部分を''ConvertFrom-Json''コマンドレットを使うことにより、Invoke-RestMethodと同様に、~
JSON形式でアクセスすることができます。
+ Invoke-WebRequestでsymbols APIをアクセス
PS C:\> $btcusd_ticker_url = "https://api.bitfinex.com/v1/ticker/btcusd"
PS C:\> $response = Invoke-WebRequest -UseBasicParsing $btcusd_ticker_url
+ 取得したレスポンスを表示します。
PS C:\> $response
StatusCode : 200
StatusDescription : OK
Content : {"mid":"6660.15","bid":"6660.1","ask":"6660.2","last_price":"6660.1","timestamp":"1531181966.189357
593"}
RawContent : HTTP/1.1 200 OK
Connection: keep-alive
X-Frame-Options: SAMEORIGIN,SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Request-Id: 0bc8dfe5-1a04-4bab-ae67-8add9427fec1
X...
Forms :
Headers : {[Connection, keep-alive], [X-Frame-Options, SAMEORIGIN,SAMEORIGIN], [X-XSS-Protection, 1; mode=blo
ck], [X-Content-Type-Options, nosniff]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml :
RawContentLength : 104
+ステータスコード、HTTP Header, Content-Typeなどの値にアクセスしてみます。
PS C:\> $response.StatusCode
200
#br
PS C:\> $response.Headers
Key Value
--- -----
Connection keep-alive
X-Frame-Options SAMEORIGIN,SAMEORIGIN
X-XSS-Protection 1; mode=block
X-Content-Type-Options nosniff
X-Request-Id 0bc8dfe5-1a04-4bab-ae67-8add9427fec1
X-Runtime 0.005799
Strict-Transport-Security max-age=31536000
Expect-CT max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
CF-RAY 437edbd18d589559-NRT
Content-Length 104
Cache-Control max-age=0, private, must-revalidate
Content-Type application/json; charset=utf-8
Date Tue, 10 Jul 2018 00:19:26 GMT
ETag W/"d19fa253552728a626c37d4221f14969"
Set-Cookie __cfduid=d581c9a228f8c52ac4d92403d2bc0d6ac1531181966; expires=Wed, 10-Jul-19 00:19:26 GMT;...
Server cloudflare
#br
PS C:\> $response.Headers.'Content-Type'
application/json; charset=utf-8
+ ConvertFrom-Jsonコマンドレットを使うことにより、JSON形式に変換することができます。
PS C:\> $json = ConvertFrom-Json $response.Content
PS C:\> $json
mid : 6660.15
bid : 6660.1
ask : 6660.2
last_price : 6660.1
timestamp : 1531181966.189357593
* Webの応答を読み取っています。(Waiting for response)を非表示にする方法 [#w12d22c6]
上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。~
#ref(51.png)
これを非表示にしたい場合は、以下のようにしてください。
非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。~
変更前は以下のように Continue が設定されています。
PS C:\> $ProgressPreference
Continue
非表示にするには、以下のように変更します。
PS C:\> $ProgressPreference = "SilentlyContinue"
以上、PowerShellを使って、Bitfinexのticker APIを呼び出す方法でした。
#htmlinsert(cc-btm.html)