Skip to content

Instantly share code, notes, and snippets.

@mark05e
Created March 9, 2020 04:28
Show Gist options
  • Save mark05e/57265d5bcbafa6328d4536f5da89f528 to your computer and use it in GitHub Desktop.
Save mark05e/57265d5bcbafa6328d4536f5da89f528 to your computer and use it in GitHub Desktop.
Scrape internet usage from webpage and notify using telegram
###########################################################
# Zain Kuwait Prepaid Internet Usage notifier
# Mark | March 2020
# Requires: noti (https://github.com/variadico/noti)
###########################################################
#User modifiable variables
$url ='https://www.kw.zain.com/en/web/myzain'
$noti_exe = 'C:\MarkTools\noti.exe'
$noti_config = 'C:\MarkTools\.noti.yml'
#Regex to scrap from url output
$PlanNumber = '<p style="color: grey" class="numberSpan"> <span class="bold">(?<PlanNumber>.*)</span> <a data-reveal-id="captchaconfirm" '
$PlanValidityDate = '<small><span class="bold">Plan is valid until</span>(?<PlanValidityDate>.*)</small> </div>'
$AccountBalance = '<div class="circle"> <span class="kdspan" style="font-size: 15px;">(?<BalanceCurrency>.*)</span> <span class="bold startfromzeroprice">(?<BalanceAmount>.*)</span> </div>'
$PackageUsageSize = '<span class="bold larger-font startfromzero">(?<PackageUsageSize>.*)<\/span>(?<PackageUsageSizeMeasure>.*)<\/h4>'
$PackageTotalSize = '</div> </div> <p> out of <strong>(?<PackageTotalSize>.*)</strong> </p> </div> </div> </div>'
$MatchPattern = "$PlanNumber.*$AccountBalance.*$PlanValidityDate.*$PackageUsageSize.*$PackageTotalSize"
#Main Run
Write-Output "`n########## Starting script $($PSCommandPath) on [$($env:computername)] at $(get-date) ##########"
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$req = Invoke-WebRequest -Uri $url
if ($req.StatusCode -ne 200)
{ write-output "Something went wrong - cannot read the page!"
exit
}
Write-output "> Took $($stopwatch.Elapsed.Seconds)s to read."
$matchtest=$req -match $MatchPattern
if (!$matchtest)
{ write-output "Something changed on the webpage! "
exit
}
#Build message
$message = "> Number: $($matches.PlanNumber.Trim())`n"
$message += "> PackageUsage : $($matches.PackageUsageSize.Trim()) $($matches.PackageUsageSizeMeasure.Trim()) "
$message += "out of $($matches.PackageTotalSize.Trim()) $($matches.PackageUsageSizeMeasure.Trim())`n"
$message += "> BalanceAmount : $($matches.BalanceAmount.Trim()) $($matches.BalanceCurrency.Trim())`n"
$message += "> ExpiryDate : $($matches.PlanValidityDate.Trim())"
#output and notification
write-output $message
& $noti_exe --file $noti_config --telegram --message "$message"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment