Debian Codegraph @

lib/WWW/Mechanize/AutoPager.pm

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package WWW::Mechanize::AutoPager;
2
3 use strict;
4 use 5.8.1;
5 our $VERSION = '0.02';
6
7 use HTML::AutoPagerize;
8 use Scalar::Util qw( weaken );
9 use WWW::Mechanize::DecodedContent;
10 use JSON;
11
12 sub WWW::Mechanize::autopager {
13 my $mech = shift;
14 $mech->{autopager} ||= WWW::Mechanize::AutoPager->new($mech);
15 }
16
17 sub WWW::Mechanize::next_link {
18 my $mech = shift;
19 $mech->{autopager}->next_link;
20 }
21
22 sub WWW::Mechanize::page_element {
23 my $mech = shift;
24 $mech->{autopager}->page_element;
25 }
26
27 sub new {
28 my($class, $mech) = @_;
29 my $self = bless {
30 mech => $mech,
31 autopager => HTML::AutoPagerize->new,
32 }, $class;
33
34 weaken($self->{mech}); # don't make it a circular reference
35 $self;
36 }
37
38 sub load_siteinfo {
39 my $self = shift;
40 my $url = shift || "http://wedata.net/databases/AutoPagerize/items.json";
41
42 my $res = $self->{mech}->get($url);
43
44 if (my $content = $self->{mech}->content) {
45 if ($res->content_type =~ m{text/html}) { # backward compatibility
46 while ($content =~ m!<textarea class="autopagerize_data".*?>\s*(.*?)\s*</textarea>!gs) {
47 my $site = $self->parse_siteinfo($1);
48 $self->{autopager}->add_site(%$site);
49 }
50 } else {
51 for my $row ( @{ from_json( $content ) } ) {
52 $self->{autopager}->add_site(%{ $row->{data} });
53 }
54 }
55 }
56 }
57
58 sub add_site {
59 my $self = shift;
60 $self->{autopager}->add_site(@_);
61 }
62
63 sub parse_siteinfo {
64 my($self, $config) = @_;
65 my $site;
66 while ($config =~ /^(\w+):\s+(.*?)\s*$/mg) {
67 $site->{$1} = $2;
68 }
69 return $site;
70 }
71
72 sub next_link {
73 my $self = shift;
74
75 my $res = $self->{autopager}->handle($self->{mech}->uri, $self->{mech}->decoded_content)
76 or return;
77
78 return $res->{next_link};
79 }
80
81 sub page_element {
82 my $self = shift;
83
84 my $res = $self->{autopager}->handle($self->{mech}->uri, $self->{mech}->decoded_content)
85 or return;
86
87 return $res->{page_element};
88 }
89
90
91 1;
92 __END__
93
94 =for stopwords AutoPagerize siteinfo
95
96 =head1 NAME
97
98 WWW::Mechanize::AutoPager - Automatic Pagination using AutoPagerize
99
100 =head1 SYNOPSIS
101
102 use WWW::Mechanize::AutoPager;
103
104 my $mech = WWW::Mechanize->new;
105
106 # Load siteinfo from http://swdyh.infogami.com/autopagerize
107 $mech->autopager->load_siteinfo();
108
109 # Or, load manually
110 $mech->autopager->add_site(
111 url => 'http://.+\.tumblr\.com/',
112 nextLink => ...,
113 );
114
115 $mech->get('http://otsune.tumblr.com/');
116
117 if (my $link = $mech->next_link) {
118 $mech->get($link);
119 $mech->page_element; # HTML snippet
120 }
121
122 =head1 DESCRIPTION
123
124 WWW::Mechanize::AutoPager is a plugin for WWW::Mechanize to do
125 automatic pagination using AutoPagerize user script.
126
127 B<THIS MODULE IS CONSIDERED EXPERIMENTAL AND ITS API WILL BE LIKELY TO CHANGE>
128
129 =head1 AUTHOR
130
131 Tatsuhiko Miyagawa E<lt>[email protected]<gt>
132
133 =head1 LICENSE
134
135 This library is free software; you can redistribute it and/or modify
136 it under the same terms as Perl itself.
137
138 =head1 SEE ALSO
139
140 L<HTML::AutoPagerize>, L<http://swdyh.infogami.com/autopagerize>
141
142 =cut