| 1 |
package Plagger::Util; |
|---|
| 2 |
use strict; |
|---|
| 3 |
our @ISA = qw(Exporter); |
|---|
| 4 |
our @EXPORT_OK = qw( strip_html dumbnail decode_content extract_title load_uri mime_type_of ); |
|---|
| 5 |
|
|---|
| 6 |
use Encode (); |
|---|
| 7 |
use List::Util qw(min); |
|---|
| 8 |
use HTML::Entities; |
|---|
| 9 |
use MIME::Types; |
|---|
| 10 |
use MIME::Type; |
|---|
| 11 |
|
|---|
| 12 |
our $Detector; |
|---|
| 13 |
|
|---|
| 14 |
BEGIN { |
|---|
| 15 |
if ( eval { require Encode::Detect::Detector; 1 } ) { |
|---|
| 16 |
$Detector = sub { Encode::Detect::Detector::detect($_[0]) }; |
|---|
| 17 |
} else { |
|---|
| 18 |
require Encode::Guess; |
|---|
| 19 |
$Detector = sub { |
|---|
| 20 |
my @guess = qw(utf-8 euc-jp shift_jis); |
|---|
| 21 |
eval { Encode::Guess::guess_encoding($_[0], @guess)->name }; |
|---|
| 22 |
}; |
|---|
| 23 |
} |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
sub strip_html { |
|---|
| 29 |
my $html = shift; |
|---|
| 30 |
$html =~ s/<[^>]*>//g; |
|---|
| 31 |
HTML::Entities::decode($html); |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
sub dumbnail { |
|---|
| 35 |
my($img, $p) = @_; |
|---|
| 36 |
|
|---|
| 37 |
if (!$img->{width} && !$img->{height}) { |
|---|
| 38 |
return ''; |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
if ($img->{width} <= $p->{width} && $img->{height} <= $p->{height}) { |
|---|
| 42 |
return qq(width="$img->{width}" height="$img->{height}"); |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
my $ratio_w = $p->{width} / $img->{width}; |
|---|
| 46 |
my $ratio_h = $p->{height} / $img->{height}; |
|---|
| 47 |
my $ratio = min($ratio_w, $ratio_h); |
|---|
| 48 |
|
|---|
| 49 |
sprintf qq(width="%d" height="%d"), ($img->{width} * $ratio), ($img->{height} * $ratio); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
sub decode_content { |
|---|
| 53 |
my $stuff = shift; |
|---|
| 54 |
|
|---|
| 55 |
my $content; |
|---|
| 56 |
my $res; |
|---|
| 57 |
if (ref($stuff) && ref($stuff) eq 'URI::Fetch::Response') { |
|---|
| 58 |
$res = $stuff; |
|---|
| 59 |
$content = $res->content; |
|---|
| 60 |
} elsif (ref($stuff)) { |
|---|
| 61 |
Plagger->context->error("Don't know how to decode " . ref($stuff)); |
|---|
| 62 |
} else { |
|---|
| 63 |
$content = $stuff; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
my $charset; |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
if ($res) { |
|---|
| 70 |
$charset = ($res->content_type =~ /charset=([\w\-]+)/)[0]; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
$charset ||= ( $content =~ /<\?xml version="1.0" encoding="([\w\-]+)"\?>/ )[0]; |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
$charset ||= ( $content =~ m!<meta http-equiv="Content-Type" content=".*charset=([\w\-]+)"!i )[0]; |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
$charset ||= $Detector->($content); |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
$charset ||= 'utf-8'; |
|---|
| 84 |
|
|---|
| 85 |
my $decoded = eval { Encode::decode($charset, $content) }; |
|---|
| 86 |
|
|---|
| 87 |
if ($@ && $@ =~ /Unknown encoding/) { |
|---|
| 88 |
Plagger->context->log(warn => $@); |
|---|
| 89 |
$charset = $Detector->($content) || 'utf-8'; |
|---|
| 90 |
$decoded = Encode::decode($charset, $content); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
$decoded; |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
sub extract_title { |
|---|
| 97 |
my $content = shift; |
|---|
| 98 |
my $title = ($content =~ m!<title>\s*(.*?)\s*</title>!is)[0] or return; |
|---|
| 99 |
HTML::Entities::decode($1); |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
sub load_uri { |
|---|
| 103 |
my($uri, $plugin) = @_; |
|---|
| 104 |
|
|---|
| 105 |
require Plagger::UserAgent; |
|---|
| 106 |
|
|---|
| 107 |
my $data; |
|---|
| 108 |
if (ref($uri) eq 'SCALAR') { |
|---|
| 109 |
$data = $$uri; |
|---|
| 110 |
} |
|---|
| 111 |
elsif ($uri->scheme =~ /^https?$/) { |
|---|
| 112 |
Plagger->context->log(debug => "Fetch remote file from $uri"); |
|---|
| 113 |
|
|---|
| 114 |
my $response = Plagger::UserAgent->new->fetch($uri, $plugin); |
|---|
| 115 |
if ($response->is_error) { |
|---|
| 116 |
Plagger->context->log(error => "GET $uri failed: " . |
|---|
| 117 |
$response->http_status . " " . |
|---|
| 118 |
$response->http_response->message); |
|---|
| 119 |
} |
|---|
| 120 |
$data = decode_content($response); |
|---|
| 121 |
} |
|---|
| 122 |
elsif ($uri->scheme eq 'file') { |
|---|
| 123 |
Plagger->context->log(debug => "Open local file " . $uri->file); |
|---|
| 124 |
open my $fh, '<', $uri->file |
|---|
| 125 |
or Plagger->context->error( $uri->file . ": $!" ); |
|---|
| 126 |
$data = decode_content(join '', <$fh>); |
|---|
| 127 |
} |
|---|
| 128 |
else { |
|---|
| 129 |
Plagger->context->error("Unsupported URI scheme: " . $uri->scheme); |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
return $data; |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
our $mimetypes = MIME::Types->new; |
|---|
| 136 |
$mimetypes->addType( MIME::Type->new(type => 'video/x-flv', extensions => [ 'flv' ]) ); |
|---|
| 137 |
$mimetypes->addType( MIME::Type->new(type => 'audio/aac', extensions => [ 'm4a', '.aac' ]) ); |
|---|
| 138 |
|
|---|
| 139 |
sub mime_type_of { |
|---|
| 140 |
my $ext = shift; |
|---|
| 141 |
|
|---|
| 142 |
if (UNIVERSAL::isa($ext, 'URI')) { |
|---|
| 143 |
$ext = ( $ext->path =~ /\.(\w+)/ )[0]; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
return unless $ext; |
|---|
| 147 |
return $mimetypes->mimeTypeOf($ext); |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
1; |
|---|