File Coverage

blib/lib/App/DuckDuckGo.pm
Criterion Covered Total %
statement 7 133 5.2
branch 0 106 0.0
condition 0 24 0.0
subroutine 3 10 30.0
pod 0 7 0.0
total 10 280 3.5


line stmt bran cond sub pod time code
1             package App::DuckDuckGo;
2             BEGIN {
3 1     1   30189 $App::DuckDuckGo::AUTHORITY = 'cpan:DDG';
4             }
5             {
6             $App::DuckDuckGo::VERSION = '0.008';
7             }
8             # ABSTRACT: Application to query DuckDuckGo
9              
10 1     1   2858 use Moose;
  1         651777  
  1         10  
11 1     1   10409 use WWW::DuckDuckGo;
  1         218724  
  1         1976  
12              
13             with qw(
14             MooseX::Getopt
15             );
16              
17             our $VERSION ||= '0.0development';
18              
19             has duckduckgo => (
20             metaclass => 'NoGetopt',
21             isa => 'WWW::DuckDuckGo',
22             is => 'ro',
23             default => sub {
24             my $self = shift;
25             WWW::DuckDuckGo->new( http_agent_name => __PACKAGE__.'/'.$VERSION, forcesecure => $self->forcesecure );
26             },
27             );
28              
29             has query => (
30             isa => 'Str',
31             is => 'rw',
32             predicate => 'has_query',
33             );
34              
35             has batch => (
36             isa => 'Bool',
37             is => 'rw',
38             default => sub { 0 },
39             );
40              
41             has forcesecure => (
42             isa => 'Bool',
43             is => 'rw',
44             default => sub { 0 },
45             );
46              
47             has api => (
48             isa => 'Str',
49             is => 'ro',
50             default => sub { 'zeroclickinfo' },
51             );
52              
53             sub set_query_by_extra_argv {
54 0     0 0   my ( $self ) = @_;
55 0 0         $self->query(join(" ",@{$self->extra_argv})) if @{$self->extra_argv};
  0            
  0            
56             }
57              
58             sub print_query_with_extra_argv {
59 0     0 0   my ( $self ) = @_;
60 0           $self->set_query_by_extra_argv;
61 0           $self->print_query;
62             }
63              
64             sub print_query {
65 0     0 0   my ( $self ) = @_;
66 0 0         return if !$self->has_query;
67 0           my $api = $self->api;
68 0           eval {
69 0           my $result = $self->duckduckgo->$api($self->query);
70 0           my $function = 'print_'.$self->api;
71 0           $self->$function($result);
72             };
73 0 0         if ($@) {
74 0           print ' _____ ____ ____ ___ ____'."\n";
75 0           print '| ____| _ \\| _ \\ / _ \\| _ \\'."\n";
76 0           print '| _| | |_) | |_) | | | | |_) |'."\n";
77 0           print '| |___| _ <| _ <| |_| | _ <'."\n";
78 0           print '|_____|_| \\_\\_| \\_\\\\___/|_| \\_\\'."\n";
79 0           print "\nAn error occured, we cant execute your query:\n\n";
80 0           print " ".$@."\n";
81 0           print "This is regulary not your fault, please try again later.\n";
82 0           print "If the problem stay, please report on https://github.com/Getty/p5-app-duckduckgo/issues\n\n";
83 0           exit 1;
84             }
85             }
86              
87             sub print_zeroclickinfo {
88 0     0 0   my ( $self, $zci ) = @_;
89 0 0         if ($self->batch) {
90 0           print join("\n",$self->zeroclickinfo_batch_lines($zci))."\n";
91             } else {
92            
93 0           print "\n";
94              
95 0 0         print "Redirected to: ".$zci->redirect."\n" if $zci->has_redirect;
96 0 0         if ($zci->has_answer) {
97 0           print "And the answer is:\n\n";
98 0           print $zci->answer."\n\n";
99 0           print "This answer was brought to you by '".$zci->answer_type."'. Fasten seat belts.\n\n";
100             }
101            
102 0           my $heading;
103 0 0         $heading = $zci->heading if $zci->has_heading;
104 0 0 0       $heading .= " (".$zci->type_long.")" if $heading and $zci->has_type;
105 0 0         print $heading."\n\n" if $heading;
106            
107 0 0         if ($zci->has_definition) {
108 0 0         my $definition = $zci->definition if $zci->has_definition;
109 0 0         $definition .= " (".$zci->definition_source.")" if $zci->has_definition_source;
110 0 0         $definition .= "\nSource: ".$zci->definition_url->as_string if $zci->has_definition_url;
111 0           print $definition."\n\n";
112             }
113              
114 0 0         if ($zci->has_abstract_text) {
115 0           my $abstract = $zci->abstract_text;
116 0 0         $abstract .= " (".$zci->abstract_source.")" if $zci->has_abstract_source;
117 0 0         $abstract .= "\nSource: ".$zci->abstract_url->as_string if $zci->has_abstract_url;
118 0           print "Description: ".$abstract."\n\n";
119             }
120            
121 0 0         if ($zci->has_default_related_topics) {
122 0           print "Related Topics:\n";
123 0           for (@{$zci->default_related_topics}) {
  0            
124 0 0 0       if ($_->has_text or $_->has_first_url) {
125 0           print " - ";
126 0 0         print $_->text."\n" if $_->has_text;
127 0 0 0       print " " if $_->has_text and $_->has_first_url;
128 0 0         print $_->first_url->as_string."\n" if $_->has_first_url;
129             }
130             }
131 0           print "\n";
132             }
133              
134 0 0 0       if (!$zci->has_default_related_topics and %{$zci->related_topics_sections}) {
  0            
135 0           print "Related Topics Groups:\n";
136 0           for (keys %{$zci->related_topics_sections}) {
  0            
137 0           print " Related Topics Groupname: ".$_."\n";
138 0           for (@{$zci->related_topics_sections->{$_}}) {
  0            
139 0 0 0       if ($_->has_text or $_->has_first_url) {
140 0           print " - ";
141 0 0         print $_->text."\n" if $_->has_text;
142 0 0 0       print " " if $_->has_text and $_->has_first_url;
143 0 0         print $_->first_url->as_string."\n" if $_->has_first_url;
144             }
145             }
146             }
147 0           print "\n";
148             }
149            
150 0 0         if ($zci->results) {
151 0           print "Other Results:\n";
152 0           for (@{$zci->results}) {
  0            
153 0 0 0       if ($_->has_text or $_->has_first_url) {
154 0           print " - ";
155 0 0         print $_->text."\n" if $_->has_text;
156 0 0 0       print " " if $_->has_text and $_->has_first_url;
157 0 0         print $_->first_url->as_string."\n" if $_->has_first_url;
158             }
159             }
160 0           print "\n";
161             }
162            
163             }
164             }
165              
166             sub zeroclickinfo_batch_lines {
167 0     0 0   my ( $self, $zci ) = @_;
168 0           my @lines;
169 0 0         push @lines, "Abstract: ".$zci->abstract if $zci->has_abstract;
170 0 0         push @lines, "AbstractText: ".$zci->abstract_text if $zci->has_abstract_text;
171 0 0         push @lines, "AbstractSource: ".$zci->abstract_source if $zci->has_abstract_source;
172 0 0         push @lines, "AbstractURL: ".$zci->abstract_url->as_string if $zci->has_abstract_url;
173 0 0         push @lines, "Image: ".$zci->image->as_string if $zci->has_image;
174 0 0         push @lines, "Heading: ".$zci->heading if $zci->has_heading;
175 0 0         push @lines, "Answer: ".$zci->answer if $zci->has_answer;
176 0 0         push @lines, "AnswerType: ".$zci->answer_type if $zci->has_answer_type;
177 0 0         push @lines, "Definition: ".$zci->definition if $zci->has_definition;
178 0 0         push @lines, "DefinitionSource: ".$zci->definition_source if $zci->has_definition_source;
179 0 0         push @lines, "DefinitionURL: ".$zci->definition_url->as_string if $zci->has_definition_url;
180 0 0         push @lines, "Type: ".$zci->type if $zci->has_type;
181 0 0         if (%{$zci->related_topics_sections}) {
  0            
182 0           push @lines, "RelatedTopicsSections:";
183 0           for (keys %{$zci->related_topics_sections}) {
  0            
184 0           push @lines, " RelatedTopicsSection: ".$_;
185 0           push @lines, $self->zeroclickinfo_batch_links_lines(@{$zci->related_topics_sections->{$_}});
  0            
186             }
187             }
188 0 0         if ($zci->results) {
189 0           push @lines, "Results:";
190 0           push @lines, $self->zeroclickinfo_batch_links_lines(@{$zci->results});
  0            
191             }
192 0           return @lines;
193             }
194              
195             sub zeroclickinfo_batch_links_lines {
196 0     0 0   my ( $self, @links ) = @_;
197 0           my @lines;
198 0           for (@links) {
199 0 0         push @lines, " -- " if @lines;
200 0 0         push @lines, " Result: ".$_->result if $_->has_result;
201 0 0         push @lines, " FirstURL: ".$_->first_url->as_string if $_->has_first_url;
202 0 0         push @lines, " Text: ".$_->text if $_->has_text;
203 0 0         if ($_->has_icon) {
204 0           push @lines, " Icon:";
205 0           push @lines, $self->zeroclickinfo_batch_icon_lines($_->icon);
206             }
207             }
208 0           return @lines;
209             }
210              
211             sub zeroclickinfo_batch_icon_lines {
212 0     0 0   my ( $self, $icon ) = @_;
213 0           my @lines;
214 0 0         push @lines, " URL: ".$icon->url->as_string if $icon->has_url;
215 0 0         push @lines, " Width: ".$icon->width if $icon->has_width;
216 0 0         push @lines, " Height: ".$icon->height if $icon->has_height;
217 0           return @lines;
218             }
219              
220             1;
221              
222              
223             __END__
224             =pod
225              
226             =head1 NAME
227              
228             App::DuckDuckGo - Application to query DuckDuckGo
229              
230             =head1 VERSION
231              
232             version 0.008
233              
234             =head1 SYNPOSIS
235              
236             use App::DuckDuckGo;
237             App::DuckDuckGo->new_with_options->print_query_with_extra_argv;
238              
239             =head2 DESCRIPTION
240              
241             This is the class which is used by duckduckgo script to do the work. Please read L<duckduckgo> to get the documentation for the command line tool.
242              
243             =head1 AUTHOR
244              
245             Torsten Raudssus <torsten@raudssus.de>
246              
247             =head1 COPYRIGHT AND LICENSE
248              
249             This software is copyright (c) 2013 by L<DuckDuckGo Inc.|https://duckduckgo.com/>.
250              
251             This is free software; you can redistribute it and/or modify it under
252             the same terms as the Perl 5 programming language system itself.
253              
254             =cut
255