dotcloudによるすぐ始める
 twitter webアプリ開発
 Twitter API勉強会 2012年4月24日
  python-CLI編 PHP編 #twtr_hack
             @yut148
自己紹介
@yut148

      職業 クラウドを大量に使っているゲーム会社
                or
       エンターテイメント会社に勤めています。

  最近所属変更があって担当職務がよくわかりませんが、
  前職含め、ゲーム会社にてサーバ管理の仕事が多い。
   写経家 要はコピペPG程度(←↑今日はこれ大事)
        会社のソースはPHPが多い
本日のお題

1)twitterの webアプリケーション開発上の
  問題
2)dotcloudについて
3)サンプル
皆様、twitterのwebアプリ開発されてますか?

アプリケーション登録必要ですよね。。
ところで、サーバどうしてますか?
・リアルなサーバで開発を行うデメリット
 →電気代、ハードウェア故障、熱
・VPS、レンタルサーバ、クラウドで開発を行うデメ
リット
 →利用用金、クレジットカード
皆様、twitterのwebアプリ開発されてますか?

アプリケーション登録必要ですよね。。
ところで、サーバどうしてますか?
これらに共通の問題
・リアルなサーバで開発を行うデメリット
 →電気代、ハードウェア故障、熱
・VPS、レンタルサーバ、クラウドで開発を行うデメ
リット
 →利用用金、クレジットカード

ネットワーク設定 ドメイン費用、名前解決、NAT設定
OS等の環境設定 ミドルウェアの設定
 Apache、.htaccess、ssh
セキュリティパッチ 故障対応ログの維持
皆様、twitterの開発されてますか?

ところで、サーバどうしてますか?

・リアルなサーバで開発を行うデメリット
 →電気代、ハードウェア故障、熱
  要は結局の所
・VPS、レンタルサーバ、クラウドで開発を行うデメ
リット
 結論:めんどくさい
 →利用用金、クレジットカード

ネットワーク設定 ドメイン費用、名前解決、NAT設定
OS等の環境設定 ミドルウェアの設定
 Apache、.htaccess、ssh
セキュリティパッチ 故障対応ログの維持
2)dotcloudについて

そこで、PaaSのdotcloudなら、、↓これらの作業は
不要です。

ネットワーク設定 BIND、名前解決、NAT設定
OS等の環境設定 ミドルウェアの設定
 Apache、.htaccess、ssh
セキュリティパッチ 故障対応ログの維持
2)dotcloudについて

そこで、PaaSのdotcloudなら、、↓これらの作業は
不要です。

ネットワーク設定 BIND、名前解決、NAT設定
OS等の環境設定 ミドルウェアの設定
 Apache、.htaccess、ssh
セキュリティパッチ 故障対応ログの維持


しかもFreeアカウントならクレジットカード
          等不要
         dotcloud.com

dotcloudなら、

とりあえずドメインがふられる。
 ∴ アプリケーション登録ができる。
dotcloud Sign UpSign Up(アカウント作成)に必
要なもの

・python環境 ver2.6が必要
 windowsでもMacでもLinuxでもOK
 
 けれど、virtualBOXとかでLinuxを動かした
 ほうがおすすめ

・メールアドレス
 gmailとかでok
dotcloud.com       サインはこちらから




   ※サービス名+アカウント名のドメインになるの
   で、アカウント登録の時に注意
開発環境の下準備

 dotcloudというコマンドのインストール




詳しくは   →
http://goo.gl/cCYVx
3)サンプル




    python編
    コマンドラインでの利用
入門ソーシャルデータ 著者Matthew Russell
(P96)twitterのフォローしているユーザID取得を試
すには、、 

このコードを試したい↓




https://github.com/ptwobrussell/Mining-the-Social-
Web/blob/master/python_code/friends_followers__get_friends.py

http://goo.gl/QJxXL
しかしtwitterのアプリケーション登録ってドメイン必
要ですよね。。




   ここ
でdotcloudなら、、

http://friendsfollowersgetfriens-3opsr7qp.
dotcloud.com/
サブドメインが設定されます。

アプリケーション名ーユーザ名.dotcloud.com
これでアプリケーション登録できます。

※実際は設定されたドメインを調べる必要がありま
す。
開発手順
1)自分の環境でディレクトリとファイルを作る
[root@localhost ‾]# ls -laR friends_followers__get_friends/
friends_followers__get_friends/:
total 16
drwxr-xr-x 3 root root 4096 Apr 14 12:10 .
dr-xr-x---. 5 root root 4096 Apr 14 12:36 ..
-rw-r--r-- 1 root root 62 Apr 14 12:10 dotcloud.yml
drwxr-xr-x 2 root root 4096 Apr 14 16:14 friends_followers__get_friends

friends_followers__get_friends/friends_followers__get_friends:
total 12
drwxr-xr-x 2 root root 4096 Apr 14 16:14 .
drwxr-xr-x 3 root root 4096 Apr 14 12:10 ..
-rw-rw-r-- 1 root root 2341 Mar 29 23:02 friends_followers__get_friends.py
[root@localhost ‾]# cat friends_followers__get_friends/dotcloud.yml
web:
  type: python
 approot: friends_followers__get_friends
開発手順

