File Coverage

blib/lib/App/bovespa.pm
Criterion Covered Total %
statement 26 32 81.2
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod 1 3 33.3
total 35 44 79.5


line stmt bran cond sub pod time code
1 1     1   546 use strict;
  1         2  
  1         31  
2 1     1   3 use warnings;
  1         1  
  1         30  
3             package App::bovespa;
4              
5 1     1   651 use HTML::TreeBuilder;
  1         23143  
  1         11  
6 1     1   767 use LWP::UserAgent;
  1         31447  
  1         244  
7              
8             # TODO = multiple providers
9             #my $url = "https://br.financas.yahoo.com/q?s=PETR4.SA";
10              
11             my $agent_string = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0";
12              
13             sub new {
14 1     1 0 311 my ( $class ) = @_;
15              
16 1         4 bless {
17             }, $class;
18             }
19              
20             sub stock {
21 1     1 1 311 my ( $self, $stock ) = @_;
22              
23 1         4 return $self->yahoo( $stock );
24              
25             }
26              
27             sub yahoo {
28 1     1 0 1 my ( $self, $stock ) = @_;
29              
30 1         1 my $url = "https://br.financas.yahoo.com/q?s=";
31 1         3 $stock = uc $stock . ".sa";
32              
33 1         4 my $ua = LWP::UserAgent->new();
34 1         2140 $ua->ssl_opts( verify_hostname => 0 );
35 1         18 $ua->timeout( 10 );
36              
37 1         15 my $response = $ua->get( $url . $stock );
38 1         9475 my $raw_html;
39 1 50       4 if ( $response->is_success ){
40 0         0 $raw_html = $response->decoded_content;
41             }else{
42 1         10 die $response->status_line;
43             }
44              
45 0           my $tree = HTML::TreeBuilder->new();
46 0           $tree->parse( $raw_html );
47              
48 0           for ( $tree->find_by_attribute('id', "yfs_l84_". lc $stock )){
49 0           return join "", grep { ref $_ ne "SCALAR" } $_->content_list;
  0            
50             }
51             }
52              
53             =head1 NAME
54              
55             App::bovespa - Simple tool to follow up your stocks at Bovespa Stock Exchange
56              
57             =head1 VERSION
58              
59             Version 0.001
60              
61             =head1 SYNOPSIS
62              
63             This module is a crawler to get the cotation of the Bovespa Stock Exchange. Now
64             it gets information one of the main portals.
65              
66             This module is just for follow up, really, nothing realtime, nothing too serious.
67             I wrote it to keep my eyes on my own stocks. It is easier run a script that login
68             on my homebroker agent, with tokens and etc just to see the cotation.
69              
70             To get a stock cotation is plain simple:
71              
72             use App::bovespa;
73              
74             my $exchange = App::bovespa->new();
75             my $cotation = $exchange->stock( "PETR4" );
76              
77             Also inside this distribution, comes a small tool called bovespa_report, it has
78             a wrapper for cotation and a parser for a file with a list of cotations and prices to
79             compare.
80              
81             bovespa_report --help
82              
83             Will display all options related with the tool
84              
85             =head1 SUBROUTINES/METHODS
86              
87             =head2 stock
88              
89             Receives a stock name and returns it cotation. The name must be four letters and the type digit.
90             Like "PETR4" or "PETR3"
91              
92             =head1 AUTHOR
93              
94             RECSKY, C<< recsky at cpan.org >>
95              
96             =head1 BUGS
97              
98             In case of one of providers change it page or something like that, the lib may broke. In this case
99             contact me at recsky@cpan.org
100              
101             Or at irc at irc.perl.org
102              
103             #sao-paulo.pm
104              
105             =head1 LICENSE AND COPYRIGHT
106              
107             Copyright 2015 RECSKY
108              
109             This program is free software; you can redistribute it and/or modify it
110             under the terms of the the Artistic License (2.0). You may obtain a
111             copy of the full license at:
112              
113             L
114             =cut
115              
116             1;