-
Notifications
You must be signed in to change notification settings - Fork 3
/
asin_complex-v20051117
160 lines (126 loc) · 4.06 KB
/
asin_complex-v20051117
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
# Blosxom Plugin : asin_complex
# Author(s) : kay <[email protected]>
# Version : 2005-11-17
package asin_complex;
# --------------------------------------
# Configurable Variable
my $associatetag = "YOUR_ASSOCIATE_TAG"; # Associate Tag
my $subscriptionid = "GTYDRES564THU"; # Subscription ID(Manual Default)
my $xslfile = "/lib/asinsimple.xsl"; # XML Stylesheet URL
my $locale = "jp";
# Cache Configuration
my $asin_dir = "$blosxom::plugin_state_dir/asin";
my $modify = "24";
# Path to no image
my $noImage = "no-image_URL";
my $noImageHeight = "130";
my $noImageWidth = "130";
# --------------------------------------
# Base URL
my $requesturl = "http://xml-jp.amznxslt.com/onca/xml3"; # for Amazon.co.jp
my $targeturl = "http://www.amazon.co.jp/exec/obidos/ASIN/"; # for Amazon.co.jp
#Constant value for deadlock routine
#--- v20051108 New
my $LockName = "$asin_dir/Locked";
my $retry = 10;
# ======================================
use LWP::Simple;
sub start {
if (!-e $asin_dir) {
my $mkdir_r = mkdir($asin_dir, 0755) or die "Can't creat $asin_dir : $1";
$mkdir_r or return 0;
}
return 1;
}
sub story {
my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
$$body_ref =~ s/´[asin:([A-Z0-9]{10})´]/requestAWS($1)/gei;
$$body_ref =~ s/<a´s+href="asin:([A-Z0-9]{10})"´s*>/'<a href="'.rewriteASIN($1).'">'/gei;
return 1;
}
sub rewriteASIN { # SubRoutine to make URL from ASIN code
my ($asinCode) = @_;
return "$targeturl" . "$asinCode/$associatetag" . "/ref=nosim/";
}
sub requestAWS { # SubRoutine to request to AWS
my ($asinCode) = @_;
my $request = "$requesturl?" .
"dev-t=$subscriptionid&" .
"t=$associatetag&" . "f=$xslfile&" .
"locale=$locale&type=lite&" . "AsinSearch=$asinCode";
my $cache = "$asin_dir/$asinCode.html";
my $tmpcache = "$asin_dir/$asinCode.tmp";
my $rtn = "";
if ( -e "$LockName$asinCode" ) {
# for LockFile exist
if ( -M "$LockName$asinCode" < 1/288 ) {
# Now cache being creat or refreshing
while ( -e "$LockName$asinCode" ) {
if ( --$retry < 1 ) { last; } # Give up to refresh cache after 10s waiting
sleep(1);
}
} else {
# cleaning
if ( -e $tmpcache ) { unlink( $tmpcache ); }
rmdir( "$LockName$asinCode" ); # LockFile Cleaning
}
} else {
# for LockFile do not exist
mkdir( "$LockName$asinCode", 0755 ); # Cache Locked!
if ( -e $cache ) {
if ( -M $cache > ( $modify / 24 ) or -s _ < 160 ) {
# Cache files less 160bytes is refreshed by my experiense.
create_cache( $tmpcache, $request );
if ( unlink( $cache ) ) {
rename( $tmpcache, $cache );
}
}
} else {
# Create new cache
create_cache( $cache, $request );
}
rmdir( "$LockName$asinCode" ); # Cache unlocked!
}
# Read from cache
if ( open CACHE, $cache ) {
$rtn = join( '', <CACHE> );
$rtn = '<div class="asinSimple">' . $rtn . '</div>';
close CACHE;
}
return $rtn;
}
sub create_cache { # SubRoutine to create new cache
my ( $cache_file, $request ) = @_;
my $url = "s?https?://[-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+";
# Request and get that response
my $ua = new LWP::UserAgent;
$ua->agent("asin_complex");
$ua->timeout(10);
my $r = $ua->mirror($request, $cache_file);
my $rtn = "";
# Cache Conversion
if ( open CACHE, $cache_file ) {
while ( <CACHE> ) {
if ( /<img´s+src="($url)"´s*>/ ) { # Is image legal?
my $header = head( $1 );
my $convtag = '<img src="' . $1 . '" />';
if ( $header->content_length < 820 ) {
# Image file less 820bytes is "No Image".
$convtag = '<img src="' . $noImage . '" ' .
'height="' . $noImageHeight . 'px" ' .
'width="' . $noImageWidth . 'px" />';
}
$_ =~ s/<img´s+src="$url"´s*>/$convtag/gs;
}
# for HTML 1.0
$_ =~ s/<br>/<br ´/>/gs;
$rtn .= $_;
}
close CACHE;
if ( open CACHE, "> $cache_file" ) {
print CACHE $rtn;
close CACHE;
}
}
return 1;
}