line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::Bitcoin::Feed::Site::Hitbtc; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
19806
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
5
|
1
|
|
|
1
|
|
417
|
use Mojo::Base 'Finance::Bitcoin::Feed::Site'; |
|
1
|
|
|
|
|
6547
|
|
|
1
|
|
|
|
|
5
|
|
6
|
1
|
|
|
1
|
|
624
|
use Mojo::UserAgent; |
|
1
|
|
|
|
|
217371
|
|
|
1
|
|
|
|
|
17
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has ws_url => 'ws://api.hitbtc.com'; |
11
|
|
|
|
|
|
|
has 'ua'; |
12
|
|
|
|
|
|
|
has site => 'HITBTC'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
1
|
|
|
1
|
1
|
17
|
my $class = shift; |
16
|
1
|
|
|
|
|
17
|
my $self = $class->SUPER::new(@_); |
17
|
1
|
|
|
|
|
4
|
$self->on('json', \&on_json); |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
9
|
return $self; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub go { |
23
|
2
|
|
|
2
|
1
|
1139
|
my $self = shift; |
24
|
2
|
|
|
|
|
11
|
$self->SUPER::go(@_); |
25
|
2
|
|
|
|
|
16
|
$self->ua(Mojo::UserAgent->new); |
26
|
2
|
|
|
|
|
102
|
$self->debug('connecting...', $self->ws_url); |
27
|
|
|
|
|
|
|
$self->ua->websocket( |
28
|
|
|
|
|
|
|
$self->ws_url => sub { |
29
|
2
|
|
|
2
|
|
162
|
my ($ua, $tx) = @_; |
30
|
2
|
100
|
|
|
|
12
|
unless ($tx->is_websocket) { |
31
|
1
|
|
|
|
|
43
|
$self->error("WebSocket handshake failed!"); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# set timeout; |
34
|
1
|
|
|
|
|
8
|
$self->set_timeout; |
35
|
1
|
|
|
|
|
27
|
return; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$tx->on( |
39
|
|
|
|
|
|
|
json => sub { |
40
|
1
|
|
|
|
|
38
|
my ($tx, $hash) = @_; |
41
|
1
|
|
|
|
|
6
|
$self->emit('json', $hash); |
42
|
1
|
|
|
|
|
39
|
}); |
43
|
2
|
|
|
|
|
44
|
}); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub on_json { |
47
|
2
|
|
|
2
|
0
|
416
|
my ($self, $hash) = @_; |
48
|
|
|
|
|
|
|
|
49
|
2
|
50
|
50
|
|
|
10
|
if ($hash->{MarketDataIncrementalRefresh} |
|
2
|
|
|
|
|
9
|
|
50
|
|
|
|
|
|
|
&& scalar @{$hash->{MarketDataIncrementalRefresh}{trade}}) |
51
|
|
|
|
|
|
|
{ |
52
|
2
|
|
|
|
|
2
|
for my $trade (@{$hash->{MarketDataIncrementalRefresh}{trade}}) { |
|
2
|
|
|
|
|
6
|
|
53
|
2
|
|
|
|
|
11
|
$self->emit('data_out', $trade->{timestamp}, $hash->{MarketDataIncrementalRefresh}{symbol}, $trade->{price}); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
__END__ |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 NAME |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Finance::Bitcoin::Feed::Site::Hitbtc -- the class that connect and fetch the bitcoin price data from site hitbtc |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 SYNOPSIS |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
use Finance::Bitcoin::Feed::Site::Hitbtc; |
69
|
|
|
|
|
|
|
use AnyEvent; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $obj = Finance::Bitcoin::Feed::Site::BtcChina->new(); |
72
|
|
|
|
|
|
|
# listen on the event 'output' to get the adata |
73
|
|
|
|
|
|
|
$obj->on('output', sub { shift; say @_ }); |
74
|
|
|
|
|
|
|
$obj->go(); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# dont forget this |
77
|
|
|
|
|
|
|
AnyEvent->condvar->recv; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 DESCRIPTION |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Connect to site Hitbtc by protocol websocket and fetch the bitcoin price data. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 EVENTS |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
This class inherits all events from L<Finance::Bitcoin::Feed::Site> and add some new ones. |
86
|
|
|
|
|
|
|
The most important event is 'output'. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head2 output |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
It will be emit by its parent class when print out the data. You can listen on this event to get the output. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 SEE ALSO |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
L<Finance::Bitcoin::Feed::Site> |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
L<Mojo::UserAgent> |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
L<hitbtc|https://github.com/hitbtc-com/hitbtc-api> |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 AUTHOR |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Chylli C<< <chylli@binary.com> >> |
103
|
|
|
|
|
|
|
|