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
|
define_type(
'laposte',
-description='Tarifs d\'expédition de la poste'
);
// flatten arrays passed as params
local: 'flattenedparams' = js_flattenparams: params;
// replace the current params with the flattened params
params = #flattenedparams -> params;
// autocreate locals from params
locals = #flattenedparams -> locals;
local(
'cookiejar' = map,
'debug' = string,
'price' = decimal,
);
define_tag(
'oncreate',
-req='weight',
-req='cc',
-optional='rec',
-optional='eco',
-optional='cr'
);
local('jsessionid') = string;
local('category') = string;
local('om') = array('GP','GF','MQ','YT','NC','PF','RE','PM','WF');
local('rf') = #om;
#rf->(insert:'F');
select(true);
case(#cc == 'F');
#category = string('8N'); //Colissimo normal
case(#om >> #cc);
#category = string('8Y'); //Colissimo OM
case;
#category = string('CC'); // Colissimo international
/select;
//si -eco et international, c'est un international economique
local_defined('eco') && #category == 'CC' ? local('category') = string('CE');
//si -eco et OM, c'est un Colissimo OM economique
local_defined('eco') && #om >> #cc ? local('category') = string('XE');
//recommandé France
local_defined('rec') && #cc == 'F' ? local('category') = string('8U');
//recommandé Outre-Mer
local_defined('rec') && #om >> #cc ? local('category') = string('8Z');
fail_if(local_defined('rec')&&(#rf!>>#cc), -1, 'Erreur : Pas de recommande pour l\'international');
local('pp') = array(
'forward' = 'success',
'codeOffre' = #category,
'codePaysDestination' = #cc,
'poids' = #weight,
'page' = 'tarificationParticulier'
);
local_defined('rec')? #pp->(insert: 'choixTauxOptionObligatoire' = #rec);
///// fin config
protect;
local('page') = include_url(
'http://www.coliposte.fr/cotation/gp/tarificationParticulier.do',
-getparams = array('codeOffre'=#category),
-SendMIMEHeaders=(array:'User-Agent'='Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; fr; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'),
-retrievemimeheaders='hdr',
-timeout=15
);
/protect;
local('debug_cookie_sent') = ((self->cookiejar)->values->join:'<br/');
//get the special cookie code we need in step 2
iterate($hdr->find('Set-Cookie'), local('i'));
#i->value >> 'JSESSIONID'? #jsessionid = #i->value;
/iterate;
#jsessionid->(Replace: 'JSESSIONID=','');
#jsessionid->(Replace: 'path=/','');
//global routine for getting and updating cookiemap, should be ctagged or something
iterate($hdr->find('Set-Cookie'), local('i'));
local('debug_cookie_rcvd') += (#i->value)+'<br/>';
//we are given an array of cookies statements
local('ivalue') = (#i->value)->(split:';');
// this way for each of each cookie statement , we get an array like this:
// array ((NAME=VALUE),(Domain=.example.com),(/Path=/),(Expires:....))
local('valuename') = ((#ivalue->Get:1)->(split:'='))->First;
(self->cookiejar)->(insert: #valuename = #i->value);
//we update the cookiejar map, and the key is the value of the cookie
//limitations is that we don't care about domains so this is only for a single-domain transaction
/iterate;
self->debug += '<h4>1. Calling first page to get a cookie</h4><p>'
'<p><b>Cookie sent : <br/></b>'#debug_cookie_sent'</p>'
'<p><b>Cookie received : <br/></b>'#debug_cookie_rcvd'</p>'
'<p><b>Cookie stored : <br/></b>'((self->cookiejar)->values->join:'<br/>')'</p>'
//'<p><b>HTML : <br/></b><pre>'#page'</pre></p>'
;
////////////step (2)
protect;
local('page2') = include_url('http://www.coliposte.fr/cotation/gp/tarificationParticulier.do;jsessionid='+#jsessionid,
-postparams = #pp,
-SendMIMEHeaders=(array:'User-Agent'='Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; fr; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11', 'Cookie'=((self->cookiejar)->values->join:';')),
-retrievemimeheaders='hdr',
-timeout=15
);
/protect;
local('debug_cookie_sent') = ((self->cookiejar)->values->join:'<br/>');
iterate($hdr->find('Set-Cookie'), local('i'));
local('debug_cookie_rcvd') += (#i->value)+'<br/>';
local('ivalue') = (#i->value)->(split:';');
local('valuename') = ((#ivalue->Get:1)->(split:'='))->First;
(self->cookiejar)->(insert: #valuename = #i->value);
/iterate;
#page2->(replace:'\r\n', '');
#page2->(replace:'\t', '');
#page2->(replace:' ', '');
local('data') = #page2->(ExportString: 'iso-8859-1');
local('computed_price')=(String_FindRegExp: #data, -Find='resest</TD><TDalign="left"class="bordgris2">:(.*?)€*', -IgnoreCase);
self->debug += '<hr><h4>2. POST results</h4>'
'<p><b>POST params passed : </b>'#pp'</p>'
'<p><b>Cookie sent : <br/></b>'#debug_cookie_sent'</p>'
'<p><b>Cookie received : <br/></b>'#debug_cookie_rcvd'</p>'
'<p><b>Cookie stored : <br/></b>'((self->cookiejar)->values->join:'<br/>')'</p>'
//'<p><b>HTML : </b><pre>'encode_html(#data)'</pre></p>'
'<p><b>Prix : </b><pre>'#computed_price'</pre></p>'
;
local('final_price') = decimal(#computed_price->Get:2);
decimal(#final_price);
local_defined('cr')? #final_price = (#final_price + 11.72);
self->price = #final_price;
/define_tag;
/define_type;
|