<commit_before>#########################################
## <NAME>
## May 13, 2017
## Perl 6 module use to easily interface with the U.S. Naval Observatory's Astronomical Applications API.
## Currently based on the 2.0.1 version of the API.
use v6;

unit class API::USNavalObservatory;
use HTTP::UserAgent;
use URI::Encode;
use WWW;
use URI;

my $baseURL = 'api.usno.navy.mil/';
## Dont think I need this ether: my @validEras = "AD", "CE", "BC", "BCE";
my $apiID = 'P6mod'; # Default ID, feel free to use an ID of your own and  override.
my $outputDir = $*CWD; # Current working Dir is the default output dir for images
my $webAgent = HTTP::UserAgent.new();
## Removing this because I don't think I need it, but will keep it around for now: my $encoded = URI::Encode.new();

subset SolarEclipses-YEAR of UInt where * eq any( 1800..2050 );
subset ValidEras of Str where * eq any( "AD", "CE", "BC", "BCE" );
subset ValidJulian of UInt where * < 5373484.5;
subset ObserChristan of UInt where * eq any( 1583..9999 );
subset ObserJewish of UInt where * eq any( 622..9999 );
subset ObserIslamic of UInt where * eq any( 360..9999 );
subset Body of Str where * eq any( "mercury", "venus", "venus-radar", "mars", "jupiter", "moon", "io", "europa", "ganymede", "callisto" );
subset Height of Int where * eq any ( -90..10999 );
subset Format of Str where * eq any( "json", "gojson" );
subset MoonPhase of UInt where * eq any( 1..99 );
subset View of Str where * eq any( "moon", "sun", "north", "south", "east", "west", "rise", "set" );

my regex coords { \-? \d+[\.\d+]? [N|S]? \,?\s? \-? \d+[\.\d+]? [E|W]? };
my regex loc { ['St.' || <alpha> ]? \s? <alpha>+ \, \s \w**2 };

###########################################
## getJSON - method used to make request which will return JSON formatted data.
method !getJSON( $template ) {
say $template;

my $test = "http://api.usno.navy.mil/eclipses/solar?date=7/2/2019&coords=46.67N, 1.48E&height=176&format=json";
#say $test;
#my URI $URI .= new( "https://" ~ $baseURL ~ $template ~ "&id={ $apiID }");
my URI $URI .= new( $test );

# exit;

#my $URI = uri_encode( $baseURL ~ $template ~ "&id={ $apiID }" );
#my $URI = uri_encode( $test );
  #say $URI; ## Used for testing URI bug.

  say "Sending: " ~ $URI;
  #my $response = get $URI;
#exit;
  my $response = $webAgent.get( $URI );
  if $response.is-success {
    return $response.content;
    }
    else {
      return $response.status-line;
  }
}
###########################################
## getIMG - method used to make request which will return .png files.
## TODO: change the default location of the base directory
method !getIMG( :$name, :$template ){
  my $file = $outputDir ~ "/"~ $name ~ ".png";
  my $url = $baseURL ~ $template;
  say "Saving to $file ";
  $file.IO.spurt: :bin, get $url;
  say "{($file.path.s / 1024).fmt("%.1f")} KB received";
}
<commit_msg>Added Cro and replaced HTTP::Useragent<commit_after>#########################################
## <NAME>
## May 13, 2017
## Perl 6 module use to easily interface with the U.S. Naval Observatory's Astronomical Applications API.
## Currently based on the 2.2.0 version of the API.
use v6;

unit class API::USNavalObservatory;
use URI;
use Cro::HTTP::Client;
use JSON::Pretty;

my $baseURL = 'api.usno.navy.mil/';
## Dont think I need this ether: my @validEras = "AD", "CE", "BC", "BCE";
my $apiID = 'P6mod'; # Default ID, feel free to use an ID of your own and  override.
my $outputDir = $*CWD; # Current working Dir is the default output dir for images
# my $webAgent = HTTP::UserAgent.new();
## Removing this because I don't think I need it, but will keep it around for now: my $encoded = URI::Encode.new();

subset SolarEclipses-YEAR of UInt where * eq any( 1800..2050 );
subset ValidEras of Str where * eq any( "AD", "CE", "BC", "BCE" );
subset ValidJulian of UInt where * < 5373484.5;
subset ObserChristan of UInt where * eq any( 1583..9999 );
subset ObserJewish of UInt where * eq any( 622..9999 );
subset ObserIslamic of UInt where * eq any( 360..9999 );
subset Body of Str where * eq any( "mercury", "venus", "venus-radar", "mars", "jupiter", "moon", "io", "europa", "ganymede", "callisto" );
subset Height of Int where * eq any ( -90..10999 );
subset Format of Str where * eq any( "json", "gojson" );
subset MoonPhase of UInt where * eq any( 1..99 );
subset View of Str where * eq any( "moon", "sun", "north", "south", "east", "west", "rise", "set" );

my regex coords { \-? \d+[\.\d+]? [N|S]? \,?\s? \-? \d+[\.\d+]? [E|W]? };
my regex loc { ['St.' || <alpha> ]? \s? <alpha>+ \, \s \w**2 };

###########################################
## getJSON - method used to make request which will return JSON formatted data.
method !getJSON( $template ) {
    my URI $URI .= new( "https://" ~ $baseURL ~ $template ~ "&id={ $apiID }");
    my $request = await Cro::HTTP::Client.get( $URI );
    my $response = await $request.body;
    return to-json $response;
}
###########################################
## getIMG - method used to make request which will return .png files.
## TODO: change the default location of the base directory
method !getIMG( :$name, :$template ){
  my $file = $outputDir ~ "/"~ $name ~ ".png";
  my $url = $baseURL ~ $template;
  say "Saving to $file ";
  $file.IO.spurt: :bin, get $url;
  say "{($file.path.s / 1024).fmt("%.1f")} KB received";
}

{'fraction_non_alphanumeric': 0.21681997371879105, 'fraction_numerical': 0.02778299230336024, 'mean_word_length': 3.9840972871842846, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '752459', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1824, 'n_words': 667}