2015/06/06

Perl で短縮URLを展開する

ぱっと調べた感じだと WWW::Expand というモジュールがあったのだけど、LWP::UserAgent を HTTP クライアントに使っていて、Furl 欲しさを感じたので WWW::Expand::More というモジュールを書いた。

WWW::Expand::More

短縮URL を渡すと、展開する。

use WWW::Expand::More;

my $expanded_url  = WWW::Expand::More->expand('http://t.co/mxuRqUuDmX');
print $expanded_url; # http://bayashi.net/diary/2015/0606

timeout できる。

print WWW::Expand::More->expand('http://t.co/mxuRqUuDmX' => {
    timeout => 1,
});

展開結果をキャッシュして2度目は HTTPリクエストしない。

print WWW::Expand::More->expand('http://t.co/mxuRqUuDmX' => {
    cache => 1, # cache on memory
});

expand_url

expand_url という CLI コマンドが同梱されてるのでターミナルで展開とかできる。

$ expand_url http://bit.ly/1BPj30x
https://www.google.com/search?q=Perl

短縮URLのリダイレクトが複数ある場合に全部出すとかできる。

$ expand_url http://bit.ly/1BPj30x --all
http://bit.ly/1BPj30x
https://goo.gl/dXwzqw
https://www.google.com/search?q=Perl

詳細は WWW::Expand::More をどうぞ。

うぇーい

サイト内検索