2012/02/06


さっそく作ってみた。
Google Japan Blog: Google マップが携帯でも表示できるようになりました
HTTP::MobileAgent::Plugin::Locatorを使って緯度経度を取り、GoogleMapの静的画像を表示するサンプルを作ってみた。
ただGoogleMapの座標指定は測地系だったので
http://minken.net/mt/archives/locations.pl の一部を使わせて頂きました。
Perlで投影系変換出来るモジュール誰か知りませんか?

ソースは以下の様な感じ。
#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use HTTP::MobileAgent;
use HTTP::MobileAgent::Plugin::Locator;


my $google_map_api_key = "ABQIAAAAS_2fKEdj-fsDOrnYqd4nthTGTkG1t9CC6WQns4yK382vvQcY9RS5JGW4WA0hwZKxfIpKeCjuMOMPIA";

sub geoconv {
    ...
}

sub llh2xyz { # 楕円体座標 -> 直交座標
    ...
}

sub xyz2llh { # 直交座標 -> 楕円体座標
    ...
}

sub deg2dms {
    ...
}

sub dms2deg {
    ...
}

my $q = CGI->new;
my $agent;
my $lat = '';
my $lng = '';
my $location = '';
eval {
  $agent = HTTP::MobileAgent->new;
  $location = $agent->get_location( $q );
  ($lng, $lat) = geoconv($location->lng, $location->lat, $location->datum)
};

print $q->header(-charset=>'Shift_JIS'),<<END;
<html>
<head>
<title>GoogleMap from GPS</title>
</head>
<body>
<h1>What is this?</h1>
Show the Google Map static image from your GPS location.<br />
END

if ($agent && $agent->is_non_mobile) {
  print "<b>Are not mobile agent?</b>";
} else {
  print "<a href=\"device:location?url=gmobile_map.cgi\" lcs>Where?</a>"
    if ($agent->is_docomo);
  print "<a href=\"device:location?url=gmobile_map.cgi\">Where?</a>"
    if ($agent->is_ezweb);
  print "<a href=\"gmobile_map.cgi\" z>Where?</a>"
    if ($agent->is_softbank && !$agent->is_type_3gc);
  print "<a href=\"location:auto?url=gmobile_map.cgi\">Where?</a>"
    if ($agent->is_softbank && $agent->is_type_3gc);
  if ($lat && $lng) {
    print << "END";
<img src="http://maps.google.com/staticmap?center=$lat,$lng&markers=$lat,$lng,red&zoom=18&size=300x200&key=$google_map_api_key"><br />
latitude: $lat<br />
longitude $lng<br />
END
  }
}

print "</body>\n</html>";
若干ずれるのはしょうがないか...
で、動いてる物は
http://mattn.kaoriya.net/cgi-bin/gmobile_map.cgi
携帯端末からアクセス下さい。
キャリア毎のアンカー作ってくれるモジュールも欲しいなぁ...

AWSWORD:google_map
Posted at by



2011/07/20


なんだか楽しそう。
Amon2::Liteでmarkdownその他のリアルタイムプレビュー - すぎゃーんメモ

Amon2::Liteでmarkdownその他のリアルタイムプレビュー Perl Amon2::Lite というモジュールを Amon2 に添付してみました。 - TokuLog 改メ tokuhir...

http://d.hatena.ne.jp/sugyan/20110720/1311146296
こういうの皆で共有したいよねと思ったので最近話題のPaaS、fluxflexにデプロイしてみた。
fluxflex

Easy One-Click Install for Web Applications You can install various OSS in a second just with one-cl...

http://www.fluxflex.com
http://text-converter.fluxflex.com
ちょっとハマった点が、fluxflexにはcpanm等による自動インストール機能が無い事。自前でextlibみたいなのに放り込めばいいんだけど、これがなかなか面倒くさい。適当なcgiを書いてどのモジュールの読み込みで失敗しているか調べた結果でいろいろ添付してます。
正直、要らない物もあがってるかもしれません。
とりあえず動いたので、負荷かけない程度に遊んで下さい。

一応CGIのコード貼っておきます。
#!/usr/bin/perl
use strict;
use warnings;

use lib qw( ../lib );
use Amon2::Lite;
use Encode 'encode_utf8';
use Text::Markdown 'markdown';
use Text::Xatena;
use Pod::Simple::XHTML;
use Plack::Handler::CGI;

$ENV{REQUEST_METHOD} ||= 'GET';
$ENV{SCRIPT_NAME} ||= $0;
$ENV{PATH_INFO} ||= '/';

