Skip to content

Instantly share code, notes, and snippets.

@bluemeda
Created August 28, 2019 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluemeda/560355fc9237bbaf734493fffa88f1a1 to your computer and use it in GitHub Desktop.
Save bluemeda/560355fc9237bbaf734493fffa88f1a1 to your computer and use it in GitHub Desktop.
A wise way to spend your remaining internet data.
param(
[parameter( ValueFromRemainingArguments = $true )]
[string[]]$args
)
function Show-Help {
Clear-Host
Write-Host
Write-Host "geget.ps1, Version 0.0.1"
Write-Host
Write-Host "A " -NoNewline
Write-Host "wise way " -ForegroundColor Green -NoNewline
Write-Host "to spend your remaining internet data." -NoNewline
Write-Host
Write-Host "Usage: " -NoNewline
Write-Host " ./GeGet.ps1 [-q|--quota] quota{M|G} " -ForegroundColor Green
Write-Host
Write-Host "Where: " -NoNewline
Write-Host " quota " -ForegroundColor Green -NoNewline
Write-Host "is either in GB (1G or 2.3G),"
Write-Host " or in MB (Must be greater or equal than 100.)."
Write-Host
Write-Host "Example: " -NoNewline
Write-Host "./GeGet.ps1 -q 2G" -ForegroundColor Green
Write-Host " ./GeGet.ps1 -q 1.5G" -ForegroundColor Green
Write-Host " ./GeGet.ps1 -q 700M" -ForegroundColor Green
Write-Host
Write-Host "Written by " -NoNewline
Write-Host "Apip" -ForegroundColor DarkGray
Write-Host "https://bluemeda.web.id" -ForegroundColor DarkBlue
Write-Host
Exit 1
}
function Calculate {
param (
[string]$quota
)
[float]$quota_num = $quota -replace ".$"
[string]$url = "https://sgp-ping.vultr.com/vultr.com.100MB.bin"
if ($quota -cmatch '.+[G]$') {
$quota_num = $quota_num * 1000
} elseif ($quota_num -lt 100) {
Show-Help
}
[int]$loop = $quota_num/100
$headers = @{
'Cache-Control' = 'no-cache'
}
for ($i = 0; $i -lt $loop; $i++) {
Invoke-RestMethod -Uri $url -Method Get -Headers $headers > $null
$totalData=($i+1)*100
$dataUnit="MB"
if ($totalData -ge 1000) {
$totalData /= 1000
$dataUnit="GB"
}
Write-Host "$($totalData)$($dataUnit)"
}
}
if ( $args.Length -gt 0 ) {
switch ($args[0]) {
{$_ -in "-q","--quota"} {
$quota = $args[1]
}
Default { Show-Help }
}
} else {
Show-Help
}
if ($quota -cmatch '^[0-9]+\.?[0-9]*[GM]$') {
Calculate($quota)
} else {
Show-Help
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment