| 1 | package Tie::DuckDuckGo; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use 5.008_005; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | our $VERSION = '0.02'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 1 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use HTML::TreeBuilder::XPath; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use HTTP::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | # when tying, call the constructor with data type name | ||||||||||||||
| 16 | 1 | ▶ | sub TIEARRAY { shift->new('array', @_) } | ||||||||||||
|
Subroutine
TIEARRAY: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 1 | ▶ | sub TIEHASH { shift->new('hash', @_) } | ||||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 1 | ▶ | sub TIESCALAR { shift->new('scalar', @_) } | ||||||||||||
|
Subroutine
TIESCALAR: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | 3 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 21 | 3 | my ($class, $type, $query) = @_; | |||||||||||||
| 22 | |||||||||||||||
| 23 | # keep track of tied data type | ||||||||||||||
| 24 | 3 | my $self = bless({type => $type}, $class); | |||||||||||||
| 25 | |||||||||||||||
| 26 | # for now i'm using a key _tie_ddg, | ||||||||||||||
| 27 | # but maybe this can be simplified | ||||||||||||||
| 28 | 3 | ▶ | if ($type eq 'array') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 29 | 1 | $self->do_search('_tie_ddg', $query); | |||||||||||||
| 30 | } | ||||||||||||||
| 31 | elsif ($type eq 'hash') { | ||||||||||||||
| 32 | 1 | $self->do_search($query, $query); | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | else { | ||||||||||||||
| 35 | ## $scalar = 1 | ||||||||||||||
| 36 | 1 | $self->do_search('_tie_ddg', $query, 1); | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 3 | $self; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 3 | ▶ | sub do_search { | ||||||||||||
|
Subroutine
do_search: called CC=6 SCAR=21.4
Pod
undocumented
| |||||||||||||||
| 43 | 3 | my ($self, $store_as, $query, $scalar) = @_; | |||||||||||||
| 44 | 3 | ▶ | return unless defined $query; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | # do search | ||||||||||||||
| 47 | 3 | my $uri = URI->new('https://duckduckgo.com/html/'); | |||||||||||||
| 48 | 3 | $uri->query_form({ q => $query }); | |||||||||||||
| 49 | |||||||||||||||
| 50 | 3 | my $resp = HTTP::Tiny->new->get($uri->as_string); | |||||||||||||
| 51 | 3 | my $tree = HTML::TreeBuilder::XPath->new_from_content($resp->{content}); | |||||||||||||
| 52 | |||||||||||||||
| 53 | # build results | ||||||||||||||
| 54 | 3 | my @results = $tree->findnodes( | |||||||||||||
| 55 | './/div[@id="content"]//div[@id="links"] | ||||||||||||||
| 56 | //div[@class =~ /\bresults_links\b/] | ||||||||||||||
| 57 | //div[@class =~ /\blinks_main\b/]' | ||||||||||||||
| 58 | ); | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 3 | my @save; | |||||||||||||
| 61 | 3 | for my $result (@results) { | |||||||||||||
| 62 | 0 | ▶ | my ($link) = $result->findnodes('.//a[@class="large"]') or next; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 0 | my ($snippet) = $result->findvalues('.//div[@class="snippet"]'); | |||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | push @save, | |||||||||||||
| 66 | { url => $link->attr('href'), | ||||||||||||||
| 67 | title => $link->as_text, | ||||||||||||||
| 68 | snippet => $snippet, | ||||||||||||||
| 69 | }; | ||||||||||||||
| 70 | 0 | ▶ | last if $scalar; # only save one result for scalar type | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 3 | ▶ | $self->{data}{$store_as} = $scalar ? [$save[0]] : \@save; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 1 | ▶ | sub FETCH { | ||||||||||||
|
Subroutine
FETCH: called CC=4 SCAR=17.9
| |||||||||||||||
| 77 | 1 | my ($self, $index) = @_; | |||||||||||||
| 78 | 1 | ▶ | $index = 0 unless defined $index; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | |||||||||||||||
| 80 | # if we're accessing a hash, we may have not done the search already | ||||||||||||||
| 81 | 1 | ▶ | if ($self->{type} eq 'hash') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 0 | ▶ | $self->do_search($index, $index) unless exists $self->{data}{$index}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | |||||||||||||||
| 84 | 0 | return $self->{data}{$index}; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 1 | $self->{data}{_tie_ddg}[$index]; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 2 | ▶ | sub FETCHSIZE { | ||||||||||||
|
Subroutine
FETCHSIZE: called CC=1 SCAR=0.0
| |||||||||||||||
| 91 | 2 | my $self = shift; | |||||||||||||
| 92 | 2 | scalar @{ $self->{data}{_tie_ddg} }; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | 0 | ▶ | sub SHIFT { shift @{shift->{data}{_tie_ddg}} } | ||||||||||||
|
Subroutine
SHIFT: not called CC=1 SCAR=6.9
| |||||||||||||||
| 96 | 0 | ▶ | sub POP { pop @{shift->{data}{_tie_ddg}} } | ||||||||||||
|
Subroutine
POP: not called CC=1 SCAR=6.9
| |||||||||||||||
| 97 | |||||||||||||||
| 98 | 0 | ▶ | sub SPLICE { | ||||||||||||
|
Subroutine
SPLICE: not called CC=2 SCAR=17.9
| |||||||||||||||
| 99 | 0 | my ($self, $offset, $limit) = @_; | |||||||||||||
| 100 | |||||||||||||||
| 101 | # oops, I don't think we want to write anything to the list | ||||||||||||||
| 102 | 0 | ▶ | if (@_ > 3) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | Carp::carp q,Can't replace search results with list!,; | |||||||||||||
| 104 | 0 | return; | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 0 | splice(@{$self->{data}{_tie_ddg}}, $offset, $limit); | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | 2 | ▶ | sub EXISTS { | ||||||||||||
|
Subroutine
EXISTS: called CC=2 SCAR=6.9
| |||||||||||||||
| 111 | 2 | my ($self, $index) = @_; | |||||||||||||
| 112 | 2 | ▶ | return exists($self->{data}{$index}) if $self->{type} eq 'hash'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | 1 | exists $self->{data}{_tie_ddg}[$index]; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | ▶ | sub STORE { Carp::carp q,Can't store search results to hash!, } | ||||||||||||
|
Subroutine
STORE: not called CC=1 SCAR=6.9
| |||||||||||||||
| 117 | 0 | ▶ | sub PUSH { Carp::carp q,Can't push search results to array!, } | ||||||||||||
|
Subroutine
PUSH: not called CC=1 SCAR=6.9
| |||||||||||||||
| 118 | 0 | ▶ | sub UNSHIFT { Carp::carp q,Can't unshift search results to array!, } | ||||||||||||
|
Subroutine
UNSHIFT: not called CC=1 SCAR=6.9
| |||||||||||||||
| 119 | |||||||||||||||
| 120 | 1 | 1; | |||||||||||||
| 121 | __END__ | ||||||||||||||