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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
#--
# Copyright (c) 2005 Robert Aman
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
if Object.const_defined?(:FEED_TOOLS_NAMESPACES)
warn("FeedTools may have been loaded improperly. This may be caused " +
"by the presence of the RUBYOPT environment variable or by using " +
"load instead of require. This can also be caused by missing " +
"the Iconv library, which is common on Windows.")
end
FEED_TOOLS_ENV = ENV['FEED_TOOLS_ENV'] ||
ENV['RAILS_ENV'] ||
'development' # :nodoc:
FEED_TOOLS_NAMESPACES = {
"access" => "http://www.bloglines.com/about/specs/fac-1.0",
"admin" => "http://webns.net/mvcb/",
"ag" => "http://purl.org/rss/1.0/modules/aggregation/",
"annotate" => "http://purl.org/rss/1.0/modules/annotate/",
"atom10" => "http://www.w3.org/2005/Atom",
"atom03" => "http://purl.org/atom/ns#",
"atom-blog" => "http://purl.org/atom-blog/ns#",
"audio" => "http://media.tangent.org/rss/1.0/",
"bitTorrent" =>"http://www.reallysimplesyndication.com/bitTorrentRssModule",
"blogChannel" => "http://backend.userland.com/blogChannelModule",
"blogger" => "http://www.blogger.com/atom/ns#",
"cc" => "http://web.resource.org/cc/",
"creativeCommons" => "http://backend.userland.com/creativeCommonsRssModule",
"co" => "http://purl.org/rss/1.0/modules/company",
"content" => "http://purl.org/rss/1.0/modules/content/",
"cp" => "http://my.theinfo.org/changed/1.0/rss/",
"dc" => "http://purl.org/dc/elements/1.1/",
"dcterms" => "http://purl.org/dc/terms/",
"email" => "http://purl.org/rss/1.0/modules/email/",
"ev" => "http://purl.org/rss/1.0/modules/event/",
"icbm" => "http://postneo.com/icbm/",
"image" => "http://purl.org/rss/1.0/modules/image/",
"indexing" => "urn:atom-extension:indexing",
"feedburner" => "http://rssnamespace.org/feedburner/ext/1.0",
"foaf" => "http://xmlns.com/foaf/0.1/",
"foo" => "http://hsivonen.iki.fi/FooML",
"fm" => "http://freshmeat.net/rss/fm/",
"gd" => "http://schemas.google.com/g/2005",
"gr" => "http://www.google.com/schemas/reader/atom/",
"itunes" => "http://www.itunes.com/dtds/podcast-1.0.dtd",
"l" => "http://purl.org/rss/1.0/modules/link/",
"media" => "http://search.yahoo.com/mrss",
"p" => "http://purl.org/net/rss1.1/payload#",
"pingback" => "http://madskills.com/public/xml/rss/module/pingback/",
"prism" => "http://prismstandard.org/namespaces/1.2/basic/",
"rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs" => "http://www.w3.org/2000/01/rdf-schema#",
"ref" => "http://purl.org/rss/1.0/modules/reference/",
"reqv" => "http://purl.org/rss/1.0/modules/richequiv/",
"rss09" => "http://my.netscape.com/rdf/simple/0.9/",
"rss10" => "http://purl.org/rss/1.0/",
"rss11" => "http://purl.org/net/rss1.1#",
"rss20" => "http://backend.userland.com/rss2",
"search" => "http://purl.org/rss/1.0/modules/search/",
"slash" => "http://purl.org/rss/1.0/modules/slash/",
"soap" => "http://schemas.xmlsoap.org/soap/envelope/",
"ss" => "http://purl.org/rss/1.0/modules/servicestatus/",
"str" => "http://hacks.benhammersley.com/rss/streaming/",
"sub" => "http://purl.org/rss/1.0/modules/subscription/",
"syn" => "http://purl.org/rss/1.0/modules/syndication/",
"taxo" => "http://purl.org/rss/1.0/modules/taxonomy/",
"thr" => "http://purl.org/rss/1.0/modules/threading/",
"ti" => "http://purl.org/rss/1.0/modules/textinput/",
"trackback" => "http://madskills.com/public/xml/rss/module/trackback/",
"wfw" => "http://wellformedweb.org/CommentAPI/",
"wiki" => "http://purl.org/rss/1.0/modules/wiki/",
"xhtml" => "http://www.w3.org/1999/xhtml",
"xml" => "http://www.w3.org/XML/1998/namespace"
}
$:.unshift(File.dirname(__FILE__))
$: << (File.dirname(__FILE__) + "/feed_tools/vendor")
$: << (File.dirname(__FILE__) + "/feed_tools/vendor/html5/lib")
begin
require 'feed_tools/version'
begin
require 'iconv'
rescue Object
warn("The Iconv library does not appear to be installed properly. " +
"FeedTools cannot function properly without it.")
raise
end
if defined?(DEVELOPER_MODE) and DEVELOPER_MODE
require 'rubygems'
gem('builder', '>= 1.2.4') if !defined?(Builder)
gem('activerecord', '>= 1.11.1') if !defined?(ActiveRecord)
end
require 'builder'
require 'active_record'
# Preload optional libraries.
begin
require 'tidy'
rescue Object
end
begin
require 'idn'
rescue Object
end
require 'html5'
require 'html5/html5parser'
require 'html5/liberalxmlparser'
require 'html5/treewalkers'
require 'html5/treebuilders'
require 'html5/serializer'
require 'html5/sanitizer'
require 'feed_tools/vendor/uri'
require 'net/http'
# TODO: Not used yet, don't load since it'll only be a performance hit
# require 'net/https'
# require 'net/ftp'
require 'rexml/document'
require 'uri'
require 'time'
require 'cgi'
require 'pp'
require 'yaml'
require 'base64'
if defined?(DEVELOPER_MODE) and DEVELOPER_MODE
gem('uuidtools', '>= 0.1.2') if !defined?(UUID)
end
require 'uuidtools'
require 'feed_tools/monkey_patch'
require 'feed_tools/feed'
require 'feed_tools/feed_item'
require 'feed_tools/feed_structures'
require 'feed_tools/database_feed_cache'
require 'feed_tools/helpers/html_helper'
require 'feed_tools/helpers/xml_helper'
require 'feed_tools/helpers/uri_helper'
rescue LoadError
# ActiveSupport will very likely mess this up. So drop a warn so that the
# programmer can figure it out if things get wierd and unpredictable.
warn("Unexpected LoadError, it is likely that you don't have one of the " +
"libraries installed correctly.")
raise
end
#= feed_tools.rb
#
# FeedTools was designed to be a simple XML feed parser, generator, and translator with a built-in
# caching system.
#
#== Example
# slashdot_feed = FeedTools::Feed.open('http://www.slashdot.org/index.rss')
# slashdot_feed.title
# => "Slashdot"
# slashdot_feed.description
# => "News for nerds, stuff that matters"
# slashdot_feed.link
# => "http://slashdot.org/"
# slashdot_feed.items.first.find_node("slash:hitparade/text()").value
# => "43,37,28,23,11,3,1"
module FeedTools
@configurations = {}
def FeedTools.load_configurations
if @configurations.blank?
# TODO: Load this from a config file.
config_hash = {}
@configurations = {
:feed_cache => nil,
:disable_update_from_remote => false,
:proxy_address => nil,
:proxy_port => nil,
:proxy_user => nil,
:proxy_password => nil,
:auth_user => nil,
:auth_password => nil,
:auth_scheme => nil,
:http_timeout => nil,
:user_agent =>
"FeedTools/#{FeedTools::FEED_TOOLS_VERSION::STRING} " +
"+http://www.sporkmonger.com/projects/feedtools/",
:generator_name =>
"FeedTools/#{FeedTools::FEED_TOOLS_VERSION::STRING}",
:generator_href =>
"http://www.sporkmonger.com/projects/feedtools/",
:tidy_enabled => false,
:tidy_options => {},
:lazy_parsing_enabled => true,
:serialization_enabled => false,
:idn_enabled => true,
:sanitization_enabled => true,
:sanitize_with_nofollow => true,
:always_strip_wrapper_elements => true,
:timestamp_estimation_enabled => true,
:url_normalization_enabled => true,
:entry_sorting_property => "time",
:strip_comment_count => false,
:tab_spaces => 2,
:max_ttl => 3.days.to_s,
:default_ttl => 1.hour.to_s,
:output_encoding => "utf-8"
}.merge(config_hash)
end
return @configurations
end
# Resets configuration to a clean load
def FeedTools.reset_configurations
@configurations = nil
FeedTools.load_configurations
end
# Returns the configuration hash for FeedTools
def FeedTools.configurations
if @configurations.blank?
FeedTools.load_configurations()
end
return @configurations
end
# Sets the configuration hash for FeedTools
def FeedTools.configurations=(new_configurations)
@configurations = new_configurations
end
# Error raised when a feed cannot be retrieved
class FeedAccessError < StandardError
end
# Returns the current caching mechanism.
#
# Objects of this class must accept the following messages:
# id
# id=
# url
# url=
# title
# title=
# link
# link=
# feed_data
# feed_data=
# feed_data_type
# feed_data_type=
# etag
# etag=
# last_modified
# last_modified=
# save
#
# Additionally, the class itself must accept the following messages:
# find_by_id
# find_by_url
# initialize_cache
# connected?
def FeedTools.feed_cache
return nil if FeedTools.configurations[:feed_cache].blank?
class_name = FeedTools.configurations[:feed_cache].to_s
if @feed_cache.nil? || @feed_cache.to_s != class_name
begin
cache_class = eval(class_name)
if cache_class.kind_of?(Class)
@feed_cache = cache_class
if @feed_cache.respond_to? :initialize_cache
@feed_cache.initialize_cache
end
return cache_class
else
return nil
end
rescue
return nil
end
else
return @feed_cache
end
end
# Returns true if FeedTools.feed_cache is not nil and a connection with
# the cache has been successfully established. Also returns false if an
# error is raised while trying to determine the status of the cache.
def FeedTools.feed_cache_connected?
begin
return false if FeedTools.feed_cache.nil?
return FeedTools.feed_cache.connected?
rescue
return false
end
end
# Creates a merged "planet" feed from a set of urls.
#
# Options are:
# * <tt>:multi_threaded</tt> - If set to true, feeds will
# be retrieved concurrently. Not recommended when used
# in conjunction with the DatabaseFeedCache as it will
# open multiple connections to the database.
def FeedTools.build_merged_feed(url_array, options = {})
FeedTools::GenericHelper.validate_options([ :multi_threaded ],
options.keys)
options = { :multi_threaded => false }.merge(options)
warn("FeedTools.build_merged_feed is deprecated.")
return nil if url_array.nil?
merged_feed = FeedTools::Feed.new
retrieved_feeds = []
if options[:multi_threaded]
feed_threads = []
url_array.each do |feed_url|
feed_threads << Thread.new do
feed = Feed.open(feed_url)
retrieved_feeds << feed
end
end
feed_threads.each do |thread|
thread.join
end
else
url_array.each do |feed_url|
feed = Feed.open(feed_url)
retrieved_feeds << feed
end
end
retrieved_feeds.each do |feed|
merged_feed.entries = merged_feed.entries.concat(
feed.entries.collect do |entry|
new_entry = entry.dup
new_entry.title = "#{feed.title}: #{entry.title}"
new_entry
end
)
end
return merged_feed
end
end
begin
unless FeedTools.feed_cache.nil?
FeedTools.feed_cache.initialize_cache
end
rescue
end
|