App-Chart

 view release on metacpan or  search on metacpan

misc/t-yahoo.pl  view on Meta::CPAN

# with Chart.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;
use Data::Dumper;
use File::Slurp 'slurp';
use POSIX 'strftime';
use App::Chart::Yahoo;
use App::Chart::TZ;
use App::Chart::Download;
use Date::Calc;
use Date::Parse;
$|=1;

# uncomment this to run the ### lines
use Smart::Comments;

# https://www.lemonfool.co.uk/viewtopic.php?t=38834
# https://query1.finance.yahoo.com/v7/finance/download/SHEL.L?period1=1650396785&period2=1681932785&interval=1d&events=history&includeAdjustedClose=true
#
# https://query2.finance.yahoo.com/v1/finance/search?q=CSCO&enableFuzzyQuery=false
#   &newsCount=0
#
# https://www.intelligentinvestor.com.au/ipo/recent-floats
#   new ASX floats (so limited recent data)
#


{
  # json v7 latest parse

  my $filename = "$ENV{HOME}/chart/samples/yahoo/GXY-v7.json";
  $filename = "$ENV{HOME}/chart/samples/yahoo/NOSUCH-v7.json";
  $filename = "$ENV{HOME}/chart/samples/yahoo/GSPC-v7.json";
  $filename = "$ENV{HOME}/chart/samples/yahoo/RMX-v7.json";
  $filename = "$ENV{HOME}/chart/samples/yahoo/SCG-v7.json";
  $filename = "$ENV{HOME}/chart/samples/yahoo/WDC.AX-v7.json";
  $filename =~ m{/(.*)?-v7} or die;
  my $symbol = $1;
  require HTTP::Response;
  my $resp = HTTP::Response->new();
  my $content = slurp ($filename);
  $resp->content($content);
  $resp->content_type('text/html; charset=utf-8');
  my $h = App::Chart::Yahoo::latest_parse ($symbol, $resp);
  print Dumper(\$h);
  # App::Chart::Download::write_latest_group ($h);
  exit 0;
}
{
  # SCT.AX Scout Security, 1 old -> 100 new consolidation Aug 2024
  # As of September 2024, split appears 4 times
  # Think one only 5 Aug 24 price 0.52 up from 0.0052
  # 1718409600
  # 1719446400

  my $symbol = 'SCT.AX';
  $symbol = 'ORD.AX';
  my $lo_tdate = App::Chart::ymd_to_tdate_floor(2024,6,1);
  my $hi_tdate = App::Chart::ymd_to_tdate_floor(2024,9,1);
  my $lo_timet = App::Chart::Yahoo::tdate_to_unix($lo_tdate);
  my $hi_timet = App::Chart::Yahoo::tdate_to_unix($hi_tdate);

  my $url = "https://query1.finance.yahoo.com/v8/finance/chart/"
    . URI::Escape::uri_escape($symbol)
    ."?period1=$lo_timet"
    ."&period2=$hi_timet"
    ."&interval=1d"
    ."&events=". URI::Escape::uri_escape('div|split')
    ."&close=unadjusted";
  print "$url\n";

  require App::Chart::UserAgent;
  my $ua = App::Chart::UserAgent->instance;
  my $resp = $ua->get($url);

  my $content = $resp->decoded_content (raise_error => 1, charset => 'none');
  File::Slurp::write_file('/tmp/z', {err_mode=>'carp'}, $content);
  $content = $resp->decoded_content (raise_error => 1);
  my $decoded = JSON::from_json($content);
  print JSON->new->pretty->encode($decoded);
  exit 0;
}
{
  # v1 info parse
  my $filename = "$ENV{HOME}/chart/samples/yahoo/v1-info-CSCO.json";
  $filename = "$ENV{HOME}/chart/samples/yahoo/v1-info-ORD.AX.json";
  $filename =~ /v1-info-([A-Z.]+)\.json/ or die;
  my $symbol = $1;
  require HTTP::Response;
  my $resp = HTTP::Response->new(200,'OK');
  $resp->content(slurp($filename));
  $resp->content_type('application/json;charset=utf-8');
  my $h = App::Chart::Yahoo::info_parse ($symbol,$resp);
  print Dumper ($h);
#  App::Chart::Download::write_daily_group ($h);
  exit 0;
}

