首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Atom/RSS feed 的应用 -3 个性化 feed 内容

Atom/RSS feed 的应用 -3 个性化 feed 内容

个性化 feed 内容除了将已有内容发布成 feed 之外,另一个主要的使用场景即为消费第三方提供的 feed 内容。sMash 提供了 zero.feed 包为消费和处理第三方的 feed 数据提供了丰富的功能支持和非常简单易用的 API。以查看搜狐 IT 新闻为例,用户只需要查看当天的内容,这时候你可以使用 zero.feed 组件提供的编程接口创建几行脚本就搞定了!
1. 使用命令行工具创建名为 personalfeed.demo 的 sMash 应用程序:
1
zero create personalfeed.demo




2. 在 sMash 应用程序的依赖管理文件 config/ivy.xml 中增加对 zero.feed 模块的依赖:
清单 5. 使用 zero.feed 模块
1
2
3
4
<dependencies>
     <dependency org="zero" name="zero.core" rev="[1.1+,2.0.0.0["/>
     <dependency org="zero" name="zero.feed" rev="[1.1+,2.0.0.0["/>
</dependencies>




3. 创建脚本文件 /public/sohuITtoday/index.groovy:
清单 6. 使用 Groovy feed API
1
2
3
4
5
6
7
// set response header "Content-Type"
headers.out."Content-Type" = "application/atom+xml"

// fetch sohu IT news feed and filter only today
zero.feed.FeedFactory.fetch("http://news.sohu.com/rss/it.xml")
     .after("-P1D")
     .writeTo(request.writer[])




4. 启动 sMash 应用程序,访问地址 http://localhost:8080/sohuITtoday/ 即可。
5. 如果你还需要查看新浪 IT 和网易 IT 新闻,还可以将他们聚合起来变成一个 feed. 在 personalfeed.demo 应用程序中创建文件 /public/ITtoday/index.groovy:
清单 7. 聚合并过滤 feed 内容
1
2
3
4
5
6
7
8
// set response header "Content-Type"
headers.out."Content-Type" = "application/atom+xml"
zero.feed.FeedFactory.aggregate(
     "http://news.sohu.com/rss/it.xml",
     "http://rss.sina.com.cn/news/allnews/tech.xml",
     "http://tech.163.com/special/00091JPQ/rssit.xml")
     .after("-P1D")
     .writeTo(request.writer[])




最后在浏览器中访问地址 http://localhost:8080/ITtoday/,你将会看到一个 feed 包含了 3 大网站的 IT 新闻。
除了编写服务器端运行的 Groovy 来获取 feed 数据之外,zero.feed 模块还提供 Ajax 风格的 JavaScript feed 编程接口。这意味着 Web 开发者只需要浏览器即可针对 feed 进行编程。为了简化 JavaScript 访问 feed 数据时冗长的方法调用,Web 开发者可以使用 JSON 格式来代理 XML 格式来访问 feed。
这里仍以访问搜狐 IT 新闻为例,直接创建 HTML 文件 public/sohuITToday/sample.html:
清单 8. 使用 JavaScript 访问 feed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<html>
<head>
<script type="text/javascript" src="/dojo/dojo.js"></script>
<script type="text/javascript">
dojo.require("dojo.parser");
dojo.require("zero.feed.Feed");
</script>
<script type="text/javascript">
function fetchSohuIT() {
   zero.feed.FeedFactory.fetch("http://news.sohu.com/rss/it.xml")
     .after("-P1D")
     .loadAsJSON(function(feed) {
       var items = feed.entries;
       var htmlContent = "";
       for (var i = 0; i < items.length; i++) {
         var title = items.title;
         var link = items.links[0].href;        
         htmlContent += "<a href='" + link + "' target='_blank'>"
          + title + "</a><br/>";
       }
       dojo.byId("feed").innerHTML = htmlContent;
     });
}
</script>
</head>
<body>
<p>This sample shows how to get resulting feed in JSON format.</p>
<button onclick="fetchSohuIT();" dojoType="dijit.form.Button">
load sohu IT news!
</button>
<div id="feed"></div>
</body>
</html>




运行 sMash 应用程序,访问 http://localhost:8080/sohuITtoday/sample.html, 单击按钮“load sohu IT news!”,稍后就可以看到如图 1 所示。
图 1. Ajax feed API 访问搜狐 IT 新闻
返回列表