RSS:概念與實作
What is RSS? Why use RSS?
RSS(Really Simple Syndication,簡易資訊聚合)是一種消息來源格式規範,用以聚合經常發布更新數據的網站,例如博客文章、新聞、多媒體等的摘要或最新更新訊息(Ref:RSS)。其目的為把新聞標題、摘要(Feed)、內容按照使用者的要求,「送」到使用者的面前。RSS摘要可以藉由RSS閱讀器、Feed Reader等軟體來閱讀(EX:Feedly、Digg)。RSS vs Sitemap
Sitemap是網站地圖,目的是完整呈現網站的所有內容,RSS則是負責列出更新的項目,在定義上是不同的。而當GoogleBot來爬的時候,你最希望它收到什麼資訊呢?不會是整個網站全部的內容吧XD,應該是整個網站最重要或最近更新的項目,因此做RSS只提交更新的頁面是相對有用的,做全站的sitemap不見得有太大的意義(而且目前的網站份量都是很大的)。
另外,GoogleBot爬網站的週期是用PR值來訂(參考Google爬蟲的大揭密 - Googlebot爬文週期)。
How to implement RSS by ASP.NET MVC?
使用ASP.NET MVC(C#)實作如下,參考自Easily Build An Atom or RSS Feed With C# and the Syndication Namespace。而這篇文章對於重點程式碼也有解說,有興趣的可以看一下。如果對欄位定義或格式有興趣,可以參考RSS Advisory Board,因為不同性質的網站對於欄位的選擇會有些許差異。
以下的步驟大致上就是如何產生item(例如想要分享的文章)、設定channel名稱、連結、logo、語系、更新時間等重要資訊,然後再寫入xml檔中。
public class Feed
{
public static void UpdateFeed(HttpRequest req)
{
string baseUrl = req.Url.Scheme + "://" + req.Url.Authority;
#region load records
List<Article> articles = new List<Article>();
#region fake data
Article ar1 = new Article();
ar1.id = 1;
ar1.title = "波蘭的饕餮、飢餓與美食復興";
ar1.link = "http://cn.tmagazine.com/food-wine/20140308/tc08recipe/zh-hant";
ar1.description = "波蘭飲食文化經歷過講求豐盛的「饕餮」時期,味寡色黯的社會主義「糊口飯」年代,直至如今的美食振興運動。美食家陳楠回顧了波蘭飲食文化,並分享兩道地道波蘭美食食譜。";
ar1.pubDate = DateTime.Now;
ar1.imgSource = "http://g1.nytimg.com/images/2014/03/07/world/tc08recipe/tc08recipe-articleLarge.png";
ar1.imgSize = 1;
articles.Add(ar1);
Article ar2 = new Article();
ar2.id = 2;
ar2.title = "金融危機五年後,冰島依然後怕";
ar2.link = "http://cn.nytimes.com/business/20140308/c08icebanks/zh-hant";
ar2.description = "冰島的經歷是一場活生生的實驗,它驗證了在全球金融危機之下,一個國家迫使自己的金融企業破產而不救援可能會發生什麼情況。";
ar2.pubDate = DateTime.Now;
ar2.imgSource = "http://graphics8.nytimes.com/images/2014/01/16/business/16icebanks-ss-slide-O7ZN/16icebanks-ss-slide-O7ZN-articleLarge.jpg";
ar2.imgSize = 1;
articles.Add(ar2);
Article ar3 = new Article();
ar3.id = 3;
ar3.title = "《冰雪奇緣》助迪士尼重奪動畫之王寶座";
ar3.link = "http://cn.tmagazine.com/culture/20140305/t05disney/zh-hant";
ar3.description = "迪士尼堪稱主流動畫的誕生地,但在電腦動畫方面遭遇瓶頸,讓後來居上的皮克斯多年佔據奧斯卡獎。從收購皮克斯、公司重組到推出《冰雪奇緣》,迪士尼一步步奪回霸主地位。";
ar3.pubDate = DateTime.Now;
ar3.imgSource = "http://g1.nytimg.com/images/2014/02/06/business/05disney/06disney-articleLarge.jpg";
ar3.imgSize = 1;
articles.Add(ar3);
#endregion fake data
#endregion
#region create feed
SyndicationFeed feed = new SyndicationFeed();
//set properties on the feed
feed = new SyndicationFeed("紐約時報中文網", "紐約時報中文網是紐約時報公司旗下的首個中文媒介産品,紐約時報中文網旨在向中國讀者提供有關全球時事、商業及文化的高水準報導。", new Uri("http://cn.nytimes.com/zh-hans"), baseUrl, DateTime.Now);
feed.Id = baseUrl;
feed.Copyright = new TextSyndicationContent("紐約時報中文網");
feed.LastUpdatedTime = new DateTimeOffset(DateTime.Now);
feed.Language = "zh-tw";
//add feed logo url
string imageUrl = "http://cn.nytimes.com/img/nameplate.gif";
feed.ImageUrl = new Uri(imageUrl);
//add the URL that will link to the published feed when it's done
SyndicationLink link = new SyndicationLink(new Uri(baseUrl + "/feed.xml"));
link.RelationshipType = "self";
link.MediaType = "text/html";
link.Title = "紐約時報中文網";
feed.Links.Add(link);
#endregion
#region loop over the entries to add feed items
List<SyndicationItem> items = new List<SyndicationItem>();
int maxItems = 1000;
for (int i = 0; i < articles.Count; i++)
{
Article a = articles[i];
TextSyndicationContent content = new TextSyndicationContent(a.description);
Uri uri = new Uri(a.link);
SyndicationItem item = new SyndicationItem(a.title, content, uri, a.id.ToString(), DateTime.Now);
item.PublishDate = a.pubDate;
item.ElementExtensions.Add(
new XElement("enclosure",
new XAttribute("type", "image/jpeg"),
new XAttribute("url", a.imgSource),
new XAttribute("length", a.imgSize)
).CreateReader()
);
items.Add(item);
//stop after adding the max desired number of items
if (items.Count >= maxItems)
{
break;
}
}
feed.Items = items;
#endregion
#region output feed to a file
var rssFormatter = new Rss20FeedFormatter(feed, false);
var output = new StringBuilder();
using (var writer = XmlWriter.Create(@"C:\feed.xml", new XmlWriterSettings { Indent = true }))
{
rssFormatter.WriteTo(writer);
writer.Flush();
}
#endregion
}//end UpdateFeed
public class Article
{
public int id { get; set; }
public string title { get; set; }
public string link { get; set; }
public string description { get; set; }
public DateTime pubDate { get; set; }
public string imgSource { get; set; }
public long imgSize { get; set; }
}
}
結果:輸出一個feed.xml檔案。資料來源:紐約新聞網
How to use RSS?
驗證工具:W3C Feed Validation Service, for Atom and RSS對使用者來說,可以使用一個連結來放置RSS來源,例如:
<a type="application/rss+xml" href="feed.rss">RSS feed for this page</a>
"feed.rss"是feed的存放位置,"type="application/rss+xml"是告知瀏覽器這是一個RSS feed的連結。注意,假設是Atom feeds的話,type是用"application/atom+xml"。
對程式來說,它們也許不是在頁面尋找,而是在
<head>
tag。<head>
<title>紐約新聞網</title>
<link rel="alternate" type="application/rss+xml" href="feed.rss" title="紐約新聞網RSS Feed">
</head>
Ref
- RSS
- RSS Tutorial
- Easily Build An Atom or RSS Feed With C# and the Syndication Namespace
- RSS Advisory Board
- 如何有效正確的提交Sitemap給搜索引擎
- Google爬蟲的大揭密 - Googlebot爬文週期
- Get Better Search Engine Rankings with RSS
因為部落格搬家了,因此在新落格也放了一份,未來若有增刪會在這裡更新-RSS:概念與實作。
留言