| 1 | # Mobile::Ads::AdMob_v2.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::AdMob_v2; | ||
| 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::AdMob_v2::VERSION='0.1.0'; | ||
| 16 | $Mobile::Ads::AdMob_v2::ver=$Mobile::Ads::AdMob_v2::VERSION; | ||
| 17 | |||
| 18 | use strict 'vars'; | ||
| 19 | use Carp(); | ||
| 20 | use Mobile::Ads(); | ||
| 21 | use Digest::MD5 qw(); | ||
| 22 | |||
| 23 | =head1 NAME | ||
| 24 | |||
| 25 | Mobile::Ads::AdMob_v2 - module to serve AdMob ads | ||
| 26 | |||
| 27 | Version 0.1.0 | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use Mobile::Ads::AdMob_v2; | ||
| 32 | $ad = new Mobile::Ads::AdMob_v2 | ||
| 33 | ($text,$link,$image) = $ad->get_v2_ad({ | ||
| 34 | site => 'AdMob site code', | ||
| 35 | remote => $ENV{'HTTP_USER_AGENT'}, | ||
| 36 | address => $ENV{'REMOTE_ADDR'}, | ||
| 37 | text => 'default ad text', | ||
| 38 | link => 'default ad link', | ||
| 39 | test => 'set this if this is a test ad', | ||
| 40 | }); | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | C<Mobile::Ads::AdMob_v2> provides an object oriented interface to serve advertisements | ||
| 45 | from AdMob in mobile sites. | ||
| 46 | This is just a slightly altered version of the perl code found on AdMob's site. | ||
| 47 | |||
| 48 | =head1 new Mobile::Ads::AdMob_v2 | ||
| 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<>=> AdMob site code, delivered by them. Something in the form off ``0123456789abcde'' | ||
| 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->{'admob_ignore'} = { | ||
| 106 | 'HTTP_PRAGMA' => 1, | ||
| 107 | 'HTTP_CACHE_CONTROL' => 1, | ||
| 108 | 'HTTP_CONNECTION' => 1, | ||
| 109 | 'HTTP_USER_AGENT' => 1, | ||
| 110 | 'HTTP_COOKIE' => 1 | ||
| 111 | }; | ||
| 112 | return $self; | ||
| 113 | } | ||
| 114 | |||
| 115 | *get_ad = \&get_v2_ad; | ||
| 116 | |||
| 117 | sub get_v2_ad { | ||
| 118 | my $self = shift; | ||
| 119 | |||
| 120 | my $admob_version = '20080401-PERL-137fac4271564026'; | ||
| 121 | my $admob_endpoint = 'http://r.admob.com/ad_source.php'; | ||
| 122 | my $encoding = 'UTF-8'; | ||
| 123 | |||
| 124 | my ($site,$test,$remote,$address,$uri,$markup,$text,$link,$postal_code,$area_code,$coordinates,$dob,$gender,$keywords,$search) = | ||
| 125 | ('','','','','','','','','','','','','','','',''); | ||
| 126 | |||
| 127 | if (ref $_[0] eq 'HASH') { | ||
| 128 | $site = $_[0]->{'site'} || $self->{'site'}; | ||
| 129 | $test = $_[0]->{'test'} || ''; | ||
| 130 | $remote = $_[0]->{'remote'} || $ENV{'HTTP_USER_AGENT'}; | ||
| 131 | $address = $_[0]->{'address'} || $ENV{'REMOTE_ADDR'}; | ||
| 132 | $uri = $_[0]->{'uri'} || 'http://'.$ENV{'HTTP_HOST'}.$ENV{'REQUEST_URI'}; | ||
| 133 | $markup = $_[0]->{'markup'} || ''; | ||
| 134 | $text = $_[0]->{'text'} || $self->{'text'} || ''; | ||
| 135 | $link = $_[0]->{'link'} || $self->{'link'} || ''; | ||
| 136 | $postal_code = $_[0]->{'postal_code'} || ''; | ||
| 137 | $area_code = $_[0]->{'area_code'} || ''; | ||
| 138 | $coordinates = $_[0]->{'coordinates'} || ''; | ||
| 139 | $keywords = $_[0]->{'keywords'} || ''; | ||
| 140 | $search = $_[0]->{'search'} || ''; | ||
| 141 | $dob = $_[0]->{'dob'} || ''; | ||
| 142 | $gender = $_[0]->{'gender'} || ''; | ||
| 143 | #I cannot yet figure out what $admob_t is supposed to be | ||
| 144 | #but in their code, the way it is written, it is always '' (empty string or NULL) | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | ($site,$test,$remote,$address,$uri,$markup,$text,$link,$postal_code,$area_code,$coordinates,$dob,$gender,$keywords,$search) = @_; | ||
| 148 | } | ||
| 149 | |||
| 150 | $site ||= $self->{'site'}; | ||
| 151 | $remote ||= $ENV{'HTTP_USER_AGENT'}; | ||
| 152 | $address ||= $ENV{'REMOTE_ADDR'}; | ||
| 153 | $text ||= $self->{'text'}; | ||
| 154 | $link ||= $self->{'link'}; | ||
| 155 | |||
| 156 | Carp::croak("cant serve ads without site\n") unless ($site); | ||
| 157 | Carp::croak("cant serve ads without remote user agent\n") unless ($remote); | ||
| 158 | Carp::croak("cant serve ads without remote address\n") unless ($address); | ||
| 159 | |||
| 160 | my $admob_post = { | ||
| 161 | 's' => $site, | ||
| 162 | 'u' => $remote, | ||
| 163 | 'i' => $address, | ||
| 164 | 'p' => $uri, | ||
| 165 | 't' => '', #still haven't figured out what this is | ||
| 166 | 'e' => $encoding, | ||
| 167 | 'ma' => $markup, | ||
| 168 | 'v' => $admob_version, | ||
| 169 | 'd[pc]' => $postal_code, | ||
| 170 | 'd[ac]' => $area_code, | ||
| 171 | 'd[coord]' => $coordinates, | ||
| 172 | 'd[dob]' => $dob, | ||
| 173 | 'd[gender]' => $gender, | ||
| 174 | 'k' => $keywords, | ||
| 175 | 'search' => $search, | ||
| 176 | }; | ||
| 177 | |||
| 178 | #stuff the rest of the $ENV in $admob_post | ||
| 179 | foreach (keys(%ENV)) { | ||
| 180 | if ( !$self->{'admob_ignore'}->{$_} ) { | ||
| 181 | length($_) > 5 and $admob_post->{"h[" . substr( $_, 5 ) . "]"} = $ENV{$_}; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | #test ads need just find POSTs ``m'' field. | ||
| 186 | if ($test eq 'test') { | ||
| 187 | $admob_post->{'m'} = ''; | ||
| 188 | } | ||
| 189 | |||
| 190 | #do the POST | ||
| 191 | my $res; | ||
| 192 | #through $self->{parent} prefrably... | ||
| 193 | eval q[$res = $self->{'parent'}->get_ad({ | ||
| 194 | url => $admob_endpoint, | ||
| 195 | method => 'POST', | ||
| 196 | params => $admob_post | ||
| 197 | });]; | ||
| 198 | if ($@) { | ||
| 199 | return ($text,$link); | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | my $ret = $self->parse($res,$text,$link); | ||
| 203 | if (wantarray) { | ||
| 204 | return ($ret->{'text'},$ret->{'link'},$ret->{'image'}); | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | return $ret; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | sub parse { | ||
| 213 | my $self = shift; | ||
| 214 | |||
| 215 | my ($toparse,$text,$link) = @_; | ||
| 216 | my $ret = { }; | ||
| 217 | |||
| 218 | $toparse =~ m|\<a.+?href=[\"\']([^\'\"]+)[\'\"]|s and $ret->{'link'} = $1; | ||
| 219 | $toparse =~ m|\<img.+?src=[\"\']([^\'\"]+)[\'\"]|s and $ret->{'image'} = $1; | ||
| 220 | $toparse =~ m|\>([^\<\>]+?)\</a\>|s and $ret->{'text'} = $1; | ||
| 221 | |||
| 222 | #we need at least link and text to exist... | ||
| 223 | if ($ret->{'link'} && $ret->{'text'}) { | ||
| 224 | $ret->{'image'} ||= ''; | ||
| 225 | } | ||
| 226 | else { | ||
| 227 | $ret->{'link'} = $link; | ||
| 228 | $ret->{'text'} = $text; | ||
| 229 | $ret->{'image'} = ''; | ||
| 230 | } | ||
| 231 | |||
| 232 | defined($ret->{'link'}) and $ret->{'link'} = $self->{'parent'}->XMLEncode($ret->{'link'}); | ||
| 233 | defined($ret->{'text'}) and $ret->{'text'} = $self->{'parent'}->XMLEncode($ret->{'text'}); | ||
| 234 | defined($ret->{'image'}) and $ret->{'image'} = $self->{'parent'}->XMLEncode($ret->{'image'}); | ||
| 235 | |||
| 236 | return $ret; | ||
| 237 | } | ||
| 238 | |||
| 239 | =pod | ||
| 240 | |||
| 241 | =head2 Methods | ||
| 242 | |||
| 243 | =over 4 | ||
| 244 | |||
| 245 | =item get_v2_ad | ||
| 246 | |||
| 247 | C<>=> Does the actual fetching of the ad for the site given. Refer to new for details | ||
| 248 | Returns a list ($text_for_ad,$link_for_ad) value in list context or an | ||
| 249 | ``<a href="$link">$text</a>'' if called in scalar context. | ||
| 250 | |||
| 251 | =back | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | |||
| 256 | =head1 Revision History | ||
| 257 | |||
| 258 | 0.0.1 | ||
| 259 | Initial Release | ||
| 260 | 0.0.2 | ||
| 261 | Fixed stupid typo | ||
| 262 | 0.0.3 | ||
| 263 | Didn't preserve default values on failure | ||
| 264 | 0.0.4 | ||
| 265 | $ua timeout set to 20 sec | ||
| 266 | 0.0.5 | ||
| 267 | $ua timeout set to 2 sec | ||
| 268 | Implemented the new version AdMob code | ||
| 269 | (still some funky parts in there, but seems to work) | ||
| 270 | 0.0.6 | ||
| 271 | Aliased get_ad to get_v2_ad | ||
| 272 | 0.0.7 | ||
| 273 | Option to reuse parent Mobile::Ads instead of creating anew | ||
| 274 | 0.0.8/0.0.9 | ||
| 275 | Skipped those to have same verion number in all modules | ||
| 276 | 0.1.0 | ||
| 277 | One could also use a reference to the parent... :) | ||
| 278 | |||
| 279 | =head1 BUGS | ||
| 280 | |||
| 281 | Thoughtlessly crafted to avoid having the same piece of code in several places. | ||
| 282 | Could use lots of enhancements. | ||
| 283 | |||
| 284 | =head1 DISCLAIMER | ||
| 285 | |||
| 286 | This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00 | ||
| 287 | which is available on CPAN. | ||
| 288 | |||
| 289 | =head1 AUTHOR | ||
| 290 | |||
| 291 | Thanos Chatziathanassiou <tchatzi@arx.net> | ||
| 292 | http://www.arx.net | ||
| 293 | |||
| 294 | =head1 COPYRIGHT | ||
| 295 | |||
| 296 | Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved. | ||
| 297 | |||
| 298 | This program is free software; you can redistribute it and/or | ||
| 299 | modify it under the same terms as Perl itself. | ||
| 300 | |||
| 301 | =cut | ||
| 302 | |||
| 303 | 1; |