利用 Ruby 进行新闻追踪与办公自动化报告生成
新闻追踪报告应用创建
在处理新闻源时,我们需要确保不重复添加已有的新闻。以下是一段 Ruby 代码,用于检查新闻是否已存在于数据库中,如果不存在则将其添加到数据库:
if not (Stories.find_by_title(feed_story.title) or
Stories.find_by_url(feed_story.link) or
Stories.find_by_guid(feed_story.guid))
puts "processing story '#{feed_story.title}' - new"
Stories.new do |new_story|
new_story.title=feed_story.title.gsub(/<[^>]*>/, '') # strip HTML
new_story.guid=feed_story.guid
new_story.sourcename=feed_story.publisher.name if feed_story.publisher.name
new_story.url=feed_story.link
new_story.published_at = feed_story.published
new_story.save
end
else
# do nothing
end
上述代码的逻辑如下:
1. 检查是否存在与当前新闻标题、链接或 GUID 相同的新闻。 <
超级会员免费看
订阅专栏 解锁全文
866

被折叠的 条评论
为什么被折叠?



