Iris Classon
Iris Classon - In Love with Code

PowerShell: RSS feeds and credentials

I’m doing some Uni courses to get a BCS with an international profile and one of the tools we use for coordinating assignments, email, discussions and more is Fronter. I’m not a terrible big fan of the system, I used it for two years when I did my programming degree but I never got comfortable with it. The biggest problem is having one more place to log in (two actually, as we have a login one the uni site as well). There is an API, but admin level only. However, there is a RSS feed that supplies the information you request to be on the front page. Neat feature, and much appreciated.

I have been playing around with PowerShell to set up a custom RSS reader for all the news sites I follow,- and with some minor tweaks I can simply run a command to get the feed using my credentials.

There are many ways to go about this, and I’m certain mine can be simplified but I’ll share this nonetheless.

#
# Fronter.ps1
#
 
$username = "-"
$password = "-" | ConvertTo-SecureString -asPlainText -Force
$url = "https://fronter.com/ltu/rss/get_today_rss.php?elements=default&LANG=en"
 
$credentials = New-Object System.Management.Automation.PSCredential($username,$password)
 
$result = Invoke-WebRequest $url -Credential $credentials
 
[xml]$feed = $result.Content
 
$feed.rss.channel.item | % {
 
    $author = if($_.author) { $_.author} else { "admin"}
 
    write-host ("{0} by {1}" -f$_.title,$author) -foreground "yellow"
 
    if($_.pubdate){
        write-host ("Published on {0} `n" -f$_.pubdate) -foreground "green"
    }
}
  

The economist likes to send markup as a part of the summary/content, and a cheeky regex sort of takes hand of that. Not liking that solution frankly. Notice also that I’m using the .Net assemblies making sure that I clean up my resources.

   
#
# TheEconomistRss.ps1
#
 
Add-Type -AssemblyName System.ServiceModel
 
$result = Invoke-WebRequest "http://www.economist.com/sections/science-technology/rss.xml"
 
[xml]$xmlFeed = $result.Content
 
try {
    $stringReader = New-Object System.IO.Stringreader $result.Content
 
    $reader = [system.Xml.XmlReader]::Create($stringReader)
 
    $sFeed = [System.ServiceModel.Syndication.SyndicationFeed]::Load($reader)
 
    $sFeed.Items | % {
        Write-Host $_.Title.Text -foreground "yellow"
        Write-Host $_.PublishDate.DateTime.ToString() -foreground "green"
        Write-Host (($_.Summary.Text -replace '<.+?>','')+ "`n`n") -foreground "gray" 
    }
}
 
finally {
            $stringReader.Close()
            $reader.Close()
}

 

Comments

Leave a comment below, or by email.

Last modified on 2015-09-19

comments powered by Disqus