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