clearvariables()
$todaydate=($year + "-" + $month + "-" + $day)
$ready=0
$thenumber=1
$sortcounter=0
$arraycounter=0
$hazard=0
definearray($aname,20,"")
definearray($amag,20,"")
definearray($adiamin,20,"")
definearray($adiamax,20,"")
definearray($ahaz,20,"")
definearray($aclosedate,20,"")
definearray($aMPH,20,"")
definearray($amiles,20,"")
definearray($abad,20,"")
$url1 = "https://api.nasa.gov/neo/rest/v1/feed?start_date=" + $todaydate + "&end_date=" + $todaydate + "&api_key=3jMUBZIcgvFXVAMouSZqlWnbjg5N1sPmgsHiEyCD"
$VZFile ="C:\users\public\documents\test\test.txt"
$ModVZFile ="C:\users\public\documents\test\Modtest.txt"
# The full path and name of the HTML scrubber program.
$ScrubProg ="C:\users\public\documents\test\HTMLScrub.exe"
FileDelete($VZFile) #Clears the file and prevents adding to it needlessly
FileWrite($VZFile,HTTPGet($url1))
# FileWrite($VZFile,HTTPGet("http://www.spaceflightinsider.com/launch-schedule"))
FileReadClose($ModVZFile)
Sleep(2000)
Exec($Scrubprog) #Modifies $VZFile.txt and creates $VZModFile.txt
Sleep(2000) #May need to be adjusted depending on file size.
FileReadReset($ModVZFile)
FileReadClose($ModVZFile)
FileReadReset($ModVZFile)

$x = FileReadEnd($ModVZFile)

repeatuntil($x=1)

  $line = FileReadLine($ModVZFile)
  $field1 = Indexof($line,"name:")
  $field2 = Indexof($line,"absolute_magnitude_h: ")
  $field3 = IndexOf($line,"feet:")
  $field4 = indexof($line,"is_potentially_hazardous_asteroid: ")
  $field5 = indexof($line,"close_approach_date:")
  $field6 = Indexof($line,"miles_per_hour:")
  $field7 = indexof($line," miss_distance:")



  if ($field1 > -1)
    $field1a = Length($line)
    $name = (substring($line,$field1+5,$field1a-7))
  endif

  if ($field2 > -1)
    $field2a = Length($line)
    $mag = (substring($line,$field2+22,$field2a-24))
  endif

  if ($field3 > -1)
    $line = FileReadLine($ModVZFile)
    $field3a = IndexOf($line,"estimated_diameter_min: ")
    $diamin=(substring($line,$field3a+24,6))

    $line = FileReadLine($ModVZFile)
    $field3b = IndexOf($line,"estimated_diameter_max: ")
    $diamax=(substring($line,$field3b+24,6))

  endif

  if ($field4 > -1)
    $haz = substring($line,$field4+35,3)
  endif

  if ($field5 > -1)
    $field5a = indexof($line," ,")
    $closedate = substring($line,$field5+20,$field5a-21)

  endif

  if ($field6 > -1)

    $MPH = substring($line,$field6+15,7)

  endif

  if ($field7 > -1)
    $line = FileReadLine($ModVZFile)
    $line = FileReadLine($ModVZFile)
    $line = FileReadLine($ModVZFile)
    $line = FileReadLine($ModVZFile)
    $field7a = Length($line)
    $miles = (substring($line,7,$field7a-8))
    $ready = 1
    $field7 = -1

    # print($name)
    # print($mag)
    # print($diamin)
    # print($diamax)
    # print($haz)
    # print($Closedate)
    # print($MPH)
    # print($miles)
    # sleep(3000)
  endif

  if ($ready = 1 )

    $arraycounter=$arraycounter+1
    $aname[$arraycounter]=$name
    $amag[$arraycounter]=$mag
    $adiamin[$arraycounter]=$diamin
    $adiamax[$arraycounter]=$diamax
    $ahaz[$arraycounter]=$haz
    if ($ahaz[$arraycounter] = "fal")
      $hazard = $hazard
    ELSE
      $hazard = $hazard + 1
      $abad[$arraycounter]=$name
    endif
    $aclosedate[$arraycounter]=$closedate
    $aMPH[$arraycounter]=$MPH
    $amiles[$arraycounter]=$miles

    $ready=0

  endif

  $x = FileReadEnd($ModVZFile)



endrepeatuntil


filereadclose($MoDVZFile)

repeatuntil($sortcounter = $arraycounter)

  if ($amiles[$thenumber]< $amiles[$sortcounter+1])
    $thenumber=$thenumber

  ELSE
    $thenumber=$sortcounter+1
  endif
  $sortcounter=$sortcounter+1
endrepeatuntil




if ($hazard = 0)
  $hazardstatement =(" None of these encounters are rated as potentially hazardous")
ELSE
  $hazardstatement = ("with " + $hazard + " rated as potentially hazardous,")
endif

#print($arraycounter)
saywait(" " + " There are " + $arraycounter + " Near Earth Asteroid Fly buys " + $hazardstatement)
saywait(" ")
saywait(" The asteroid designated" + $aname[$thenumber] + " will safely pass by earth at a distance of " + round($amiles[$thenumber]) + " Miles.")
saywait("It is estimated to be between " + round($adiamin[$thenumber]) + " feet and " + round($adiamax[$thenumber])+ " feet in diameter and, is traveling At a speed of " + (round($aMPH[$thenumber])) + " MPH")

halt