{
  # v8 parse

  my $filename = "$ENV{HOME}/chart/samples/yahoo/TSCO.L-2017.json";
  $filename = "$ENV{HOME}/chart/samples/yahoo/SCT.AX-split2024.json";
  $filename = "$ENV{HOME}/chart/samples/yahoo/BHP.AX-2024div.json";
  $filename = "$ENV{HOME}/chart/samples/yahoo/NKLA.MX-split.json";
  $filename =~ m{.*/(.*)-} or die;
  my $symbol = $1;
  my $content = slurp ($filename);
  require JSON;
  my $href = JSON::decode_json($content);
  #  ### $href
  my $result = $href->{'chart'}->{'result'}->[0];
  my $meta = $result->{'meta'};
  my $timestamps = $result->{'timestamp'};
  my $quote= $result->{'indicators'}->{'quote'}->[0];
  my $events = $result->{'events'};
  my $dividends = $events->{'dividends'};
  my $splits = $events->{'splits'};
  ### $meta
  ### $quote
  ### $timestamps

misc/t-yahoo.pl  view on Meta::CPAN

  # symbol
  # priceHint
  # exchangeName
  # currency
  # gmtoffset

  require HTTP::Response;
  my $resp = HTTP::Response->new();
  $resp->content($content);
  my $h = App::Chart::Yahoo::daily_parse ($symbol, $resp);
  App::Chart::Download::crunch_h ($h);
  print Dumper(\$h);
  exit 0;
}
{
  # cookie
  # cf Finance::Quote::YahooJSON

  # https://www.yahoo.com/
  # Set-Cookie: RRC=st=1725958300186.3&cnt=1; path=/; domain=.www.yahoo.com; HttpOnly
  # Redirect to https://au.yahoo.com/?p=us
  #
  # https://finance.yahoo.com/quote/IBM/history/?p=IBM
  # no cookie

  my $url = 'https://www.yahoo.com/';
  $url = 'https://finance.yahoo.com';
  $url = 'https://finance.yahoo.com/quote/IBM/history/?p=IBM';

  $App::Chart::option{'verbose'} = 2;
  require HTTP::Cookies;
  my $jar = HTTP::Cookies->new;
  my $user_agent = 'Mozilla/5.0';
  my $resp = App::Chart::Download->get
    ($url,
     user_agent => $user_agent,
     cookie_jar => $jar,
     redirect_ok => 0);
  ### headers: $resp->headers->as_string
  print "jar string: ",$jar->as_string,"\n";
  print "content:\n";
  my $content = $resp->decoded_content (raise_error => 1, charset => 'none');
  print $content,"\n";
  exit 0;
}
{
  $App::Chart::option{'verbose'} = 2;
  my $h = App::Chart::Yahoo::cookie_and_crumb_data();
  print Dumper($h);
  exit 0;
}

{
  # v8 dividends ahead
  # https://www.intelligentinvestor.com.au/investment-tools/shares/dividends
  # https://query2.finance.yahoo.com/v8/finance/chart/IBM?formatted=true&lang=en-US&region=US&period1=1487372400&period2=1495058400&interval=1d&events=div%7Csplit&corsDomain=finance.yahoo.com

  my $symbol = 'NKLA.MX';
  $symbol = 'WBCPK.AX';
  my $tdate = App::Chart::Yahoo::daily_available_tdate ($symbol);
  my $lo_timet = App::Chart::Yahoo::tdate_to_unix($tdate - 30);
  my $hi_timet = App::Chart::Yahoo::tdate_to_unix($tdate + 100);

  my $url = "https://query1.finance.yahoo.com/v8/finance/chart/"
    . URI::Escape::uri_escape($symbol)
    ."?period1=$lo_timet"
    ."&period2=$hi_timet"
    ."&interval=1d"
    ."&events=". URI::Escape::uri_escape('div|split')
    ."&close=unadjusted";
  print "$url\n";

  require App::Chart::UserAgent;
  my $ua = App::Chart::UserAgent->instance;
  my $resp = $ua->get($url);

  my $content = $resp->decoded_content (raise_error => 1, charset => 'none');
  File::Slurp::write_file('/tmp/z', {err_mode=>'carp'}, $content);
  $content = $resp->decoded_content (raise_error => 1);
  print $content;

  exit 0;
}
{
  # v8 dates, as of June 2024
  my $t = 1504013400;  # IBM
  my $tz = "America/New_York";
  my $gmtoff = -14400;

  $t = 1504051200;  # BHP.AX
  $tz = "Australia/Sydney";
  $gmtoff = 36000;

  $t = 1503990000;  # TSCO.L
  $tz = "Europe/London";
  $gmtoff = 3600;

  my @t = gmtime($t);
  print join(',',@t),"\n";
  print "GMT     ",strftime('%H:%M:%S  %d %m %Y',@t),"\n";

  $ENV{TZ} = $tz;
  @t = localtime($t);
  print "local   ",strftime('%H:%M:%S  %d %m %Y',@t),"\n";

  @t = gmtime($t - $gmtoff);
  print "GMT off ",strftime('%H:%M:%S  %d %m %Y',@t),"\n";

  # 2024-06-17
  # 1718593328 - 2*86400 == 1718420528
  # 1718593328 + 2*86400 == 1718766128
  # https://query2.finance.yahoo.com/v8/finance/chart/TSCO.L?formatted=true&lang=en-US&region=US&period1=1718420528&period2=1718766128&interval=1d&events=div%7Csplit&corsDomain=finance.yahoo.com
  # "open":[43.04999923706055,42.63999938964844,42.77000045776367],
  # "low":[42.540000915527344,42.40999984741211,42.61000061035156],
  # "close":[42.540000915527344,42.79999923706055,42.7400016784668],
  # "volume":[7993163,11623663,6440310],
  # "high":[43.13999938964844,43.06999969482422,42.88999938964844]}],
  # "adjclose":[{"adjclose":[42.540000915527344,42.79999923706055,42.7400016784668]}]b
  foreach my $t (1718582400,1718668800,1718755200) {
    $ENV{TZ} = "Australia/Sydney";
    @t = localtime($t);
    print "stamp   ",strftime('%H:%M:%S  %d %m %Y',@t),"\n";



( run in 0.780 second using v1.01-cache-2.11-cpan-64ef6c95b5d )