File: linuxtrade.advfn

package info (click to toggle)
linuxtrade 3.65-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,880 kB
  • ctags: 1,969
  • sloc: ansic: 30,091; sh: 2,278; perl: 566; makefile: 126
file content (443 lines) | stat: -rwxr-xr-x 11,200 bytes parent folder | download | duplicates (2)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/perl
#
#	advfn.com helper script.
#
#	This script creates a "monitor" list with the desired symbols,
#	the fires up the so-called streamer.
#
#	The advfn.com streamer protocol is the worst streaming protocol
#	I have seen so far.  You have to do these 3 TCP/IP connections
#	to create a list of symbols first, then you can run the streamer
#	with yet another TCP/IP connection.  Its completely daft.  You
#	cannot easily change the symbols on the fly.
#
#	Oh yeah, did I mention that the streamer doesn't work on weekends?
#	You have to parse a web page if you want to see quotes then.

use LWP::UserAgent;

use Getopt::Std;
getopts('D:');

my $username = <STDIN>;
my $password = <STDIN>;
my $netname = <STDIN>;
chomp $username;
chomp $password;
chomp $netname;

$ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");

$hdrs = HTTP::Headers->new;
$hdrs->user_agent('Mozilla/4.78 [en] (X11; U; Linux 2.4.9-21 i686)');
$hdrs->authorization_basic($username, $password);

$NUM=99;
$burl = "http://www.$netname/cmn/monitor_form.php3";

# delete old list
$url = $burl . "?delete_$NUM=on&delete=true";
if ($opt_D >= 1) {
	print "GET $url\n";
}
$req = HTTP::Request->new("GET", $url, $hdrs);

$rc = $ua->simple_request($req);

if (!$rc->is_success && !$rc->is_redirect) {
	print $rc->content;
	die "delete request failed";
}

# create new list
$SYMS = $ARGV[0] || "VOD,SHEL,KEL,KMT";
$SYMS =~ tr/a-z/A-Z/;
$url = $burl . "?company_list=$SYMS"
		. "&new_list_name=linuxtrade&new_index=$NUM&create1=create1";
if ($opt_D >= 1) {
	print "GET $url\n";
}
$req = HTTP::Request->new("GET", $url, $hdrs);

$rc = $ua->simple_request($req);

if (!$rc->is_success && !$rc->is_redirect) {
	print $rc->content;
	die "create request failed";
}
#print "------------------------\n";
#print "$rc->is_redirect\n";
#print $rc->content;
#print "------------------------\n";