2)dotcloud コマンドでpushして
dotcloud push friendsfollowersgetfriends
friends_followers__get_friends
3)sshでログイン
dotcloud ssh friendsfollowersgetfriends.web
twitterだけはインストール
# $SHELL
dotcloud@friendsfollowersgetfriends-default-
web-0:‾$ easy_install twitter
実行
dotcloud@friendsfollowersgetfriends-default-web-0:‾/code/friends_followers__get_friends$ python
friends_followers__get_friends.py sarasvati_dev VJxJYySM6XjJA7ydEB5BA
4SVrl6ABSVmCm51IrdBCqxZudiElsjtWw9MWLBP0
Hi there! We're gonna get you all set up to use MiningTheSocialWeb.
/home/dotcloud/env/lib/python2.6/site-packages/twitter-1.7.2-py2.6.egg/twitter/api.py:84: DeprecationWarning: object.
__init__() takes no parameters

In the web browser window that opens please choose to Allow
access. Copy the PIN number that appears on the next page and paste or
type it here:

Opening: http://api.twitter.com/oauth/authorize?oauth_token=53cVoKMztBC8i9tfykDLbaGEiZxwluj07EGrYeyH4


Uh, I couldn't open a browser on your computer. Please go here to get
your PIN:

http://api.twitter.com/oauth/authorize?oauth_token=53cVoKMztBC8i9tfykDLbaGEiZxwluj07EGrYeyH4




                                ↑ブラウザから入力
PINコードがとれる。
表示されたPINコードを入力した結果



↑地味だな。。

無事、フォローしている人のIDが取れる。
(上の例では7名)

※ただし再度pushしたらtwitterを入れ直し
 一度のpushでがんばるのがいいか。。
3)サンプル




         php編
Twitter API ポケットリファレンス
    著者 山本 裕介 氏




P201 サンプルコード リスト購読者の管理
http://goo.gl/Mfeho
実行手順
1)自分の環境でディレクトリとファイルを作る
[root@localhost ‾]# tree twtr-api-pocket-reference/   ←ディレクトリ
twtr-api-pocket-reference/                            ←ディレクトリ
├── dotcloud.yml                                      ←ファイル
└── twtr-api-pocket-reference                         ←ディレクトリ
     ├── manage_list_subscription.php                 ←phpファイル
     └── tmhOAuth.php                                 ←phpファイル

1 directory, 3 files
[root@localhost ‾]# cat twtr-api-pocket-reference/dotcloud.yml
web:                                 ←dotcloud.ymlという定義ファイルの中身
  type: php
  approot: twtr-api-pocket-reference

作るファイルは2ディレクトリと3ファイルだけ
実行手順

2)dotcloud コマンドでcreateしてdotcloudにアプリ
ケーションを作る

[root@localhost ‾]# dotcloud create
twtrapipocketreference
Created application "twtrapipocketreference"

※最初の1回だけ
ハイフン(-)は使えない
実行手順

3)dotcloud コマンドでpushして
[root@localhost ‾]# dotcloud push 
twtrapipocketreference /root/twtr-api-pocket-
reference
# upload /root/twtr-api-pocket-reference ssh://dotcloud@uploader.dotcloud.
com:443/twtrapipocketreference
# rsync
Pseudo-terminal will not be allocated because stdin is not a terminal.
building file list ... done
./
dotcloud.yml
twtr-api-pocket-reference/
twtr-api-pocket-reference/manage_list_subscription.php

sent 1.40K bytes received 56 bytes 265.27 bytes/sec
total size is 2.83K speedup is 1.94
05:13:12 ---> Deploy of "twtrapipocketreference" scheduled for revision rsync-
1335071591826 at 2012-04-22 05:13:12
05:13:12 ---> Building the application...
05:13:12 [web] Build started for revision rsync-1335071591826 (clean build)
05:13:19 [web] Build completed successfully. Compiled image size is 428KB
05:13:19 ---> Initializing new services... (This may take a few minutes)
〜中略〜
Deployment finished. Your application is available at the following URLs
web: http://twtrapipocketreference-3opsr7qp.dotcloud.com/
実行手順

3)dotcloud コマンドでsshログイン
コードを直す
[root@localhost]# dotcloud ssh 
twtrapipocketreference.web
# $SHELL
dotcloud@twtrapipocketreference-default-web-
0:‾$

アプリケーション名.サービス名でssh
実行手順

4)dotcloudでviを使う
dotcloud@twtrapipocketreference-default-web-
0:‾$ vi code/twtr-api-pocket-
reference/manage_list_subscription.php


macの場合ターミナルの環境設定..→設定→詳細
→言語環境でUnicode(UTF-8)にしないと文字化け
する
実行手順

5)manage_list_subscription.phpを修正

12 array("consumer_key" => "コンシューマキー",
13 "consumer_secret" => "コンシューマシークレット",
14 "user_token" => "アクセストークン",
15 "user_secret" => "アクセストークンシークレット"));


macの場合ターミナルの環境設定..→設定→詳細
→言語環境でUnicode(UTF-8)にしないと文字化け
する

dotcloudでもphpソースの変更は不要
6)修正したらブラウザから確認




http://twtrapipocketreference-3opsr7qp.
dotcloud.com/manage_list_subscription.php
http://goo.gl/re0Fd
鍵アカウントの場合なにも表示されない。
6)修正したらブラウザから確認
まとめ

twitterアプリを開発するなら


      dotcloud.com
がお手軽です。
Freeアカウントを使いましょう。
制限事項(Freeアカウントの場合)

 登録できるアプリは基本2つまで。
  とはいえサブディレクトリで工夫できる

 ディスク容量


お困りの際は@yut148まで
 ベストエフォートでお答えします。
配布用Dotcloudによるすぐ始めるtwitterwebアプリ開発#twtr hack

配布用Dotcloudによるすぐ始めるtwitterwebアプリ開発#twtr hack