分享

真正的Web2.0:书签?标记?_Web服务技巧_Web2.0_TechTarget IT...

 weicat 2007-08-03
本土方法

  您可能只是对门面仰慕已久,而在 Web 2.0 站点的某些点上,您会寻求某些工具,用甚至站点创建者都没有预见到的方法获得站点功能。对于 del. 来说,有很多这样的工具。开始时,站点的创建者提供他们自己的一些工具,从可用于在其他站点上显示您网络中的帖子的标记到浏览器工具栏。此外,一百多个第三方工具覆盖了您能想像到的 del. 的大多数用途,大多数,但并非全部,所以可以这样归纳,Web 2.0 站点的重要性就在于您可以突破编译器或解释程序,创建您自己的特性。

  Web 提要:便宜的 API

  del. 的官方 API 使用 HTTP 以及 SSL 和身份验证。但是如果您只需要读访问,那么可以选择整体上采用 Web 2.0 的更为官方的 API:Web 提要。您可以访问用户(http://del./rss/<username>)、标记(http://del./rss/<tag>)或者两者组合(http://del./rss/<username>/<tag>)的 Web 提要。那么问题只在于解析 Web 提要以提取所需信息。清单 1 提供了一个示例。它是 Python 代码,该代码发送包含前一天 del. 提要条目的电子邮件。

  清单 1. 用于发送前一天 del. 帖子的电子邮件的代码

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./tag/‘

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

FROM = ‘del.@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. 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()

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多