Finance-Bitcoin-Feed

 view release on metacpan or  search on metacpan

lib/Finance/Bitcoin/Feed.pm  view on Meta::CPAN

package Finance::Bitcoin::Feed;

use strict;
use warnings;

use Mojo::Base 'Mojo::EventEmitter';
use AnyEvent;
use Module::Runtime qw(require_module);
use Carp;

use feature qw(say);

our $VERSION = '0.05';

has 'sites' => sub { [qw(Hitbtc BtcChina CoinSetter LakeBtc BitStamp)] };
has 'output' => sub {
    sub { shift; say join " ", @_ }
};

sub new {
    my $class = shift;
    my $self  = $class->SUPER::new(@_);
    $self->on('output', $self->output);
    return $self;
}

sub run {
    my $self = shift;

    my @sites;

    for my $site_class (@{$self->sites}) {
        $site_class = 'Finance::Bitcoin::Feed::Site::' . $site_class;
        eval { require_module($site_class) }
            || croak("No such module $site_class");
        my $site = $site_class->new;
        $site->on('output', sub { shift; $self->emit('output', @_) });
        $site->go;
        push @sites, $site;
    }

    AnyEvent->condvar->recv;
    return;
}

1;

__END__

=head1 NAME

Finance::Bitcoin::Feed - Collect bitcoin real-time price from many sites' streaming data source

=head1 SYNOPSIS

    use Finance::Bitcoin::Feed;

    #default output is to print to the stdout
    Finance::Bitcoin::Feed->new->run();
    # will print output to the stdout:
    # BITSTAMP BTCUSD 123.00
    

    #or custom your stdout
    open  my $fh, ">out.txt";
    $fh->autoflush();
    my $feed = Finance::Bitcoin::Feed->new(output => sub{
       my ($self, $site, $currency, $price) = @_;
       print $fh "the price currency $currency on site $site is $price\n";
    });
    # let's go!
    $feed->run();

    #you can also custom which site you want to connect
    Finance::Bitcoin::Feed->new(sites => [qw(LakeBtc)])->go;

=head1 DESCRIPTION

L<Finance::Bitcoin::Feed> is a bitcoin realtime data source which collect real time data source from these sites:

=over 4

=item * L<HitBtc|https://hitbtc.com/api#socketio>

=item * L<BtcChina|http://btcchina.org/websocket-api-market-data-documentation-en>

=item * L<CoinSetter|https://www.coinsetter.com/api/websockets/last>

=item * L<<lakebtc api|https://www.lakebtc.com/s/api>

=back

The default output format to the stdout by this format:

   site_name TIMESTAMP CURRENCY price

For example:

   COINSETTER 1418173081724 BTCUSD 123.00

The unit of timestamp is ms.

You can custom your output by listen on the event L<output> and modify the data it received.

Note the followiing sites doesn't give the timestamp. So the timestamp in the result will be 0:

LakeBtc

=head1 METHODS

This class inherits all methods from L<Mojo::EventEmitter>



( run in 1.307 second using v1.01-cache-2.11-cpan-140bd7fdf52 )