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

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

Upload: yut148atgmaildotcom

Post on 07-Jul-2015

1.008 views

Category:

Documents


2 download

TRANSCRIPT

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

dotcloudによるすぐ始める

twitter webアプリ開発

Twitter API勉強会 2012年4月24日python-CLI編 PHP編 #twtr_hack

@yut148

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

自己紹介

@yut148

職業 クラウドを大量に使っているゲーム会社 or

エンターテイメント会社に勤めています。

最近所属変更があって担当職務がよくわかりませんが、前職含め、ゲーム会社にてサーバ管理の仕事が多い。

写経家 要はコピペPG程度(←↑今日はこれ大事)会社のソースはPHPが多い

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

本日のお題

1)twitterの webアプリケーション開発上の  問題2)dotcloudについて3)サンプル

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

皆様、twitterのwebアプリ開発されてますか?

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

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

皆様、twitterのwebアプリ開発されてますか?

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

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

これらに共通の問題

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

皆様、twitterの開発されてますか?

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

ところで、サーバどうしてますか? ・リアルなサーバで開発を行うデメリット →電気代、ハードウェア故障、熱・VPS、レンタルサーバ、クラウドで開発を行うデメリット →利用用金、クレジットカード

要は結局の所結論:めんどくさい

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

2)dotcloudについて

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

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

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

2)dotcloudについて

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

そこで、PaaSのdotcloudなら、、↓これらの作業は不要です。 しかもFreeアカウントならクレジットカード

等不要

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

         dotcloud.com

dotcloudなら、

とりあえずドメインがふられる。 ∴ アプリケーション登録ができる。

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

dotcloud Sign UpSign Up(アカウント作成)に必要なもの

・python環境 ver2.6が必要 windowsでもMacでもLinuxでもOK  けれど、virtualBOXとかでLinuxを動かした ほうがおすすめ ・メールアドレス gmailとかでok

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

dotcloud.com サインはこちらから

※サービス名+アカウント名のドメインになるの

で、アカウント登録の時に注意

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

開発環境の下準備

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

詳しくは   →http://goo.gl/cCYVx

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

python編コマンドラインでの利用

3)サンプル

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

入門ソーシャルデータ 著者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

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

しかしtwitterのアプリケーション登録ってドメイン必要ですよね。。

ここ

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

でdotcloudなら、、

http://friendsfollowersgetfriens-3opsr7qp.dotcloud.com/サブドメインが設定されます。 アプリケーション名ーユーザ名.dotcloud.comこれでアプリケーション登録できます。 ※実際は設定されたドメインを調べる必要があります。

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

1)自分の環境でディレクトリとファイルを作る[root@localhost ‾]# ls -laR friends_followers__get_friends/friends_followers__get_friends/:total 16drwxr-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.ymldrwxr-xr-x 2 root root 4096 Apr 14 16:14 friends_followers__get_friends friends_followers__get_friends/friends_followers__get_friends:total 12drwxr-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.ymlweb: type: python approot: friends_followers__get_friends

開発手順

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

開発手順

2)dotcloud コマンドでpushしてdotcloud push friendsfollowersgetfriends friends_followers__get_friends3)sshでログイン

dotcloud ssh friendsfollowersgetfriends.webtwitterだけはインストール# $SHELLdotcloud@friendsfollowersgetfriends-default-web-0:‾$ easy_install twitter

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

実行

dotcloud@friendsfollowersgetfriends-default-web-0:‾/code/friends_followers__get_friends$ python friends_followers__get_friends.py sarasvati_dev VJxJYySM6XjJA7ydEB5BA 4SVrl6ABSVmCm51IrdBCqxZudiElsjtWw9MWLBP0Hi 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 Allowaccess. Copy the PIN number that appears on the next page and paste ortype 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 getyour PIN: http://api.twitter.com/oauth/authorize?oauth_token=53cVoKMztBC8i9tfykDLbaGEiZxwluj07EGrYeyH4

↑ブラウザから入力

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

PINコードがとれる。

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

表示されたPINコードを入力した結果

↑地味だな。。

無事、フォローしている人のIDが取れる。(上の例では7名) ※ただし再度pushしたらtwitterを入れ直し 一度のpushでがんばるのがいいか。。

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

php編

3)サンプル

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

Twitter API ポケットリファレンス

 著者 山本 裕介 氏

P201 サンプルコード リスト購読者の管理

http://goo.gl/Mfeho

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

実行手順

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.ymlweb: ←dotcloud.ymlという定義ファイルの中身

type: php approot: twtr-api-pocket-reference 作るファイルは2ディレクトリと3ファイルだけ

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

実行手順

2)dotcloud コマンドでcreateしてdotcloudにアプリケーションを作る [root@localhost ‾]# dotcloud create twtrapipocketreferenceCreated application "twtrapipocketreference" ※最初の1回だけハイフン(-)は使えない

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

実行手順

3)dotcloud コマンドでpushして[root@localhost ‾]# dotcloud push \ twtrapipocketreference /root/twtr-api-pocket-reference

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

# upload /root/twtr-api-pocket-reference ssh://[email protected]:443/twtrapipocketreference# rsyncPseudo-terminal will not be allocated because stdin is not a terminal.building file list ... done./dotcloud.ymltwtr-api-pocket-reference/twtr-api-pocket-reference/manage_list_subscription.php sent 1.40K bytes received 56 bytes 265.27 bytes/sectotal size is 2.83K speedup is 1.9405:13:12 ---> Deploy of "twtrapipocketreference" scheduled for revision rsync-1335071591826 at 2012-04-22 05:13:1205: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 428KB05:13:19 ---> Initializing new services... (This may take a few minutes)〜中略〜Deployment finished. Your application is available at the following URLsweb: http://twtrapipocketreference-3opsr7qp.dotcloud.com/

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

実行手順

3)dotcloud コマンドでsshログインコードを直す[root@localhost]# dotcloud ssh \ twtrapipocketreference.web# $SHELLdotcloud@twtrapipocketreference-default-web-0:‾$ アプリケーション名.サービス名でssh

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

実行手順

4)dotcloudでviを使うdotcloud@twtrapipocketreference-default-web-0:‾$ vi code/twtr-api-pocket-reference/manage_list_subscription.php macの場合ターミナルの環境設定..→設定→詳細→言語環境でUnicode(UTF-8)にしないと文字化けする

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

実行手順

5)manage_list_subscription.phpを修正

12 array("consumer_key" => "コンシューマキー",

13 "consumer_secret" => "コンシューマシークレット",

14 "user_token" => "アクセストークン",

15 "user_secret" => "アクセストークンシークレット"));

macの場合ターミナルの環境設定..→設定→詳細→言語環境でUnicode(UTF-8)にしないと文字化けする dotcloudでもphpソースの変更は不要

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

6)修正したらブラウザから確認

鍵アカウントの場合なにも表示されない。

http://twtrapipocketreference-3opsr7qp.dotcloud.com/manage_list_subscription.php

http://goo.gl/re0Fd

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

6)修正したらブラウザから確認

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

まとめ

twitterアプリを開発するなら

dotcloud.com がお手軽です。

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

Freeアカウントを使いましょう。

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

制限事項(Freeアカウントの場合)

 登録できるアプリは基本2つまで。  とはいえサブディレクトリで工夫できる  ディスク容量 お困りの際は@yut148まで ベストエフォートでお答えします。

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