# Set list of columns
if ($netname eq "advfn.fr") {
	#GET /cmn/monitor_cust.php3?the_index=99&col_0=a&col_1=b&col_2=c&col_3=d&col_4=e&col_5=f&col_6=g&col_7=h&col_8=i&col_9=j&col_10=k&col_11=s&col_12=t&col_13=o&col_14=p&col_15=z&col_16=z&col_17=z&col_18=z&col_19=z&col_20=z&col_21=z&col_22=z&col_23=z&col_24=z&col_25=z&col_26=z&col_27=z&col_28=z&col_29=z&col_30=z&col_31=z&col_32=z&col_33=z&update.x=33&update.y=5 HTTP/1.0

        #<OPTION value='a' selected>Flche de variation</OPTION> #up/down arrow
        #<OPTION value='b'>Code</OPTION>			#symbol number
        #<OPTION value='c'>Libell de la valeur</OPTION>	#fullname
        #<OPTION value='d'>J/J-1 EUR</OPTION>
        #<OPTION value='e'>J/J-1 %</OPTION>
        #<OPTION value='f'>Dernier (cours actuel)</OPTION>	#last quote
        #<OPTION value='g'>Premier</OPTION>			#open
        #<OPTION value='h'>+haut</OPTION>			#high
        #<OPTION value='i'>+bas</OPTION>			#low
        #<OPTION value='j'>Clture Veille</OPTION>		#close
        #<OPTION value='k'>Volume jour</OPTION>			#volume
        #<OPTION value='l'>Volume J-1</OPTION>
        #<OPTION value='m'>Nb de transaction J</OPTION>
        #<OPTION value='n'>Nb de transaction J-1</OPTION>
        #<OPTION value='o'>Demande</OPTION>			#ask
        #<OPTION value='p'>Offre</OPTION>			#bid
        #<OPTION value='q'>Volume demande</OPTION>		#asksize
        #<OPTION value='r'>Volume offre</OPTION>		#bidsize
        #<OPTION value='s'>Dernire trans. vol</OPTION>		#lastsize
        #<OPTION value='t'>Dernire trans. heure</OPTION>	#lasttime
        #<OPTION value='u'>Intraday</OPTION>
        #<OPTION value='v'>Communiqus</OPTION>
        #<OPTION value='w'>Mmo</OPTION>
        #<OPTION value='x'>Achat/Vente Moniteur</OPTION>
        #<OPTION value='y'>Cumul Achat vol</OPTION>
        #<OPTION value='A'>Cumul Vente vol</OPTION>
        #<OPTION value='B'>Cumul ? vol</OPTION>
        #<OPTION value='C'>Achat %</OPTION>
        #<OPTION value='D'>Vente %</OPTION>
        #<OPTION value='E'>? %</OPTION>
        #<OPTION value='F'>+Haut J-1</OPTION>
        #<OPTION value='G'>+Bas J-1</OPTION>
        #<OPTION value='H'>Cap Traded</OPTION>
        #<OPTION value='z'><Selectionnez le contenu></OPTION>

	$url = "http://www.$netname/cmn/monitor_cust.php3"
		. "?the_index=$NUM"
		.  "&col_0=a"
		.  "&col_1=b"	#symbol
		.  "&col_2=c"
		.  "&col_3=d"	#change
		.  "&col_4=e"
		.  "&col_5=f"	#curr
		.  "&col_6=p"	#bid
		.  "&col_7=o"	#ask
		.  "&col_8=h"	#high
		.  "&col_9=i"	#low
		. "&col_10=g"	#open
		. "&col_11=k"	#vol
		. "&col_12=f"	#last
		. "&col_13=s"	#lastsz
		. "&col_14=t"	#lasttime
		. "&col_15=w"
		. "&col_16=u"
		. "&col_17=r"	#bidsz
		. "&col_18=q"	#lastsz
		. "&col_19=z"
		. "&col_20=z"
		. "&col_21=z"
		. "&col_22=z"
		. "&col_23=z"
		. "&col_24=z"
		. "&col_25=z"
		. "&col_26=z"
		. "&col_27=z"
		. "&col_28=z"
		. "&col_29=z"
		. "&col_30=z"
		. "&col_31=z"
		. "&col_32=z"
		. "&col_33=z"
		. "&update.x=33"
		. "&update.y=5"
		;

	$req = HTTP::Request->new("GET", $url, $hdrs);
	$rc = $ua->simple_request($req);
	if (!$rc->is_success && !$rc->is_redirect) {
		print $rc->content;
		die "column change request failed";
	}
}

# get applet launch web page
$url = "http://www.$netname/cmn/monitor.php3?the_index=$NUM";
$req = HTTP::Request->new("GET", $url, $hdrs);

$rc = $ua->simple_request($req);
if (!$rc->is_success && !$rc->is_redirect) {
	print $rc->content;
	die "create request failed";
}

if ($opt_D >= 9) {
	print $rc->content;
}

my $data;
my $cols;
my $rows;
my $streamer;
my $parmdata;
my $parmcols;
my $parmrows;

if ($netname eq "advfn.fr") {
	$parmcols = "1columns";
	$parmrows = "1rows";
	$parmdata = "1data";
	$streamer = "localhost";
} else {
	$parmcols = "2columns";
	$parmrows = "2rows";
	$parmdata = "2data";
	$streamer = "stream.$netname";
}

sub handler
{
	($tagname,$attr) = @_;
	return if $tagname ne "param";

	# print "<$attr->{name}> = <$attr->{value}>\n";

	if ($attr->{name} eq "$parmcols") {
		$cols = $attr->{value};
	} elsif ($attr->{name} eq "$parmrows") {
		$rows = $attr->{value};
	} elsif ($attr->{name} eq "$parmdata") {
		$data = $attr->{value};
	} elsif ($attr->{name} eq "STREAM_HOST") {
		$streamer = $attr->{value};
	}
}
$parser = HTML::Parser->new(api_version => 3);
$parser->handler( start => \&handler, "tagname,attr");
$parser->parse($rc->content);

if ($opt_D >= 1) {
	print $data . "\n";
	print "rows=$rows\n";
	print "cols=$cols\n";
	print "streamer=$streamer\n";
}

$id="1020832743456";

sub callback
{
	my($data, $response, $protocol) = @_;
	if ($data eq "STREAM\n") {
	}
	print "<" . $data . ">";
}

#$url = "http://stream.$netname/stream.php3?$id";
#$req = HTTP::Request->new("CONNECT", $url);
#$resp = $ua->request($req, \&callback, 4096);

sub strwrite
{
    my($socket,$data,$timeout) = @_;
    my $n = $socket->syswrite($data, length($data));
    return if $timeout && !$sel->can_write($timeout);
    $n;
}

my $socket_stuff = '';
sub getline
{
    my($socket,$timeout) = @_;
	
    while ($socket_stuff !~ m/\n/) {		# No complete line in buffer
	return if $timeout && !$sel->can_read($timeout);

	my $n = $socket->sysread(my $buf, 4096);
	return if !defined($n) || $n <= 0;
	$socket_stuff .= $buf;			# Append to our buffer.
    }
  
    (my $data, $socket_stuff) = split("\n", $socket_stuff, 2);
    chomp $data;
    $data;
}

$timeout = 20;
local($^W) = 0;  # IO::Socket::INET can be noisy
$socket = IO::Socket::INET->new(PeerAddr => "$streamer",
                                     PeerPort => 80,
                                     Proto    => 'tcp',
                                     Timeout  => $timeout,
                                    );

unless ($socket) {
        $@ =~ s/^.*?: //;
        die "Can't connect to advfn ($@)";
}

$sel = IO::Select->new($socket);
$CRLF="\r\n";
$buf = "POST /stream.php3?$id HTTP/1.1$CRLF"
	. "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)$CRLF"
	. "Host: stream.$netname$CRLF"
	. "Cache-Control: no-cache$CRLF"
	. "$CRLF";
$n = &strwrite($socket, $buf, 20);
die "write timeout" unless defined($n);

my $xxx = 
"{51~|i|{s1}/{g1}_down.gif~0\r
p1\r
{52~|l|{f2}VOD|t|VOD~0\r
{53~|l|{f1}VOD|t|Vodafone Grp.~0\r
{54~-0.25~65\r
{55~-0.2%~65\r
{56~102.75~65\r
{57~102.0~2\r
p10\r
{58~102.25~2\r
{59~103.25~0\r
{60~99.5~0\r
{61~100.0~0\r
{62~313,699,523~0\r
{63~|l|/cmn/trades.php3?epic=VOD|t|101.908|~2\r
{64~15,604~2\r
p20\r
{65~close~0\r
{66~|l|{f4}VOD|t|{l1}|~0\r
{67~|b|VOD|~0\r
{34~|i|{s1}/{g1}_up.gif~0\r
{35~|l|{f2}SHEL|t|SHEL~0\r
{36~|l|{f1}SHEL|t|Shell~0\r
{37~4.0~33\r
p30\r
{38~0.8%~33\r
{39~516.0~33\r
{40~515.5~2\r
{41~516.5~1\r
{42~517.5~0\r
{43~507.0~0\r
{44~507.0~0\r
p41\r
{45~25,582,069~0\r
{46~|l|/cmn/trades.php3?epic=SHEL|t|514.037|~2\r
{47~5,033~2\r
{48~close~0\r
{49~~0\r\n";

STDOUT->autoflush(1);

while (1)
{
	$buf = &getline($socket, 100);
        die "read timeout" unless defined($buf);
	chomp $buf;
	if ($opt_D >= 5) {
		print "<$buf>\n";
	}
	if ($buf =~ /STREAM/) {
		$n = &strwrite($socket,
			"GET /login.php3?1020832743456,2&906 HTTP/1.1"
			. "$CRLF$CRLF"
			. "DATA: $data$CRLF",
			20);
		die "write timeout" unless defined($n);
	}
	if ($buf =~ /^OK/) {
		#$socket_stuff = $xxx;
		#$cols = 17;
		#$rows = 4;
	}
	if ($buf =~ /^{/) {
		(my $crap, my $index, my $val, my $extra)
			= split(/^{|~/, $buf);
		$row = int($index / $cols);
		$col = $index % $cols;
		#print "$row,$col = $val\n";
		for ($col) {
		/^1$/	&& do { 
			$sym = $val;
			$sym =~ s/.*\|//;
			if ($sym ne "") {
				$sym{$row} = $sym;
			}
			last;
			};
		/^3$/	&& do {
			$change{$row} = $val;
			if ($curr{$row}) {
				print "$sym{$row}|$col|$val|chg\n";
			}
			last;
			};
		/^5$/	&& do {
			print "$sym{$row}|$col|$val|curr\n";
			if (!$curr{$row}) {
				print "$sym{$row}|3|$change{$row}|chg\n";
			}
			$curr{$row} = $val;
			last;
			};
		/^6$/	&& do { print "$sym{$row}|$col|$val|bid\n"; last; };
		/^7$/	&& do { print "$sym{$row}|$col|$val|ask\n"; last; };
		/^8$/	&& do { print "$sym{$row}|$col|$val|high\n"; last; };
		/^9$/	&& do { print "$sym{$row}|$col|$val|low\n"; last; };
		/^10$/	&& do { print "$sym{$row}|$col|$val|open\n"; last; };
		/11/	&& do {
			$val =~ s/,//g;
			print "$sym{$row}|$col|$val|vol\n";
			last;
			};
		/12/	&& do {
			my $g;
			$val =~ s/.*\|([^|]*)\|/\1/;
			($g,$g,$g,$g,my $val2,$g) = split(/|/, $val);
			print "$sym{$row}|$col|$val|last\n";
			last;
			};
		/13/	&& do {
			$val =~ s/,//g;
			print "$sym{$row}|$col|$val|lastsz\n";
			last;
			};
		/14/	&& do { print "$sym{$row}|$col|$val|time\n"; last; };
		/17/	&& do {
			$val =~ s/,//g;
			print "$sym{$row}|$col|$val|bidsz\n";
			last;
			};
		/18/	&& do {
			$val =~ s/,//g;
			print "$sym{$row}|$col|$val|asksz\n";
			last;
			};
		}
	}
}

#col	desc
#0	?	{51~|i|{s1}/{g1}_down.gif~0
#1	sym	|l|{f2}VOD|t|VOD
#2	name	|l|{f1}VOD|t|Vodafone Grp.
#3	change	-2.75
#4	chg%	-2.5%
#5	curr	107.75
#6	bid	108
#7	ask	108.25
#8	hi
#9	low
#10	open
#11	volume	276,131,679
#12	last	|l|/cmn/trades.php3?epic=VOD|t|107.79|
#13	tradevol
#14	time	close
#15		{66~~0>
#16		{67~|b|VOD|~0>

#$size = 4096;
#while(1) {
#        die "read timeout" if $timeout && !$sel->can_read($timeout);
#        $n = $socket->sysread($buf, $size, length($buf));
#        die $! unless defined($n);
#        die "unexpected EOF before status line seen" unless $n;
#	chop $buf;
#	print "<$buf>\n"
#}