# encoding: ascii
# api: powershell
# title: Factorio blog alert
# description: This script plays beep sound when there is new post at Factorio blog. Converted from JS original – https://gist.github.com/demipixel/1a5a42022c24d88de1308a934bd78246 by demipixel.
# version: 0.1
# type: function
# author: Xaegr
# license: CC0
# x-poshcode-id: 6407
# x-archived: 2016-06-29T10:16:36
# x-published: 2016-06-27T11:59:00
#
#
$AlertOnErrors = $false
$LastGid = '250329347071682810'
$Interval = 60 # Seconds
$URI = 'http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=427520&count=3&maxlength=300&format=json'

$client = New-Object System.Net.WebClient

function Check()
{
    try 
    {
        $data = $client.DownloadString($URI)
        $json = ConvertFrom-Json $data
        $currentGid = $json.appnews.newsitems[0].gid
    }
    catch
    {
        Write-Warning "Error: $_"
        if ($AlertOnErrors)
        {
            return $true
        }
    }
    return ($currentGid -ne $LastGid)
}

while (1)
{
    if (Check)
    {
        Write-Warning "ALERT!!!"
        [System.Console]::Beep(1000,1000)
    }
    
    sleep -Seconds $Interval
}
