/ Published in: ASP
This script will retrieve a remote xml rss feed, modify it based upon a date extracted from a remote html document (using a regular expression), and save it to a local file.
Expand |
Embed | Plain Text
<% ' ASP (vbscript):Retrieve Remote XML RSS Feed, Modify, and then Save to Local XML File ' Author: Karl Horky ' Creation Date: Sept 30, 2009 ' Last Modified: Sept 30, 2009 ' ' ' ' ' Sample Input: ' ' <?xml version="1.0" encoding="UTF-8"?> ' ' <rss version="2.0"> ' ' <channel> ' ' <title>News</title> ' ' <link>http://www.news.com/</link> ' ' <description>The latest headlines</description> ' ' <language>en-us</language> ' ' <copyright>Copyright © 2009</copyright> ' ' <ttl>5</ttl> ' ' <item> ' ' <title>News Item 1</title> ' ' <description>Item Description 1</description> ' ' <link>http://www.news.com/news_release_1.htm</link> ' ' <pubDate>Tue, 29 Sep 2009 17:47:42 GMT</pubDate> ' ' </item> ' ' ' ' <item> ' ' <title>News Item 2</title> ' ' <description>Item Description 2</description> ' ' <link>http://www.news.com/news_release_2.htm</link> ' ' <pubDate>Mon, 24 Aug 2009 07:00:00 GMT</pubDate> ' ' </item> ' ' </channel> ' ' </rss> ' ' ' ' ' ' ' ' Sample Output file (note the changed date on first item): ' ' <rss version="2.0"> ' ' <channel> ' ' <title>News</title> ' ' <link>http://www.news.com/</link> ' ' <description>The latest headlines</description> ' ' <language>en-us</language> ' ' <copyright>Copyright © 2009</copyright> ' ' <ttl>5</ttl> ' ' <item> ' ' <title>News Item 1</title> ' ' <description>Item Description 1</description> ' ' <link>http://www.news.com/news_release_1.htm</link> ' ' <pubDate>Tue, 30 Sep 2009 12:21:49 GMT</pubDate> ' ' </item> ' ' ' ' <item> ' ' <title>News Item 2</title> ' ' <description>Item Description 2</description> ' ' <link>http://www.news.com/news_release_2.htm</link> ' ' <pubDate>Mon, 24 Aug 2009 07:00:00 GMT</pubDate> ' ' </item> ' ' </channel> ' ' </rss> ' ' ' Leading Zero function LdgZ(ByVal N) if (N>=0) and (N<10) then LdgZ = "0" & N else LdgZ = "" & N end function function GetNewsDate() NewsURL = "http://www.news.com/news_release_1.html" 'Create connection and get content Set NewsHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0") NewsHttp.Open "GET", NewsURL, false NewsHttp.Send() NewsContent = NewsHttp.ResponseText 'Match the Updated date paragraph Set re = new regexp re.Pattern = "<p class=MsoNormal align=center style='text-align:center'><b><span lang=EN-CA\s*style='mso-ansi-language:EN-CA;mso-fareast-language:EN-CA'>Update:\s*([^<]+)</span></b></p>" 'Set match to the Updated date paragraph Set reMatches = re.Execute(NewsContent) Set reMatch = reMatches(0) 'Strip out only the date from the paragraph NewsDate = re.Replace(reMatch, "$1") 'Clean up some objects Set re = nothing Set reMatches = nothing Set reMatch = nothing 'Strip out the PDT part of the date, make the date a standard format, and add 8 hours to the date 'to compensate for GMT Set re = new regexp re.Pattern = "^(\d*)(\s?[pa])\.m\.\sPDT,\s*(.*)" adjustedDate = DateAdd("h",8,re.Replace(NewsDate, "$3 $1$2m")) ' Format the date GetNewsDate = WeekdayName(Weekday(adjustedDate), 2) & ", " & Day(adjustedDate) & " " & MonthName(Month(adjustedDate),true) & " " & LdgZ(Hour(adjustedDate)) & ":" & LdgZ(Minute(adjustedDate)) & ":" & LdgZ(Second(adjustedDate)) & " GMT" ' Clean up some objects Set NewsHttp = Nothing Set re = nothing end function Response.Expires = -1 URLToRSS = "http://www.news.com/yourfeed.xml" ' Create HTTP request Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0") xmlHttp.Open "GET", URLToRSS, false xmlHttp.Send() RSSXML = xmlHttp.ResponseText ' Parse out XML DOM Set xmlDOM = Server.CreateObject("MSXML2.DomDocument.3.0") xmlDOM.async = False xmlDOM.validateOnParse = False xmlDOM.resolveExternals = False xmlFile = Server.MapPath("/location_to/yourlocalfeed.xml") ' If no errors occur parsing the xml, save the file If xmlDOM.LoadXml(RSSXML) Then xmlDOM.getElementsByTagName("pubDate")(0).childNodes(0).nodeValue = GetNewsDate() xmlDOM.Save(xmlFile) End If ' Clear variables Set xmlHttp = Nothing Set xmlDOM = Nothing %>
You need to login to post a comment.
