Search code examples
vbscriptrsscmdfeed

How to get RSS feed through VBS Script and display contents in CMD


Hello there im trying to figure out how i can parse RSS feeds with VBS and display the contents into cmd. I got some code i have found around the net. this is what i got so far.

Batchfile:

:news
start scripts\xml\getxml.exe -N --directory-prefix=%temp% http://feeds.bbci.co.uk/news/rss.xml
:newscheck
if NOT EXIST %temp%\rss.xml (
ping 123.45.67.89 -n 1 -w 500 > nul.
goto newscheck
)
start scripts\news\parsebbcnews.vbs
ping 123.45.67.89 -n 1 -w 500 > nul.
:newsxmlparsecheck
if NOT EXIST %temp%\bbcnews.txt (
ping 123.45.67.89 -n 1 -w 500 > nul.
goto newsxmlparsecheck
)
set /p headline= <%temp%\bbcnews.txt
echo %headline%
%speech% "%headline%"
del %temp%\rss.xml
del %temp%\bbcnews.txt
goto start

then this starts the VBS:

    Dim xmlDoc, objNodeList, plot

Set wshShell = CreateObject( "WScript.Shell" )
tfolder =  wshShell.ExpandEnvironmentStrings("%TEMP%")
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.load(tfolder & "\rss.xml")
Set objNodeList = xmlDoc.getElementsByTagName("channel/item/description") 'Node to search for
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Write all found results into forecast.txt
Const ForWriting = 2
Set objTextFile = objFSO.OpenTextFile _
    (tfolder & "\bbcnews.txt", ForWriting, True)
If objNodeList.length > 0 then
For each x in objNodeList
plot=x.Text
objTextFile.WriteLine(plot)
Next 'just remove this?
objTextFile.Close   
End If

'Extract todays data (first line) from 'forecast.txt' and write each data type to seperate line in today.txt
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    (tfolder & "\bbcnews.txt", ForReading)
    strNextLine = objTextFile.Readline
    'currentsplit = Split(strNextLine , ", ")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(tfolder & "\headline.txt", ForWriting, True)  
    objTextFile.WriteLine(strNextLine)

This gets 1 news rss feed from bbc. I want also to get more than one feed displayed.

I did not create this code so i don't know how i can get the script and the similar code, to get rss from different sites. So basically what i want is to know how i can do this with a vb-script and display it in cmd.


Solution

  • If you're only interested in the top headline, you should use "channel/item/title" instead of "channel/item/description". With many feeds the description is enriched with HTML, so printing the first line of that probably won't do you any good.

    You could simplify both scripts to something like this:

    Sub PrintTopHeadline(feed)
      Set req = CreateObject("MSXML2.XMLHTTP.3.0")
      req.Open "GET", feed, False
      req.Send
    
      Set xml = CreateObject("Msxml2.DOMDocument")
      xml.loadXml(req.responseText)
      WScript.StdOut.WriteLine xml.getElementsByTagName("channel/item/title")(0).Text
    End Sub
    
    PrintTopHeadline "http://feeds.bbci.co.uk/news/rss.xml"
    PrintTopHeadline "http://news.google.com/news?ned=us&topic=h&output=rss"
    ...
    

    You'd have to call the script with cscript.exe, though, to be able to print commandline output:

    cscript //NoLogo feeds.vbs
    

    Edit: For displaying more headlines from a feed you'd add a loop to PrintTopHeadline(), something like this:

    For i = 0 To 4
      WScript.StdOut.WriteLine xml.getElementsByTagName("channel/item/title")(i).Text
    Next
    

    HTML tags can be removed by something like this:

    descr = xml.getElementsByTagName("channel/item/description")(0).Text
    
    Set re = New RegExp
    re.Pattern = "\s*<.+?>\s*"
    re.Global  = True
    
    descr = Trim(re.Replace(descr, " "))
    

    However, you'd need additional code to convert HTML entities back to plain text, e.g.:

    descr = Replace(descr, "&quot;", """")
    descr = Replace(descr, "&ntilde;", "ñ")
    ...