App-bovespa

 view release on metacpan or  search on metacpan

lib/App/bovespa.pm  view on Meta::CPAN

use strict;
use warnings;
package App::bovespa;

use LWP::UserAgent;
use JSON;

my $agent_string = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0";

sub new {
    my ( $class ) = @_;

    bless {
    }, $class;
}

sub stock {
    my ( $self, $stock ) = @_;

    return $self->yahoo( $stock );

}

sub yahoo {
    my ( $self, $stock ) = @_;

    my $url = "https://query.yahooapis.com/v1/public/yql";

    #$stock = uc $stock . ".sa";
    $stock = uc $stock;
    my $yahoo_query = "select * from yahoo.finance.quotes where symbol in ( \"$stock\" )";

    my $ua = LWP::UserAgent->new();
    $ua->ssl_opts( verify_hostname => 0 );
    $ua->timeout( 10 );

    $DB::single = 1;

    my %url_params = (
        q           => $yahoo_query,
        format      => "json",
        env         => "store://datatables.org/alltableswithkeys", 
        callback    => undef,
    );


    my $query_params = "?";
    while ( my ( $key, $value ) = each( %url_params ) ) {
        $query_params .=  "$key=" . ( $value ? $value : '' ). "\&";
    }

    my $response = $ua->get( $url . $query_params );

    my $raw_html;
    if ( $response->is_success ){
        $raw_html = $response->decoded_content;
    }else{
        die;
    }

    my $data = decode_json $raw_html;
    return $data->{ query }{ results }{ quote }{ Bid };

}

=head1 NAME

App::bovespa - Simple tool to follow up your stocks at Bovespa Stock Exchange

=head1 VERSION

Version 0.003

=head1 SYNOPSIS

This module is a crawler to get the cotation of the Bovespa Stock Exchange. Now
it gets information one of the main portals.

This module is just for follow up, really, nothing realtime, nothing too serious.
I wrote it to keep my eyes on my own stocks. It is easier run a script that login
on my homebroker agent, with tokens and etc just to see the cotation.

To get a stock cotation is plain simple:

    use App::bovespa;

    my $exchange = App::bovespa->new();
    my $cotation = $exchange->stock( "PETR4.SA" );

Also inside this distribution, comes a small tool called bovespa_report, it has
a wrapper for cotation and a parser for a file with a list of cotations and prices to
compare.

    bovespa_report --help



( run in 1.744 second using v1.01-cache-2.11-cpan-5837b0d9d2c )