mojo::useragentかわいい (@ yapc::asia2013 前夜祭 lt-thon 2013-09-19)
TRANSCRIPT
![Page 1: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/1.jpg)
Mojo::UserAgentかわいい
~ use Mojolicious as HTTP client ~
2013-09-19 @ LT-thon in YAPC::Asia 2013
turugina
![Page 2: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/2.jpg)
皆さん、
![Page 3: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/3.jpg)
Mojolicious好きですか?
![Page 4: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/4.jpg)
ですよね
![Page 5: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/5.jpg)
Mojolicious萌え♡
![Page 6: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/6.jpg)
Mojolicious萌え♡
![Page 7: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/7.jpg)
みんな大好きMojolicious
● http://mojolicio.us/● 現在のバージョン: 4.39 (2013-09-16)
![Page 8: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/8.jpg)
Mojolicious has...
● Web Server (HTTP, HTTPS, WebSocket)○ Mojo::Server::*
● CLI Framework○ Mojolicious::Command::*○ test, cpanify, print routes, run as CGI,○ you can write your own commands
● UserAgent (HTTP/WebSocket client)○ Mojo::UserAgent, Mojo::Transaction::*○ Mojo::Cookie::○ DOM/Selectors
● ojo module (for one-liner)○ perl -Mojo -E ‘b(g(“mojolicio.us”)->body)-
>html_unescape->say’
![Page 9: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/9.jpg)
As HTTP UserAgent
Mojo::UserAgent
Mojo::UserAgent::Transactor
Mojo::Transaction::HTTP
Mojo::Message::Request
Mojo::Message::Response
use
![Page 10: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/10.jpg)
Basic Usage
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;my $tx = $ua->get(‘URL’ => Header =>BODY);
if ( my $res = $tx->success) {# $res をつかってごにょごにょ
} else {my ($msg, $code) = $tx->error;die “$msg: $code”;
}
![Page 11: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/11.jpg)
Get
my $tx = $ua->post(‘http://www.example.com/login’, form => { username => ‘scott’, password => ‘tiger’ });
if ( my $res = $tx->success ) {for my $cookie (map {$_->res->cookies} @{$tx-
>redirects}) {if ( $cookie->name eq ‘PHPSESSID’ ) {
# ごにょごにょ
}}
}
![Page 12: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/12.jpg)
Put
my $cookie = Mojo::Cookie::Response->new;$cookie->name(‘PHPSESSID’);$cookie->value($phpsessid);$cookie->path(‘/’);
$ua->cookie_jar->add($cookie);
![Page 13: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/13.jpg)
Scraping
my $coll = $res->dom(‘section#illust_list ul li a.work’);# $coll … Mojo::Collection
$coll->each(sub {my ($e, $count) = @_;my $title = $e->find(‘h1’)->first->text;my $img_url = $e->find(‘img’)->first->attr(‘src’);
# ごにょごにょ
});
![Page 14: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/14.jpg)
JSON
my $tx = $ua->post(‘...’, json => {loren => ‘ipsum’});
# assuming json in response:# { “result”: “ok”, “body”: {“title”: “hogehoge”} }if (my $res = $tx->success) {
if ( $res->json(‘/result’) eq “ok” ) {say $res->json(‘/body/title’);
}}
![Page 15: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/15.jpg)
ByteStream
use Mojo::ByteStream;
my $tx = $ua->get(‘http://.../foo.jpg’);
if ( my $res = $tx->success ) {my $bs = Mojo::ByteStream->new($res->body);$bs->spurt(‘/tmp/foo.jpg’);say “MD5: “, $bs->md5_sum;
}
![Page 16: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/16.jpg)
普通に使う分には
超優 秀
![Page 17: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/17.jpg)
Mojo::UserAgent
Mojo::Server
優秀な子たちですから
![Page 18: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/18.jpg)
Easy to mashup
Mojo::UserAgent
Mojo::Server
the Internet
Data
myapp
Cache/DB
HTML, js,...
Service
![Page 19: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/19.jpg)
と、いうのを
2日くらいで作ってみた
けどデータ元がpixivで自分のアカウントでログインしてるから公開はできない
2013-09-23追記: pixivのアカウント情報はバージョン管理に入ってないことを確認したのでコード晒してみます:
https://code.launchpad.net/~turugina/+junk/illust_sns_viewer但し、このコードを使ってpixivさんに迷惑はかけないように!
![Page 20: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/20.jpg)
概要
Mojo::UserAgent
Mojo::Server
pixiv
検索結果画像
myapp
DBMFile Cache
jQuery. Gallarie
「艦これかわいい」
![Page 21: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/21.jpg)
艦これ KAWAII!
http://www.pixiv.net/member_illust.php?mode=medium&illust_id=38587771
![Page 22: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/22.jpg)
Demo
![Page 23: Mojo::UserAgentかわいい (@ YAPC::Asia2013 前夜祭 LT-thon 2013-09-19)](https://reader033.vdocuments.mx/reader033/viewer/2022042715/55a0be751a28ab6f5e8b473d/html5/thumbnails/23.jpg)
ご清聴ありがとうございました