2016年2月20日土曜日

Bitcoin関連データ収集自動化2 Ticker Jason type based API 2 CSV by shell script

環境はubuntu14.04 シェルスクリプトを用いてBitcoin取引所のデータを取得する。
取得するデータはログイン不要でとれるもののみ。
Curlというデフォルトでインストールされていないソフトを用いている。
必要な人は sudo apt-get install curl でインストールしてください。

Bitcoin取引所の多くが同じシステムを使っていたので、
サブルーチン化した上で展開した。

やってることは単純だが、慣れないので非常に時間がかかった。
CurlでJasonフォーマットのファイルを取得。
これとこの保存に時間がかかるので念のため、5秒Sleep。
Jasonフォーマットは使いにくいので、文字置換でカンマ区切りのCSVにした。

本当に疲れた。

前回作ったYahoo finance からの続きになるが、わかりにくいので全部載せた。

#!/bin/sh

#Set the directory which this file exists will be the root directory.
dirname=`dirname $0`;

# make directory for saving the raw data files.
mkdir -p ./Raw_Data; 

#-----------Yahoo finance data--------------
#Get the raw data from Yahoo finance
wget -O ./Raw_Data/Yahoo_data.csv "http://finance.yahoo.com/d/quotes.csv?s=BTCUSD=X+JPY=X&f=nl1" ;
#-----------end--------------

#------------JASON based Ticker routine function-----------------
#This needs the Ticker API_web_address and the part of the data_file_name as valiables.

Ticker(){
curl -X GET "$API_web_address" --output ./Raw_Data/"$data_file_name"_data.csv;

#In case the previous step takes time, this helps the next step.
sleep 5;

#Substitute some characters to form CSV from Jason format.
sed -i -e 's/[{|}|"]//g' -e 's/,/\n/g' -e 's/:/,/g' ./Raw_Data/"$data_file_name"_data.csv
}
#-----------end--------------

#-----------Each website data--------------
#BTC box
API_web_address=https://www.btcbox.co.jp/api/v1/ticker/
data_file_name=btcbox
Ticker

#Bitstamp
API_web_address=https://www.bitstamp.net/api/ticker/
data_file_name=bitstamp
Ticker

#CEX.IO
API_web_address=https://cex.io/api/ticker/BTC/USD
data_file_name=cexio
Ticker
#-----------end--------------

0 件のコメント:

コメントを投稿