2007年1月10日 星期三

用VB.Net寫的RSS Feed產生函式

我自己是很喜歡用RSS的服務的人,所以寫網站或是做一些事時常會想到要弄個RSS Feed,最起碼自己會用的到~

不過VB中並沒有直接提供RSS Feed的產生函式,國外有些網站好像有提供類似的東西,不過不是要註冊就是有些複雜,所以我就自己寫了一個~

作法

我原本是用FileIO那篇的寫文字檔的方式,用比較笨的方法產生RSS Feed~

不過後來我改用以下的內容,將RSS的內容,以Xml.XmlDocument這個類別來產生XML的結構,順便學習在VB.Net下怎麼自己產生XML結構,並且它會自己將HTML編碼的問題解決

RSS 2.0的規格則是參考RSS 2.0 Specification中的規定,來設計RSS feed的元素,不過我剔除掉了一些我覺得沒用或是我看不出來功能的標籤,來產生結果

模組程式碼

Imports System
Module RSSGenModule
   ''' 
   ''' RSSGenerator V1.0 is builded by Allen
   ''' Please get the newest information at
   ''' http://allen080.blogspot.com/
   ''' 
   Public Class RSSGenerator
       Public Sub createRSS(ByRef rssfeed As RSSFeed, ByVal path As String)
           'RSS 2.0 specification is Reference at
           'http://www.rssboard.org/rss-specification/
           Dim x As New Xml.XmlDocument
           x.LoadXml("" + "" + "")

           Dim channel As Xml.XmlElement = x.CreateElement("channel")
           x.DocumentElement.AppendChild(channel)

           Dim element As Xml.XmlElement
           Dim subElement As Xml.XmlElement
           'set channel start
           element = x.CreateElement("title")
           channel.AppendChild(element)
           element.InnerText = rssfeed.title
           If Not rssfeed.link Is Nothing Then
               element = x.CreateElement("link")
               channel.AppendChild(element)
               element.InnerText = rssfeed.link
           End If
           If Not rssfeed.description Is Nothing Then
               element = x.CreateElement("description")
               channel.AppendChild(element)
               element.InnerText = rssfeed.description
           End If
           If Not rssfeed.language Is Nothing Then
               element = x.CreateElement("language")
               channel.AppendChild(element)
               element.InnerText = rssfeed.language
           End If
           If Not rssfeed.copyright Is Nothing Then
               element = x.CreateElement("copyright")
               channel.AppendChild(element)
               element.InnerText = rssfeed.copyright
           End If
           If Not rssfeed.managingEditor Is Nothing Then
               element = x.CreateElement("managingEditor")
               channel.AppendChild(element)
               element.InnerText = rssfeed.managingEditor
           End If

           If Not rssfeed.webMaster Is Nothing Then
               element = x.CreateElement("webMaster")
               channel.AppendChild(element)
               element.InnerText = rssfeed.webMaster
           End If

           element = x.CreateElement("pubDate")
           channel.AppendChild(element)
           element.InnerText = (rssfeed.pubDate.AddHours(-8)).ToString("r")

           element = x.CreateElement("lastBuildDate")
           channel.AppendChild(element)
           element.InnerText = (rssfeed.lastBuildDate.AddHours(-8)).ToString("r")

           If Not rssfeed.category Is Nothing Then
               element = x.CreateElement("category")
               channel.AppendChild(element)
               element.InnerText = rssfeed.category
           End If
           If Not rssfeed.generator Is Nothing Then
               element = x.CreateElement("generator")
               channel.AppendChild(element)
               element.InnerText = rssfeed.generator
           End If
           If Not rssfeed.docs Is Nothing Then
               element = x.CreateElement("docs")
               channel.AppendChild(element)
               element.InnerText = rssfeed.docs
           End If


           If Not rssfeed.image Is Nothing Then
               element = x.CreateElement("image")
               channel.AppendChild(element)
               'set image start
               If Not rssfeed.image.title Is Nothing Then
                   subElement = x.CreateElement("title")
                   element.AppendChild(subElement)
                   subElement.InnerText = rssfeed.image.title
               End If
               If Not rssfeed.image.link Is Nothing Then
                   subElement = x.CreateElement("link")
                   element.AppendChild(subElement)
                   subElement.InnerText = rssfeed.image.link
               End If
               subElement = x.CreateElement("url")
               element.AppendChild(subElement)
               subElement.InnerText = rssfeed.image.url
               If Not rssfeed.image.description Is Nothing Then
                   subElement = x.CreateElement("description")
                   element.AppendChild(subElement)
                   subElement.InnerText = rssfeed.image.description
               End If
               'set image end
           End If
           If Not rssfeed.itmes Is Nothing Then
               For Each item As RSSElement In rssfeed.itmes
                   element = x.CreateElement("item")
                   channel.AppendChild(element)
                   'set item start
                   subElement = x.CreateElement("title")
                   element.AppendChild(subElement)
                   subElement.InnerText = item.title
                   If Not item.link Is Nothing Then
                       subElement = x.CreateElement("link")
                       element.AppendChild(subElement)
                       subElement.InnerText = item.link
                   End If
                   If Not item.description Is Nothing Then
                       subElement = x.CreateElement("description")
                       element.AppendChild(subElement)
                       subElement.InnerText = item.description
                   End If
                   If Not item.author Is Nothing Then
                       subElement = x.CreateElement("author")
                       element.AppendChild(subElement)
                       subElement.InnerText = item.author
                   End If
                   If Not item.category Is Nothing Then
                       subElement = x.CreateElement("category")
                       element.AppendChild(subElement)
                       subElement.InnerText = item.category
                   End If
                   If Not item.comments Is Nothing Then
                       subElement = x.CreateElement("comments")
                       element.AppendChild(subElement)
                       subElement.InnerText = item.comments
                   End If
                   If Not item.guid Is Nothing Then
                       subElement = x.CreateElement("guid")
                       subElement.SetAttribute("isPermaLink", "false")
                       element.AppendChild(subElement)
                       subElement.InnerText = item.guid
                   End If

                   subElement = x.CreateElement("pubDate")
                   element.AppendChild(subElement)
                   subElement.InnerText = (item.pubDate.AddHours(-8)).ToString("r")
                   'set item end
               Next
           End If
           'set channel end

           'write to file
           Dim settings As New Xml.XmlWriterSettings()
           settings.Indent = True
           settings.IndentChars = "    "
           settings.Encoding = System.Text.Encoding.UTF8
           Using writer As Xml.XmlWriter = Xml.XmlWriter.Create(path, settings)
               x.WriteTo(writer)
               writer.Flush()
           End Using
       End Sub
   End Class

   Public Class RSSElement
       Private _title As String
       Private _link As String
       Private _description As String
       Private _author As String
       Private _category As String
       Private _comments As String
       Private _guid As String
       Private _pubDate As Date
       Public Property pubDate() As Date
           Get
               Return _pubDate
           End Get
           Set(ByVal value As Date)
               _pubDate = value
           End Set
       End Property

       Public Property guid() As String
           Get
               Return _guid
           End Get
           Set(ByVal value As String)
               _guid = value
           End Set
       End Property

       ''' 
       ''' 回應的URL
       ''' 
       Public Property comments() As String
           Get
               Return _comments
           End Get
           Set(ByVal value As String)
               _comments = value
           End Set
       End Property
       Public Property category() As String
           Get
               Return _category
           End Get
           Set(ByVal value As String)
               _category = value
           End Set
       End Property

       ''' 
       ''' E-Mail
       ''' 
       Public Property author() As String
           Get
               Return _author
           End Get
           Set(ByVal value As String)
               _author = value
           End Set
       End Property

       Public Property description() As String
           Get
               Return _description
           End Get
           Set(ByVal value As String)
               _description = value
           End Set
       End Property

       Public Property title() As String
           Get
               Return _title
           End Get
           Set(ByVal value As String)
               _title = value
           End Set
       End Property
       Public Property link() As String
           Get
               Return _link
           End Get
           Set(ByVal value As String)
               _link = value
           End Set
       End Property
   End Class

   Public Class RSSImage
       Private _title As String
       Private _link As String
       Private _url As String
       Private _description As String
       Public Property description() As String
           Get
               Return _description
           End Get
           Set(ByVal value As String)
               _description = value
           End Set
       End Property

       Public Property url() As String
           Get
               Return _url
           End Get
           Set(ByVal value As String)
               _url = value
           End Set
       End Property

       Public Property link() As String
           Get
               Return _link
           End Get
           Set(ByVal value As String)
               _link = value
           End Set
       End Property

       Public Property title() As String
           Get
               Return _title
           End Get
           Set(ByVal value As String)
               _title = value
           End Set
       End Property
   End Class

   Public Class RSSFeed
       Private _title As String
       Private _link As String
       Private _description As String
       Private _language As String = "zh-tw"
       Private _copyright As String
       Private _managingEditor As String
       Private _webMaster As String
       Private _guid As String
       Private _pubDate As Date = Now
       Private _lastBuildDate As Date = Now
       Private _category As String
       Private _generator As String
       Private _docs As String = "http://www.rssboard.org/rss-specification"
       Private _image As RSSImage
       Private _items As ArrayList

       ''' 
       ''' 元素型態是RSSElement
       ''' 
       Public Property itmes() As ArrayList
           Get
               Return _items
           End Get
           Set(ByVal value As ArrayList)
               _items = value
           End Set
       End Property
       Public Property image() As RSSImage
           Get
               Return _image
           End Get
           Set(ByVal value As RSSImage)
               _image = value
           End Set
       End Property

       ''' 
       ''' URL
       ''' 
       Public Property docs() As String
           Get
               Return _docs
           End Get
           Set(ByVal value As String)
               _docs = value
           End Set
       End Property

       Public Property generator() As String
           Get
               Return _generator
           End Get
           Set(ByVal value As String)
               _generator = value
           End Set
       End Property

       Public Property category() As String
           Get
               Return _category
           End Get
           Set(ByVal value As String)
               _category = value
           End Set
       End Property

       Public Property lastBuildDate() As Date
           Get
               Return _lastBuildDate
           End Get
           Set(ByVal value As Date)
               _lastBuildDate = value
           End Set
       End Property
       Public Property pubDate() As Date
           Get
               Return _pubDate
           End Get
           Set(ByVal value As Date)
               _pubDate = value
           End Set
       End Property

       Public Property guid() As String
           Get
               Return _guid
           End Get
           Set(ByVal value As String)
               _guid = value
           End Set
       End Property

       ''' 
       ''' E-Mail
       ''' 
       Public Property managingEditor() As String
           Get
               Return _managingEditor
           End Get
           Set(ByVal value As String)
               _managingEditor = value
           End Set
       End Property

       ''' 
       ''' E-Mail
       ''' 
       Public Property webMaster() As String
           Get
               Return _webMaster
           End Get
           Set(ByVal value As String)
               _webMaster = value
           End Set
       End Property

       Public Property copyright() As String
           Get
               Return _copyright
           End Get
           Set(ByVal value As String)
               _copyright = value
           End Set
       End Property

       Public Property language() As String
           Get
               Return _language
           End Get
           Set(ByVal value As String)
               _language = value
           End Set
       End Property

       Public Property description() As String
           Get
               Return _description
           End Get
           Set(ByVal value As String)
               _description = value
           End Set
       End Property

       Public Property title() As String
           Get
               Return _title
           End Get
           Set(ByVal value As String)
               _title = value
           End Set
       End Property
       Public Property link() As String
           Get
               Return _link
           End Get
           Set(ByVal value As String)
               _link = value
           End Set
       End Property
   End Class

End Module

使用方法

這個模組中主要有4個類別,分別是

  • RSSFeed:整個RSS的資料都存在這裡面,如果沒有被設定到的屬性,就不會被寫入檔案中(日期除外)
  • RSSImage:用來顯示Logo的資訊,如果沒有Logo就不要把它設定給RSSFeed就好了
  • RSSElement:用來存每一筆新資訊的地方,在RSSFeed中是被ArrayList存起來的
  • RSSGenerator:將路徑與RSSFeed交給其中的createRSS函式,就能產生一個RSS feed檔

使用範例

            Dim rssfeed As New RSSFeed
           rssfeed.title = title
           rssfeed.link = url
           rssfeed.description = title
           rssfeed.generator = "倫倫3號"
           rssfeed.webMaster = "allen080+Tech@gmail.com"
           Dim rssimage As New RSSImage
           rssfeed.image = rssimage
           rssimage.title = title
           rssimage.link = url
           rssimage.url = url + "images/fslogo.jpg"
           rssfeed.itmes = New ArrayList
           Dim notes As ArrayList = gbdatas
           For Each item As GuestBookData In notes
               Dim rsselement As New RSSElement
               rsselement.title = item.title
               rsselement.author = item.eMail + " (" + item.name + ")"
               rsselement.link = item.url
               rsselement.description = item.content
               rsselement.pubDate = item.time
               rsselement.guid = item.url
               rssfeed.itmes.Add(rsselement)
           Next

           Dim rssgen As New RSSGenerator
           rssgen.createRSS(rssfeed, path)

總結

有這樣的一個類別就可以很快的產生出屬於自己的RSS Feed,當然這個xml檔要放在網頁伺服器上,我通常是用IIS就解決了~

完成的檔案可以透過Feed Validator for Atom and RSS這樣的網站來做驗證,不過如果只是一些小警告、小錯誤不理他也沒關係,只要你的RSS Reader不會讀不出來就好了

寫完這個RSS產生函式,如果有空我會找時間看看Atom的不同,如果有必要,也會寫Atom的版本~

參考連結

沒有留言:

張貼留言