コマンドラインでDropboxを操作する(Python library for Dropbox編)


RaspberryのコマンドラインからDropboxを操作する2番目の方法として「Python library for Dropbox」を紹介します。
「Python library for Dropbox」は以下で紹介されています。
https://pypi.python.org/pypi/dropbox

インストールする前にpythonのバージョンを確認してください。

$ python --version
Python 2.7.3

Python2の方はこちら
$ sudo apt install python-pip
$ sudo pip install dropbox

Python3の方はこちら
$ sudo apt-get install python3-pip
$ sudo pip install dropbox

次に以下のページから「dropbox-2.2.0.zip」をダウンロードします。
https://pypi.python.org/pypi/dropbox

上記ファイルを$HOMEに展開すると、「$HOME/dropbox-2.2.0/example/」にサンプルができます。
サンプルの中からクライアントプログラムの使い方を紹介します。
$ ls -l dropbox-2.2.0/example
total 44
-rwxr-xr-x 1 pi pi 10091 Apr 25 07:23 cli_client.py → クライアント プログラム
-rwxr-xr-x 1 pi pi  4253 Sep 19  2014 copy_between_accounts.py
drwxr-xr-x 2 pi pi  4096 Apr 25 07:21 datastore_app
drwxr-xr-x 4 pi pi  4096 Apr 25 07:21 flask_app
-rwxr-xr-x 1 pi pi  1215 Sep 19  2014 oauth1_upgrade.py
-rwxr-xr-x 1 pi pi  8863 Sep 19  2014 search_cache.py

「cli_client.py」の以下の赤字の部分を修正します。
# XXX Fill in your consumer key and secret below
# You can find these at http://www.dropbox.com/developers/apps
APP_KEY = 'ron0m5ovj8vq9f7'  # GuidoFullAccessApp
APP_SECRET = 'ussirz6zlo6s6x6'

デフォルトで指定されているAPP_KEYとAPP_SECRETは既に無効(あるいは適当な値)になっていますので、
前回紹介したApp keyとApp secretに置き換えます。
実行するとurllib3に関するワーニングが出ますが無視しました。
ちなみにこのワーニングはurllib3をアップデートしても消えません。

$ python dropbox-2.2.0/example/cli_client.py
Dropbox> login_oauth1
/usr/local/lib/python2.7/dist-packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
1. Go to: https://www.dropbox.com/1/oauth/authorize?oauth_token=XXXXXXXXXXXX
2. Click "Allow" (you might have to log in first).
3. Press ENTER.

ここで上記「https://www.dropbox.com/1/oauth /authorize?oauth_token=XXXXXXXXXXXX」をブラウザーで開いて
「許可」のボタンを押します。
これでクラウドを操作することができます。
Dropbox> ls → クラウド上のファイル一覧を表示
out2.jpg
Dropbox> get out2.jpg /tmp/out2.jpg → クラウドからローカルにファイルをダウンロード
Metadata: {u'revision': 443, u'bytes': 64665, u'thumb_exists': True, u'rev': u'1bb35162150', u'modified': u'Fri, 24 Apr 2015 22:54:43 +0000', u'mime_type': u'image/jpeg', u'path': u'/out2.jpg', u'is_dir': False, u'size': u'63.1 KB', u'root': u'app_folder', u'client_mtime': u'Fri, 24 Apr 2015 22:54:43 +0000', u'icon': u'page_white_picture'}
Dropbox> rm out2.jpg → クラウド上のファイルを削除
Dropbox> ls
Dropbox> put /tmp/out2.jpg out2.jpg → ローカルからクラウドにファイルをアップ ロード
Dropbox> ls
out2.jpg
Dropbox> logout → ログアウト
Dropbox> exit

一旦ログアウトすると再びログインする必要があります。
ログインしたままexitすると、次はログインする必要がありません。
$ python dropbox-2.2.0/example/cli_client.py
Dropbox> login_oauth1
/usr/local/lib/python2.7/dist-packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
1. Go to: https://www.dropbox.com/1/oauth/authorize?oauth_token=XXXXXXXXXXXX
2. Click "Allow" (you might have to log in first).
3. Press ENTER.

Dropbox> ls
out2.jpg
Dropbox> exit → ログインしたまま終了

$ python dropbox-2.2.0/example/cli_client.py
[loaded OAuth 1 access token]
Dropbox> ls → いきなり使える
/usr/local/lib/python2.7/dist-packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
out2.jpg
Dropbox>

提供されているのはサンプルコードなので、後は好きなようにしてください。