my $converters = {
    markdown => sub {
        my $text = shift;
        return markdown($text);
    },
    xatena => sub {
        my $text = shift;
        return Text::Xatena->new->format($text);
    },
    pod => sub {
        my $text = shift;
        my $parser = Pod::Simple::XHTML->new;
        $parser->html_header('');
        $parser->html_footer('');
        $parser->output_string(\my $html);
        $parser->parse_string_document($text);
        return $html;
    },
};

get '/' => sub {
    my ($c) = @_;
    return $c->render('index.tt');
};

post '/preview' => sub {
    my ($c) = @_;
    my $converter = $converters->{$c->req->param('format')};
    my $html = $converter ? $converter->($c->req->param('text')) : '';
    return $c->create_response(200, ['Content-Type' => 'text/plain'], [encode_utf8($html)]);
};

Plack::Handler::CGI->new->run(__PACKAGE__->to_app);
あと、頭の方でやってるENVの初期値設定は、これが無いとエラーで動かなかった為。この辺は後でフィードバックしておきます。
dotcloudの時もそうでしたが、__DATA_トークンにあったテンプレートはtmpl/index.ttに配置して動作させています。

おまけで、Text::Xatenaの出力を少し変えて(codeというclass属性をprettyprintに変更)、google code prettifyによる色付け機能を足してあります。
#ちょさん!変更出来る様にして下さい!

SuprePre記法で遊んで下さい!
Posted at by



2011/07/19


なんか呼ばれたけど、勉強しとかなくちゃ答えるにも答えれないだろうなと思ったので、ちょっとくらいは勉強しておこうとAmon2でgyazoを書いてdotcloudにpushしてみた。
まず # amon2-setup.pl --flavor=Lite Gyazo
として雛形を作る。
POSTハンドラを書く。
post '/' => sub {
    my $c = shift;
    my $imagedata = $c->req->param('imagedata');
    $imagedata = read_file($c->req->uploads->{imagedata}->pathbinmode => ':raw'unless $imagedata;
    my $filename = "image/" . md5_hex($imagedata) . ".png";
    write_file($filename, {binmode => ':raw'}, $imagedata);
    my $url = $c->req->base() . $filename;
    return $c->create_response(200, ['Content-Type' => 'text/plain'], [$url]);
};
セッション使わないのでプラグイン読み込み処理をカットして、Plack::Middleware::Staticでimageディレクトリを見える様にした。あとdotcloudで動かす為に__DATA__トークンに書かれているindex.ttをtmpl/index.ttに移した。
app.psgiの全体コードはこんな感じ。
use strict;
use warnings;
use File::Spec;
use File::Basename;
use File::Slurp;
use Digest::MD5 qw( md5_hex );
use lib File::Spec->catdir(dirname(__FILE__), 'extlib''lib''perl5');
use lib File::Spec->catdir(dirname(__FILE__), 'lib');
use Plack::Builder;
use Amon2::Lite;

# put your configuration here
sub config {
    +{
    }
}

get '/' => sub {
    my $c = shift;
    return $c->render('index.tt');
};

post '/' => sub {
    my $c = shift;
    my $imagedata = $c->req->param('imagedata');
    $imagedata = read_file($c->req->uploads->{imagedata}->pathbinmode => ':raw'unless $imagedata;
    my $filename = "image/" . md5_hex($imagedata) . ".png";
    write_file($filename, {binmode => ':raw'}, $imagedata);
    my $url = $c->req->base() . $filename;
    return $c->create_response(200, ['Content-Type' => 'text/plain'], [$url]);
};

# for your security
__PACKAGE__->add_trigger(
    AFTER_DISPATCH => sub {
        my ( $c$res ) = @_;
        $res->header'X-Content-Type-Options' => 'nosniff' );
    },
);

builder {
    enable 'Plack::Middleware::Static',
        path => qr{^(?:/static/|/robot\.txt$|/favicon.ico$|/image/)},
        root => File::Spec->catdir(dirname(__FILE__));
    enable 'Plack::Middleware::ReverseProxy';

    __PACKAGE__->to_app();
};
最後にdotcloud.ymlに www:
    type: perl
    requirements:
        File::Slurp
        Digest::MD5
        Amon2
を書いてgitでcommitした後に # dotcloud push mattn すれば、あとはdotcloudが自動で依存物をワンサカワンサカ入れてくれて、動くようになる。(mattnというのはdotcloud createで作った際のapplication。上記wwwはservice)
できあがったサーバはこれ

なんか知らない間にhttp://gyazo.mattn.dotcloud.comみたいなURLで公開出来なくなっちゃったので、ひとまず我慢します。

最後に一言

それAmon2じゃなくてもおk
Posted at by