开源项目 我的故事新功能上线:RSS 订阅和发文章自动 ping baidu 和 google

gazeldx · 2013年03月05日 · 4376 次阅读

我的故事 http://mystory.cc/ 多人博客系统,源代码在: https://github.com/gazeldx/mystory/

加了 RSS 订阅功能。实现挺容易的,rails 支持 atom 的,写法如下 controller.rb

# RSS and atom Feed.
def feed    
  notes = @user.notes.where(:is_draft => false).limit(30).order("updated_at desc")
  blogs = @user.blogs.where(:is_draft => false).limit(20).order("updated_at desc")
  @all = (notes | blogs).sort_by{|x| x.updated_at}.reverse!    

  respond_to do |format|
    format.atom { render :layout => false }
    # we want the RSS feed to redirect permanently to the ATOM feed
    format.rss { redirect_to feed_path(:format => :atom), :status => :moved_permanently }
  end
end

view feed.atom.builder


atom_feed :language => 'zh-CN' do |feed|
  feed.title "#{@user.name}_#{t'site.name'}"
  feed.updated @all.first.updated_at unless @all.empty?

  @all.each do |item|
    feed.entry(item) do |entry|
      entry.url eval("#{item.class.name.downcase}_url(item)")
      entry.title item.title
      entry.content style_it_no_blank(item.content), :type => 'html'

      # the strftime is needed to work with Google Reader.
      entry.updated(item.updated_at.strftime("%Y-%m-%dT%H:%M:%SZ")) 

      entry.author do |author|
        author.name @user.name
      end
    end
  end
end

还上线了在发文章时自动 ping baidu 和 google 功能,参考了https://github.com/robbin/robbin_site 实现如下:

# blog search ping for SEO purpose
  def ping_search_engine(item)
    # http://www.google.cn/intl/zh-CN/help/blogsearch/pinging_API.html
    # http://www.baidu.com/search/blogsearch_help.html
    baidu = XMLRPC::Client.new2("http://ping.baidu.com/ping/RPC2")
    baidu.timeout = 5  # set timeout 5 seconds
    b = baidu.call("weblogUpdates.extendedPing",
               "#{@user.name}_#{t'site.name'}",
               site(@user),
               eval("#{item.class.name.downcase}_url(item)"),
               site(@user) + feed_path)
    logger.info(b)
    google = XMLRPC::Client.new2("http://blogsearch.google.com/ping/RPC2")
    google.timeout = 5
    g = google.call("weblogUpdates.extendedPing",
               "#{@user.name}_#{t'site.name'}",
               site(@user),
               eval("#{item.class.name.downcase}_url(item)"),
               site(@user) + feed_path,
               item.tags.join('|'))    
    logger.info(g)
  rescue Exception => e
    logger.warn(e)
  end
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号