Ads/AdSense.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 273 CC 16 SCAR 35.2
CC
16
Cov
0%
get_adsense_ad
90.0
append_screen_res
20.0
new
12.0
CRAP 33.9
1 # Mobile::Ads::AdSense.pm version 0.1.0
2 #
3 # Copyright (c) 2008 Thanos Chatziathanassioy <tchatzi@arx.net>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6  
7 package Mobile::Ads::AdSense;
8 local $^W;
9 require 'Exporter.pm';
10 use vars qw(@ISA @EXPORT @EXPORT_OK);
11 @ISA = (Exporter);
12 @EXPORT = qw(); #&new);
13 @EXPORT_OK = qw();
14  
15 $Mobile::Ads::AdSense::VERSION='0.1.0';
16 $Mobile::Ads::AdSense::ver=$Mobile::Ads::AdSense::VERSION;
17  
18 use strict 'vars';
19 use Carp();
20 use Mobile::Ads();
21 use Time::HiRes();
22 use XML::LibXML();
23  
24 =head1 NAME
25  
26 Mobile::Ads::AdSense - module to serve Google Adsense for Mobile ads
27  
28 Version 0.1.0
29  
30 =head1 SYNOPSIS
31  
32 use Mobile::Ads::AdSense;
33 $ad = new Mobile::Ads::AdSense
34 ($text,$link) = $ad->get_text_ad({
35 site => 'Google AdSense site code',
36 remote => $ENV{'HTTP_USER_AGENT'},
37 address => $ENV{'REMOTE_ADDR'},
38 text => 'default ad text',
39 link => 'default ad link',
40 });
41
42 =head1 DESCRIPTION
43  
44 C<Mobile::Ads::AdSense> provides an object oriented interface to serve advertisements
45 from Google AdSense for Mobile in mobile sites.
46 This is just a slightly altered version of the perl code found on Google's site.
47  
48 =head1 new Mobile::Ads::AdSense
49  
50 =over 4
51  
52 =item [$parent]
53  
54 To reuse Mobile::Ads in multiple (subsequent) ad requests, you can pass a C<Mobile::Ads>
55 reference here. Instead of creating a new Mobile::Ads object, we will use the one you
56 pass instead. This might save a little C<LWP::UserAgent> creation/destruction time.
57  
58 =head2 Parameters/Properties
59  
60 =over 4
61  
62 =item site
63  
64 C<>=> Goggle Adsense site code, delivered by them. Something in the form off ``999''
65  
66 =item remote
67  
68 C<>=> Remote User Agent ($ENV{'HTTP_USER_AGENT'}). In fact $ENV{'HTTP_USER_AGENT'} will be used
69 if not supplied.
70  
71 =item address
72  
73 C<>=> $ENV{'REMOTE_ADDR'}. All things about HTTP_USER_AGENT also apply here.
74  
75 =item text
76  
77 C<>=> Should we fail to retrieve a real ad, this is the text of the ad displayed instead
78  
79 =item link
80  
81 C<>=> Same with text, but for the ad's link.
82  
83 =back
84  
85 =cut
86  
87 sub new {
88 my $this = shift;
89 my $class = ref($this) || $this;
90 my $self = {};
91 bless $self, $class;
92  
93 my $parent = shift;
94
95 if ($parent && ref($parent) && ref($$parent) && ref($$parent) eq "Mobile::Ads") {
96 $self->{'parent'} = $$parent;
97 }
98 elsif ($parent && ref($parent) && ref($parent) eq "Mobile::Ads") {
99 $self->{'parent'} = $parent;
100 }
101 else {
102 $self->{'parent'} = new Mobile::Ads;
103 }
104
105 $self->{'parser'} = XML::LibXML->new;
106 return $self;
107 }
108  
109 *get_ad = \&get_adsense_textimage_ad;
110  
111 sub get_adsense_ad {
112 my $self = shift;
113
114 my $ad_type = shift;
115
116 my ($site,$client,$remote,$address,$text,$link,$image,$color_border,$color_bg,$color_link,$color_text,$color_url,$format,$markup) =
117 ('','','','','','','','','','','','','','');
118 if (ref $_[0] eq 'HASH') {
119 $site = $_[0]->{'site'} || $self->{'site'};
120 $client = $_[0]->{'client'} || $self->{'client'};
121 $remote = $_[0]->{'remote'};
122 $address = $_[0]->{'address'};
123 $text = $_[0]->{'text'};
124 $link = $_[0]->{'link'};
125 $image = $_[0]->{'image'};
126 $color_border = $_[0]->{'color_border'};
127 $color_bg = $_[0]->{'color_bg'};
128 $color_link = $_[0]->{'color_link'};
129 $color_text = $_[0]->{'color_text'};
130 $color_url = $_[0]->{'color_url'};
131 $format = $_[0]->{'format'};
132 $markup = $_[0]->{'markup'};
133 }
134 else {
135 ($site,$client,$remote,$address,$text,$link,$image,$color_border,$color_bg,$color_link,$color_text,$color_url,$format,$markup) = @_;
136 }
137
138 $site ||= $self->{'site'};
139 $remote ||= $ENV{'HTTP_USER_AGENT'};
140 $address ||= $ENV{'REMOTE_ADDR'};
141 $text ||= $self->{'text'};
142 $link ||= $self->{'link'};
143
144 Carp::croak("cant serve ads without site (google calls it ``channel'')\n") unless ($site);
145 Carp::croak("cant serve ads without client\n") unless ($client);
146 Carp::croak("cant serve ads without remote user agent\n") unless ($remote);
147 Carp::croak("cant serve ads without remote address\n") unless ($address);
148
149 my $google_dt = sprintf("%.0f", 1000 * Time::HiRes::gettimeofday());
150 my $encoding = 'utf8';
151 my $google_host = (defined($ENV{'HTTP_HOST'}) ? 'http://'.$ENV{'HTTP_HOST'} : 'http://just.a.test');
152 my $url = ( defined($ENV{'REQUEST_URI'}) ? $google_host.$ENV{'REQUEST_URI'} : 'http://just.a.test/please_ignore_me');
153 my $referer = $ENV{'HTTP_REFERER'} || '';
154 #my $google_host = 'http://wapamama.net';
155 #my $url = $google_host.'/lalakis.xml';
156 #my $referer = 'http://wapamama.net/llala.xml?user=2' || '';
157
158 $color_border ||= '555555';
159 $color_bg ||= 'EEEEEE';
160 $color_link ||= '0000CC';
161 $color_text ||= '000000';
162 $color_url ||= '008000';
163
164 $format ||= 'mobile_single';
165 $markup ||= 'xhtml';
166 $ad_type ||= 'text_image';
167
168 # fetch data
169 my $res;
170 my $params = {
171 ad_type => $ad_type,
172 channel => $site,
173 client => $client,
174 color_border => $color_border,
175 color_bg => $color_bg,
176 color_link => $color_link,
177 color_text => $color_text,
178 color_url => $color_url,
179 dt => $google_dt,
180 format => $format,
181 host => $google_host,
182 ip => $address,
183 markup => $markup,
184 oe => $encoding,
185 output => $markup,
186 ref => $referer,
187 url => $url,
188 useragent => $remote,
189 };
190
191 $self->append_screen_res(\$params);
192
193 eval q[$res = $self->{'parent'}->get_ad({
194 url => 'http://pagead2.googlesyndication.com/pagead/ads',
195 method => 'GET',
196 params => $params
197 });];
198 if ($@) {
199 return '';
200 }
201 else {
202 my $ret = $self->parse($res);
203 return $ret;
204 }
205 }
206  
207 sub get_adsense_image_ad {
208 my $self = shift;
209
210 my $ad = $self->get_adsense_ad('image',@_);
211
212 return $ad;
213 }
214  
215 sub get_adsense_text_ad {
216 my $self = shift;
217
218 my $ad = $self->get_adsense_ad('text',@_);
219
220 return $ad;
221 }
222  
223 sub get_adsense_textimage_ad {
224 my $self = shift;
225
226 my $ad = $self->get_adsense_ad('text_image',@_);
227
228 return $ad;
229 }
230  
231 sub append_screen_res {
232 my $self = shift;
233
234 my $params = shift;
235
236 return if (!ref($params));
237
238 my $screen_res = $ENV{"HTTP_UA_PIXELS"};
239 my $delimiter = "x";
240 if (!$screen_res) {
241 $screen_res = $ENV{"HTTP_X_UP_DEVCAP_SCREENPIXELS"};
242 $delimiter = ",";
243 }
244
245 if ($screen_res) {
246 $screen_res =~ m|(\d+)$delimiter(\d+)| and ($$params->{'u_w'},$$params->{'u_h'}) = ($1,$2);
247 }
248 }
249  
250 sub parse {
251 my $self = shift;
252
253 my $toparse = shift;
254  
255 my $ret = '';
256
257 #if answer isn't a big comment...
258 if ($toparse !~ m|^\<\!\-\-.+?\-\-\>$|) {
259 $ret = $toparse;
260 }
261
262 if ($ret) {
263 $ret =~ s/\&(?!amp\;)/\&amp;/sg;
264 my $dom = $self->{'parser'}->parse_html_string($ret, { encoding => 'UTF-8' }) or Carp::croak("Cannot parse |$ret|");
265 $dom->setEncoding('UTF-8');
266 my $output_html = $dom->toString;
267 $output_html =~ s/.*\n//;
268 $output_html =~ s/.*\n//;
269 $output_html =~ s/\<\/?body\>//gs;
270 $output_html =~ s/\<\/?html\>//gs;
271 $ret = $output_html;
272 }
273
274 return $ret;
275 }
276  
277 =pod
278  
279 =head2 Methods
280  
281 =over 4
282  
283 =item get_text_ad
284  
285 C<>=> Does the actual fetching of a text ad for the site given. Refer to new for details
286 Returns a list ($text_for_ad,$link_for_ad) value.
287  
288 =item get_image_ad
289  
290 C<>=> Does the actual fetching of an image ad for the site given. Refer to new for details
291 Returns a list ($text_for_ad,$link_for_ad,$image_url) value.
292  
293 =back
294  
295 =cut
296  
297  
298 =head1 Revision History
299  
300 0.0.1
301 Initial Release
302 0.0.2
303 Fixed stupid typo
304 0.0.3
305 Didn't preserve default values on failure
306 0.0.4
307 All ua stuff put in Mobile::Ads
308 0.0.5
309 $ua timeout set to 2 sec in $self->{'parent'}
310 0.0.6
311 raw google output has some pitfalls. implemented XML::LibXML parsing
312 of the returned ad code.
313 0.0.7
314 slight problems with encoding in LibXML parsing
315 0.0.8
316 Aliased get_ad to get_adsense_ad
317 0.0.9
318 Option to reuse parent Mobile::Ads instead of creating anew
319 0.1.0
320 One could also use a reference to the parent... :)
321  
322 =head1 BUGS
323  
324 Thoughtlessly crafted to avoid having the same piece of code in several places.
325 Could use lots of enhancements.
326  
327 =head1 DISCLAIMER
328  
329 This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00
330 which is available on CPAN.
331  
332 =head1 AUTHOR
333  
334 Thanos Chatziathanassiou <tchatzi@arx.net>
335 http://www.arx.net
336  
337 =head1 COPYRIGHT
338  
339 Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved.
340  
341 This program is free software; you can redistribute it and/or
342 modify it under the same terms as Perl itself.
343  
344 =cut
345  
346 1;