Advertisement
mikelieman

DJIA Comparison

Mar 14th, 2020
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.27 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use v5.12;
  6.  
  7. use Cwd qw(abs_path);
  8. use FindBin;
  9. use lib abs_path(qq{$FindBin::Bin});
  10.  
  11. use Time::HiRes qw( gettimeofday tv_interval );
  12. my $t0 = [ gettimeofday ];
  13.  
  14. #  Start logging
  15. use Log::Log4perl qw(:easy);
  16. binmode( STDOUT, ":utf8" );
  17. use Log::Log4perl::CommandLine ( ':all', ':logconfig', abs_path(qq{$FindBin::Bin/logging.cfg}) );
  18.  
  19. INFO("Begin Program");
  20. DEBUG(qq{Logging configuration from $FindBin::Bin/logging.cfg});
  21. DEBUG("Debug flag turned on");
  22. TRACE("Trace flag turned on");
  23.  
  24. #
  25. # Do Stuff
  26. #
  27.  
  28. use Mojo::UserAgent;
  29.  
  30. my $quoteurl = q{https://in.finance.yahoo.com/quote/^DJI}; # <div id="quote-header-info"
  31.  
  32. # Fresh user agent
  33. my $ua = Mojo::UserAgent->new;
  34. my $res = $ua->get($quoteurl)->result;
  35.  
  36. my $target = q{div#quote-header-info.quote-header-section.Cf.Pos(r).Mb(5px).Maw($maxModuleWidth).Miw($minGridWidth).smartphone_Miw(ini).Miw(ini)!--tab768.Miw(ini)!--tab1024.Mstart(a).Mend(a).Px(20px).smartphone_Pb(0px).smartphone_Mb(0px)};
  37.  
  38. my @text;
  39.  
  40. foreach my $item ( $res->dom->find($target)->each) {
  41.  
  42.     TRACE(q{item - TOP of item loop: } . $item);
  43.    
  44.     for my $n ($item->descendant_nodes->each) {
  45.         TRACE(q{descendant - TOP of descendant loop});
  46.    
  47.         if ($n->type eq 'text') {
  48.             DEBUG(q{text: } . $n->content);
  49.             push @text, $n->content;
  50.         }
  51.  
  52.         TRACE(q{descendant - BOTTOM of descendant loop});
  53.  
  54.     } # for my $n
  55.  
  56.     TRACE(q{item - BOTTOM of item loop: });
  57.  
  58. } # foreach my $item
  59.  
  60. my $idx = 0;
  61. foreach my $line (@text) {
  62.     DEBUG(qq{$idx  $line});
  63.     $idx++;
  64. }
  65.  
  66. #
  67. # Output
  68. #
  69. my ($label, undef, $trump_current, $ts) = @text;
  70.  
  71. $trump_current =~ s/,//;
  72. my $obama_end = 19813.55;
  73. my $diff = $trump_current - $obama_end;
  74.  
  75. my $outline = sprintf(qq{%s}, $label);
  76. say qq{\n\t} . $outline;
  77. INFO $outline;
  78.  
  79. $outline = sprintf(qq{%5s    %6.2f (%s)}, q{Trump}, $trump_current, $text[4]);
  80. say qq{\n\t} . $outline;
  81. INFO $outline;
  82.  
  83. $outline = sprintf(qq{%5s    %6.2f (%s)}, q{Obama}, $obama_end, q{last day in office});
  84. say qq{\t} . $outline;
  85. INFO $outline;
  86.  
  87. $outline = sprintf(qq{%5s    %s-%s},      q{-----}, q{--------}, q{-----------------------});
  88. say qq{\t} . $outline;
  89. INFO $outline;
  90.  
  91. $outline = sprintf(qq{%5s    % 6.2f}, q{Diff}, ( $diff ));
  92. say qq{\t} . $outline;
  93. INFO $outline;
  94.  
  95. say qq{\n};
  96.  
  97. # Exit
  98. INFO sprintf (q{End Program - ET: %02.2f seconds}, tv_interval($t0));
  99.  
  100. exit;
  101. __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement