-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadsense-v0i2
181 lines (138 loc) · 5.25 KB
/
adsense-v0i2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
M# blosxom plugin: adsense
# raffi krikorian <[email protected]>
# version: 0.0.2
# see the bottom of the source for documentation
package adsense;
use CGI qw/:standard :netscape/;
use File::stat;
use FileHandle;
use LWP::UserAgent;
# --- configurable variables -----
# the identifier that is included as google_ad_client in the
# javascript that is given to you by the adsense website
my $ad_client = "pub-1258118923120631";
# --------------------------------
$adsenseurl;
$adsenseurlescaped;
$adsensetitle;
$adsensedesc;
sub start {
return 0 if( $blosxom::flavour ne "rss" );
1;
}
# this is the helper function that actually gets the advertisment from
# google, parses the information, and assigns the variables to be used
# by the blosxom templates
sub fetchad {
my( $path ) = @_;
my $useragent = LWP::UserAgent->new();
# construct the URL to get the advertisement from the google site
# with. this has to get the referrer correct, the time of day for
# the request, and the asks for only a single advertisement. we
# use the first story as the referrer so we can get (hopefully)
# better targeted advertisements.
my $refererurl = $path;
$refererurl =~ s|^$blosxom::datadir(/.*?)\.[^\.]*?$|$blosxom::url$1\.html|;
$refererurl =~ s|\:|\%3A|g;
$refererurl =~ s|/|\%2F|g;
my $adurl = "http://pagead2.googlesyndication.com/pagead/ads?client=ca-";
$adurl .= $ad_client;
$adurl .= "&random=" . ( time() * 1000 );
$adurl .= "&format=125x125_as";
$adurl .= "&output=html";
$adurl .= "&url=" . $refererurl;
# make the request to fetch the html containing the advertisement
my $request = HTTP::Request->new( GET => $adurl );
$request->referer( $blosxom::url );
$request->user_agent( "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/124 (KHTML, like Gecko) Safari/125" );
$response = $useragent->request( $request )->content();
# and if that worked, pull out the relavent information
if( $response ) {
$response =~ m|<font[^>]*?>\s*<a.*?href=\"(.*?)\"[^>]*>\s*<b>(.*?)</b></a><br></font><font[^>]*?>(.*?)<br>|;
$googleurl = "http://pagead2.googlesyndication.com$1";
$googletitle = $2;
$googledesc = $3;
$googleurlescaped = $googleurl;
$googleurlescaped =~ s|\&|&|g;
return( $googleurl, $googleurlescaped, $googletitle, $googledesc );
} else {
return( );
}
}
sub filter {
my( $pkg, $files_ref ) = @_;
# get a list of all the files that are being rendered, and pick
# out the timestamp of the most recent story
my $fh = new FileHandle;
my @files =
sort { $files_ref->{$b} <=> $files_ref->{$a} } keys %$files_ref;
my $recenttime = $files_ref->{$files[0]};
# now check to see if we have a state file -- we check the
# timestamp on that file and if the timestamp is -earlier- than
# the recenttime variable, then we are going to replace that file
# with a new advertisement. if it is less than or equal to, then
# we are going to open the file and pull the google ad out of it.
#
# we don't want to pull a new advertisement every time as google
# changes the advertisement URLs frequently, even if they are
# pointing to the same destination web site. this does lead to
# the problem that the advertisement URLs may get stale.
my $statefile = $blosxom::plugin_state_dir . "/adsense.state";
if( ( ! -e $statefile ) ||
( stat( $statefile )->mtime < $recenttime ) ) {
my @ad = fetchad( $files[0] );
# store the advertisement into this state file for the next
# time around
unlink $statefile;
open ADSTATE, ">$statefile";
print ADSTATE $ad[0] . "\n" . $ad[1] . "\n" . $ad[2] . "\n" . $ad[3] . "\n";
close ADSTATE;
$adsenseurl = $ad[0];
$adsenseurlescaped = $ad[1];
$adsensetitle = $ad[2];
$adsensedesc = $ad[3];
} else {
# we don't need a new advertisement, so we are just going to
# read the one from the state file
open ADSTATE, "<$statefile";
$adsenseurl = <ADSTATE>;
$adsenseurlescaped = <ADSTATE>;
$adsensetitle = <ADSTATE>;
$adsensedesc = <ADSTATE>;
close ADSTATE;
}
1;
}
1;
__END__
=head1 NAME
blosxom plug-in: adsense
=head1 SYNOPSIS
fills the variables $adsense::adsenseurl, $adsense::adsenseurlescaped,
$adsense::adsensetitle, $adsense::adsensedesc with the google
advertisement URL (in non-escaped and escaped form), the title of the
advertisement, and a one line description of the advertisement for
inclusion in the RSS of the weblog.
=head1 USAGE
activate this plugin by placing it in your bloxsom plugin directory,
then in your foot.rss (or something similar), add the following
<item>
<title>Google Ad: $adsense::adsensetitle</title>
<link>$adsense::adsenseurlescaped</link>
<description>$adsense::adsensedesc</description>
</item>
</channel>
</rss>
=head1 VERSION
0.0.1
=head1 AUTHOR
raffi krikorian <[email protected]>, http://www.bitwaste.com/
=head1 LICENSE
adsense plugin
copyright 2004, raffi krikorian
this software is distributed under the GPL
(http://www.gnu.org/copyleft/gpl.html) with the following addition --
if you do use this plugin, then you are required to publish full
stories in your RSS and -not- use excerpts. the only exception to
that rule is if the story is "long" (as defined by more than a 250
words) and requires a link to get to the remainder of the content.