root/WWW-Cache-Google/trunk/lib/WWW/Cache/Google.pm

Revision 554 (checked in by miyagawa, 18 years ago)

0.03

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package WWW::Cache::Google;
2
3 use strict;
4 use vars qw($VERSION);
5 $VERSION = 0.03;
6
7 use URI;
8 use URI::Escape;
9
10 sub cache_base {
11     return 'http://www.google.com/search?q=cache:%s';
12 }
13
14
15 # ro-accessor
16 sub orig   { $_[0]->[0] }
17 sub cache  { $_[0]->[1] }
18
19 sub new {
20     my($class, $thingy) = @_;
21     my $uri = _make_uri($thingy);
22     my $self = bless [ $uri ], $class;
23     $self->init;
24     return $self;
25 }
26
27 sub _make_uri {
28     my $thingy = shift;
29     return $thingy if UNIVERSAL::isa($thingy => 'URL');
30     return URI->new($thingy);
31 }
32
33 sub init {
34     my $self = shift;
35     my $uri = sprintf $self->cache_base, $self->_cache_param;
36     $self->[1] = URI->new($uri);
37 }
38
39 sub _cache_param {
40     my $self = shift;
41     return $self->orig->host . uri_escape($self->orig->path_query, q(\W));
42 }
43
44 sub fetch {
45     require LWP::Simple;
46     my $self = shift;
47     return LWP::Simple::get($self->cache->as_string);
48 }
49
50 sub DESTROY { }
51
52 use vars qw($AUTOLOAD);
53 sub AUTOLOAD {
54     my $self = shift;
55     (my $meth = $AUTOLOAD) =~ s/.*://;
56     $self->cache->$meth(@_);
57 }
58
59
60 1;
61 __END__
62
63
64 =head1 NAME
65
66 WWW::Cache::Google - URI class for Google cache
67
68 =head1 SYNOPSIS
69
70   use WWW::Cache::Google;
71
72   $cache = WWW::Cache::Google->new('http://www.yahoo.com/');
73
74   $url  = $cache->as_string;    # cache URL
75   $html = $cache->fetch;        # fetches via LWP::Simple
76
77 =head1 DESCRIPTION
78
79 Oops, 404 Not Found. But wait ... there might be a google cache!
80
81 WWW::Cache::Google provides an easy way conversion from an URL to
82 Google cache URL.
83
84 =head1 METHODS
85
86 =over 4
87
88 =item $cache = WWW::Cache::Google->new($url);
89
90 constructs WWW::Cache::Google instance.
91
92 =item $orig_uri = $cache->orig;
93
94 returns original URL as URI instance.
95
96 =item $cache_uri = $cache->cache;
97
98 returns Google cache URL as URI instance.
99
100 =item $html = $cache->fetch;
101
102 gets HTML contents of Google cache. Requires LWP::Simple.
103
104 =item $url_str = $cache->as_string;
105
106 returns Google cache's URL as string. Every method defined in URI
107 class is autoloaded through $cache->cache. See L<URI> for details.
108
109 =back
110
111 =head1 AUTHOR
112
113 Tatsuhiko Miyagawa <miyagawa@bulknews.net>
114
115 This library is free software; you can redistribute it and/or modify
116 it under the same terms as Perl itself.
117
118 It comes WITHOUT WARRANTY OF ANY KIND.
119
120 =head1 SEE ALSO
121
122 L<WWW::Cache::Google::Imode>, L<URI>, L<URI::Escape>, L<LWP::Simple>, http;//www.google.com/
123
124 =cut
Note: See TracBrowser for help on using the browser.