Always A-HEAD, By being ahead you are always master of time

Hits

BOOKS

Friday, December 29, 2006

PowerShell RSS Reader

$oIE=new-object -com internetexplorer.application
$oIE.navigate2("About:blank")
while ($oIE.busy) {
sleep -milliseconds 50
}
#$oIE.visible=$true

$feed=[xml](new-object system.net.webclient).downloadstring("http://www.rediff.com/rss/newsrss.xml")

#$feed=[xml]$(get-content C:\Preetam\Money.xml)
$results=$feed.rss.channel.item Select-Object TITLE,DESCRIPTION ConvertTo-Html
$oDocBody=$oIE.document.documentelement.lastchild ;
#populate the document.body
$oDocBody.innerhtml=$results
$oDocBody.style.font="10pt Arial";
$oIE.document.bgcolor="#D7D7EA"
#Reading back from IE.
$oTBody=@($oIE.document.getElementsByTagName(">] ;
foreach ($oRow in $oTBody.childNodes)
{$oRow.bgColor="#AAAAAA" ;}
#Prepare a title.
$oTitle=$oIE.document.createElement("P")
$oTitle.style.font="bold 20pt Arial"
$oTitle.innerhtml="PowerShell NEWS Reader";
$oTitle.align="center" ;
#Display the title before the Table object.
$oTable=@($oIE.document.getElementsByTagName(">] ;
$oDocBody.insertBefore($oTitle,$oTable) > $null;

#$line=$oIE.document.createTextNode("MADEND")
#$Para=$oIE.document.createElement("HR")
#$oDocBody.appendchild($Para)
#$oDocBody.appendchild($Para)
#$oDocBody.appendchild($line)

#--------------------------------------------------------------

$feed01=[xml](new-object system.net.webclient).downloadstring("http://www.rediff.com/rss/moneyrss.xml")
$results01=$feed01.rss.channel.item Select-Object TITLE,DESCRIPTION ConvertTo-Html
$oDocBody=$oIE.document.documentelement.lastchild.firstchild ;
#populate the document.body
$oDocBody.innerhtml=$results01
$oDocBody.style.font="10pt Arial";
$oIE.document.bgcolor="#D7D7EA"
#Reading back from IE.
$oTBody=@($oIE.document.getElementsByTagName(">] ;
foreach ($oRow in $oTBody.childNodes)
{
$oRow.bgColor="#336600" ;

}
#Prepare a title.
$oTitle=$oIE.document.createElement("P")
$oTitle.style.font="bold 20pt Arial"
$oTitle.innerhtml="PowerShell NEWS Reader";
$oTitle.align="center" ;
#Display the title before the Table object.
$oTable=@($oIE.document.getElementsByTagName(">] ;
$oDocBody.insertBefore($oTitle,$oTable) > $null;
$oIE.visible=$true



Before I begin, what this script does, let me thank three people over here.

Scott Hansell --------> http://www.hanselman.com/blog : For giving such superb presentation on parsing XML via PowerShell ..worth watching...

Website Brainjar --------> http://www.brainjar.com/dhtml/intro/default2.asp This site tells us how to parse HTML tags; I’ was absolutely dumb about it before I visited it.

PowerShell Blos --------> Yuksel Akinci http://blogs.msdn.com/powershell/archive/2006/09/10/748883.aspx This where I got the hint of parsing and formatting html output.

Disclaimer: This is no way like a RSS reader, as you get on internet, it is just explains latent potential lies in Powershell to unleash power of XML, Non-programmer like me it has been very simple to prove it. Of course code can be made much more complex get our favourite RSS reader formatted in our own way. And there is already something like this on Wiki (http://en.wikipedia.org/wiki/Windows_PowerShell)

Code is nothing if you know DOM (document object modelling).

$feed=[xml](new-object system.net.webclient).downloadstring(http://www.rediff.com/rss/newsrss.xml)

I have pulled two separate XML file from the internet and pasted into HTML document using DOM. To do this I have to typecast which means to convert thing specifically into XML;if this is missing it is normal HTML document.

After that I have converted them into html format

$results=$feed.rss.channel.item Select-Object TITLE,DESCRIPTION ConvertTo-Html

After this everything is about formatting HTML in way it looks as attractive HTML page

$oDocBody=$oIE.document.documentelement.lastchild.firstchild ;

Above line is important since this line actually pulls the second link and drops it in first child of last child, it can get tricky if you more xml links.

Lastly how to run it, you will need to change ("http://www.rediff.com/rss/newsrss.xml") and get your favourite XML link, of course one you have it you don’t need to change everytime. Atleast I’ve made provision for two RSS links, further can be made easily

Technorati tags: , ,

del.icio.us tags: ,


No comments: