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

书签?标记?del.icio.us 网站!(2)

书签?标记?del.icio.us 网站!(2)

本帖最后由 look_w 于 2018-7-20 08:44 编辑

本土方法您可能只是对门面仰慕已久,而在 Web 2.0 站点的某些点上,您会寻求某些工具,用甚至站点创建者都没有预见到的方法获得站点功能。对于 del.icio.us 来说,有很多这样的工具。开始时,站点的创建者提供他们自己的一些工具,从可用于在其他站点上显示您网络中的帖子的标记到浏览器工具栏。此外,一百多个第三方工具覆盖了您能想像到的 del.icio.us 的大多数用途,大多数,但并非全部,所以可以这样归纳,Web 2.0 站点的重要性就在于您可以突破编译器或解释程序,创建您自己的特性。
Web 提要:便宜的 APIdel.icio.us 的官方 API 使用 HTTP 以及 SSL 和身份验证。但是如果您只需要读访问,那么可以选择整体上采用 Web 2.0 的更为官方的 API:Web 提要。您可以访问用户(;username>)、标记(;tag>)或者两者组合(;username>/<tag>)的 Web 提要。那么问题只在于解析 Web 提要以提取所需信息。 提供了一个示例。它是 Python 代码,该代码发送包含前一天 del.icio.us 提要条目的电子邮件。
清单 1. 用于发送前一天 del.icio.us 帖子的电子邮件的代码
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import time
import smtplib
from email.MIMEText import MIMEText
from datetime import date, timedelta

import amara

#The base URI for all tags
TAGBASE = 'http://del.icio.us/tag/'

#Set FEEDS to customize which feeds you want to monitor
FEEDS = ['http://del.icio.us/rss/uche', 'http://del.icio.us/rss/popular']

FROM = 'del.icio.us@example.com'
TO = 'user@example.com'
SMTPHOST = 'localhost'

#Compute the date string for yesterday in ISO-8601 format
yesterday = (date(*time.gmtime()[:3]) - timedelta(1)).isoformat()

message_text = u''

#Using Amara. Easy to just grab the RSS feed
for feed in FEEDS:
    doc = amara.parse(feed)
    message_text += u'\n' + unicode(doc.RDF.channel.title) + u'\n\n'
    current_items = [ item for item in doc.RDF.item
                      if unicode(item.date).startswith(yesterday) ]
    for item in current_items:
        #Get the properties of the link, defaulting to empty string
        title = unicode(getattr(item, 'title', u''))
        href = unicode(getattr(item, 'link', u''))
        desc = unicode(getattr(item, 'description', u''))
        creator = unicode(getattr(item, 'creator', u''))
        message_text += u'<%s>--"%s" (from %s)\n'%(href, title, creator)
        message_text += desc + '\n'

#Be sure to handle Unicode by encoding to UTF-8
msg = MIMEText(message_text.encode('utf-8'))

#Set message metadata
msg['Subject'] = u'del.icio.us bookmarks for %s\n' % yesterday
msg['From'] = FROM
msg['To'] = TO

#Send the message via the specified SMTP server
s = smtplib.SMTP()
s.connect(SMTPHOST)
#s.login(SMTP_USERNAME, SMTP_PASSWORD) #If login is necessary
s.sendmail(FROM, [TO], msg.as_string())
s.close()




该代码使用 Amara XML Toolkit(请参阅 )下载和解析 Web 提要(del.icio.us 使用 RSS 1.0 作为其 Web 提要格式)。示例中的两个提要一个用于我自己的用户,一个用于 popular 标记。for 循环遍历提要,查找所需日期的条目,并提取几个字段以构建到 message_text 中积累的消息文本中。循环体之后的行实际上是就是用于发送该消息。  
正式的 API如果您希望能够编辑条目,或者以任何方式访问那些尚未为您打包到 Web 提要中的数据,则您必须使用官方 API,它基于身份验证和加密的 HTTP。这些出色的 HTTP 特性的秘密就是 Python 模块 urllib2。我不会提供使用官方 API 的全部 Python 示例,原因是在 Web 上可以轻松找到这些示例。但是我会提到,每当使用 Web 2.0 站点 API 时,您必须始终注意查询频率不要太高。原因是 del.icio.us API 页面提到了以下内容:
请在两次查询之间至少等待一秒钟,否则可能会自动控制您的速度。如果您发布一个库以访问 API,则必须做到这一点。  
该页面还包含其他警告,并且大多数警告适用于任何免费的、开放的 Web API,因此,花时间去关注那些能使您成为优秀 Web 开发者公民的惯例是值得的。
在 JavaScript 中使用 del.icio.us如果您使用 ECMAScript(有时称为 JavaScript)编写使用 del.icio.us 的工具,则有另外一个进行读访问的选择,它与 Web 提要类似。到目前为止,大多数 Web 开发人员都熟悉 Asynchronous JavaScript with XML(Ajax)。XML 部分表示在网络中传递信息的方法,但是 XML 不是必需的(有关详细信息,请参阅 )。另一种受欢迎的格式是 JavaScript Object Notation(JSON),该格式的优点是,与 XML 相比,使用 ECMAScript 解析更容易。一些 del.icio.us JSON 提要类似于本文中以前使用过的 Web 提要。当您访问 URL(如 ;username> 和 ;tag>)时,您得到最近条目的 JSON 表示。 是包含从 del.icio.us 中加载 JSON 提要的 ECMAScript 的网页的一个小例子。
清单 2. 用于在网页上显示最新的 del.icio.us 帖子的代码
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
    <title>Bookmarks from del.icio.us</title>
    <script type="text/javascript"
            src="//del.icio.us/feeds/json/uche?count=10">
      //Loads JSON from del.icio.us, which evaluates to an an array: Delicious.posts
    </script>

    <script type="text/javascript">
//Set up asynchronous display of images
function showImage(img){ return (function(){ img.style.display='inline'; }) };

function showBookmarks()
{
    var ul = document.createElement('ul');
    //Process each item in the del.icio.us JSON feed
    for (var i=0, post; post = Delicious.posts; i++) {
        var li = document.createElement('li');
        var a = document.createElement('a');
        var img = document.createElement('img');
        a.style.marginLeft = '20px';
        img.style.position = 'absolute';
         //Don't display image at first: will be asynchronously turned on
        img.style.display = 'none';
        img.height = img.width = 16;
        //Raw string processing to get the URL of the bookmark icon
        img.src = post.u.split('/').splice(0,3).join('/')+'/favicon.ico';
        img.onload = showImage(img);
        a.setAttribute('href', post.u);
        a.appendChild(document.createTextNode(post.d));
        li.appendChild(img);
        li.appendChild(a);
        ul.appendChild(li);
    }
    //No bullet markings for list items
    ul.style.setProperty('list-style', 'none', 'important');
    //Replace a targeted portion of initial content with the constructed list
    var removeTarget = document.getElementById('removeTarget');
    var updateTarget = document.getElementById('updateTarget');
    updateTarget.removeChild(removeTarget);
    updateTarget.appendChild(ul);
}
    </script>
  </head>
  <body id='updateTarget'>
    <h2>Bookmarks from del.icio.us</h2>
    <p id='removeTarget'>
      <a href="javascript:showBookmarks()">Click here to load addresses</a>
    </p>
  </body>
</html>




此 HTML 包括两个脚本。第一个脚本只是用于检索 JSON 的 del.icio.us 链接。所得到的对象现在对页面上的其他脚本都是可用的。第二个脚本由主 HTML 页面上的链接触发。它读取 JSON 并使用 DOM 指令构建要在页面中包含的列表。 是单击所选择的 Click here to load addresses 链接时所产生的页面的一部分。  
图 2. 自动加载的具有 del.icio.us 条目的网页
返回列表