ncmbのpythonモジュールがあるらしい・・・
先日のハッカソンでこの話しを聞き、早速使ってみようと思った次第
詳しくはいかにある・・・
http://petitviolet.hatenablog.com/entry/20141011/1413037537
pipインストール
前にやったことがある方法で下記のコマンドを打ってみたがうまくいかず
curl -kL https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python
結局このサイトに載っている
$sudo apt-get install python-pip
でpipインストールを行える状態にした。
しかし、バージョンの問題でインストールできない・・・
そもそもラズパイのPythonバージョンをあげる
ラズパイのバージョンを2.7.7にアップデート
アップデートはこの記事を参考に行った
以下のコマンド群を入力することでバージョンアップを図った
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install build-essential libncursesw5-dev libgdbm-dev libc6-dev
sudo apt-get install zlib1g-dev libsqlite3-dev tk-dev
sudo apt-get install libssl-dev openssl
cd ~
wget https://www.python.org/ftp/python/2.7.7/Python-2.7.7.tgz
tar -zxvf Python-2.7.7.tgz
cd Python-2.7.7
./configure
make -j 4
sudo make install
アップデート後は再起動
sudo reboot
再起動後はバージョンを確認
python -V
と打つと
Python 2.7.7
とでて正しくインストールできていることがわかる
pipの再度インストール
wget https://bootstrap.pypa.io/get-pip.py
sudo python2 get-pip.py
これでpipの準備を行える
ncmbのpythonモジュールのインストールと準備
sudo pip install py_nifty_cloud
また、初期化(シグネチャ生成)のための準備。このモジュールはyamlファイルアプリケーションキー、クライアントキーを読み込むようになっているらしいのでそれを準備する。
下記のような形で・・・
nifty_cloud.yml
APPLICATION_KEY: 'your application key'
CLIENT_KEY: 'your client key'
データベースに書き込みを行う
データベースへの書き込みのコードは下記のように書く
ncmb_post.py
#import
from py_nifty_cloud.nifty_cloud_request import NiftyCloudRequest
# instanciate with yaml file contains APPLICATION KEY and CLIENT KEY
ncr = NiftyCloudRequest('./nifty_cloud.yml')
path = '/classes/TestClass'
method = 'POST'
# post a new recode
values = {'key': 'test'}
response = ncr.post(path=path, query=values)
print(response.status_code)
また、コマンドの画面で下記のような警告が出る
InsecurePlatformWarning
どうしてもでてしまうものらしい・・・
データベースから値を引き出す
同様に引き出すコードは下記
ncmb_get.py
#import
from py_nifty_cloud.nifty_cloud_request import NiftyCloudRequest
# instanciate with yaml file contains APPLICATION KEY and CLIENT KEY
ncr = NiftyCloudRequest('./nifty_cloud.yml')
path = '/classes/TestClass'
query = {'where' : {'key': 'test'}}
method = 'GET'
# standard way to request
# get recodes which matches a query from path, with GET or POST or PUT http method
response = ncr.request(path=path, query=query, method=method)
type(response)
# >>> requests.models.Response
# show status code
print(response.status_code)
# show response as json format
print(response.json())
結果は以下
200
{u'results': [{u'key': u'test', u'createDate': u'2015-10-04T12:17:49.729Z', u'updateDate': u'2015-10-04T12:17:49.730Z', u'objectId': u'mVgYUeowLXFuEQ0R', u'acl': {u'*': {u'read': True, u'write': True}}}]}