{"id":222,"date":"2010-07-28T20:41:18","date_gmt":"2010-07-29T00:41:18","guid":{"rendered":"http:\/\/www.xavignu.com\/?p=222"},"modified":"2010-07-28T20:41:18","modified_gmt":"2010-07-29T00:41:18","slug":"check-your-stocks-with-perl","status":"publish","type":"post","link":"https:\/\/www.xavignu.com\/?p=222","title":{"rendered":"Check your stocks with Perl"},"content":{"rendered":"<p>I wrote this small script to check the value of stocks with <a href=\"http:\/\/www.perl.org\" target=\"_blank\">Perl<\/a>. It doesn&#8217;t shows the value of the stock in the pre and post market, but it does its job.<\/p>\n<pre>\r\n     1  #!\/usr\/bin\/perl -w\r\n     2\r\n     3  use strict;\r\n     4  # Loading modules with use\r\n     5  use LWP::UserAgent;\r\n     6  use HTTP::Request;\r\n     7  use HTML::Strip;\r\n     8\r\n     9  my $length = $#ARGV + 1;\r\n    10  my $stock;\r\n    11\r\n    12  if ($length <= 0) {\r\n    13          print \"Usage: .\/stock.pl ticker1 ticker2 .. tickern\\n\";\r\n    14          exit 1 }\r\n    15\r\n    16  my $ua = LWP::UserAgent->new;\r\n    17  #$ua->agent(\"Mozilla\/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)\");\r\n    18  $ua->agent(\"Perl Script checking stock value\");\r\n    19\r\n    20  # Getting Google finance site website\r\n    21  foreach $stock (0 .. $#ARGV) {\r\n    22  my $url = \"http:\/\/www.google.com\/finance?q=$ARGV[$stock]\";\r\n    23  my $req = HTTP::Request->new(GET => $url);\r\n    24  my $response = $ua->request($req);\r\n    25  my $content = $response->content();\r\n    26\r\n    27  # Splitting the HTML code previously requested\r\n    28  my @values = split(' ', $content);\r\n    29  my @valor = grep(\/ref_(\\d+)_l\/, @values);\r\n    30\r\n    31  foreach my $val (@valor) {\r\n    32          # Using HTML::Strip to clean HTML tag\r\n    33          my $hs = HTML::Strip->new();\r\n    34          my $clean_text = $hs->parse( $val );\r\n    35          $hs->eof;\r\n    36          # Using split to remove > \r\n    37          my @value_2 = split(\/\\>\/, $clean_text);\r\n    38                  # Printing stock value after removing >\r\n    39                  print \"$ARGV[$stock] price is $value_2[1]\\n\"; \r\n    40    }\r\n    41  }\r\n<\/pre>\n<p>Here is an usage example.<\/p>\n<pre>\r\nxavi@liberdade:\/tmp$ .\/stockparse.pl ge cat\r\nge price is 16.05\r\ncat price is 69.72\r\nxavi@liberdade:\/tmp$ \r\n<\/pre>\n<p>Suggestions are always welcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wrote this small script to check the value of stocks with Perl. It doesn&#8217;t shows the value of the stock in the pre and post market, but it does its job. 1 #!\/usr\/bin\/perl -w 2 3 use strict; 4 # Loading modules with use 5 use LWP::UserAgent; 6 use HTTP::Request; 7 use HTML::Strip; 8 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[3],"tags":[44,6,23,50,70,42],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/pTQgt-3A","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/222"}],"collection":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=222"}],"version-history":[{"count":0,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/222\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}