Win32::UrlCache::Title - looks for an html cache and extract its title


Win32-UrlCache documentation Contained in the Win32-UrlCache distribution.

Index


Code Index:

NAME

Top

Win32::UrlCache::Title - looks for an html cache and extract its title

SYNOPSIS

Top

  use Win32::UrlCache::Title;
  Win32::UrlCache::Title->extract( $cache->filename );

DESCRIPTION

Top

This is used internally to look for an html cache and extract its title (this is for Win32 only).

METHOD

Top

extract

receives a temporary file name, walks down in the cache diretories to find a cache of the name, tries to extract its title, and returns the (hopefully) properly-encoded title.

AUTHOR

Top

Kenichi Ishigaki, <ishigaki at cpan.org>

COPYRIGHT AND LICENSE

Top


Win32-UrlCache documentation Contained in the Win32-UrlCache distribution.

package Win32::UrlCache::Title;

use strict;
use warnings;
use Carp;
use Win32::TieRegistry ( Delimiter => '/' );
use File::Spec;
use Encode;

my @CacheDirs;

sub import {
  my $class = shift;

  my $cachedir = $Registry->{'CUser/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders//Cache'};
  my $ie5_dir  = File::Spec->catdir( $cachedir, 'Content.IE5' );
     $cachedir = $ie5_dir if -d $ie5_dir;

  opendir my $dh, $cachedir or croak $!;
  while( my $entry = readdir $dh ) {
    next if $entry =~ /^\./;
    next if -f $entry;
    push @CacheDirs, File::Spec->catdir( $cachedir, $entry );
  }
}

sub extract {
  my ($class, $file) = @_;

  foreach my $dir ( @CacheDirs ) {
    opendir( my $dh, $dir );
    while( my $entry = readdir $dh ) {
      if ( $entry eq $file ) {
        return _extract( File::Spec->catfile( $dir, $entry ) );
      }
    }
    closedir $dh;
  }
  return;
}

sub _extract {
  my $file = shift;

  open my $fh, '<', $file or return;
  binmode $fh;
  read $fh, my $chunk, 1024;
  close $fh;

  return unless $chunk;

  my ($title) = $chunk =~ m{<title>(.+?)</title>}is;
  return unless $title;

  my ($charset) = $chunk =~ m{<meta[^>]+?;\s*charset=['"]?(['">]+?)}is;
  $charset ||= 'utf8';

  eval { $title = Encode::decode( $charset => $title ) };

  return $title;
}

1;

__END__