diff --git a/lib/opds.rb b/lib/opds.rb index 5d648df..e1a831b 100644 --- a/lib/opds.rb +++ b/lib/opds.rb @@ -1,2 +1,4 @@ require 'opds/opds' -require 'opds/support/browser.rb' +require 'opds/support/logging' +require 'opds/support/browser' +require 'opds/parser' diff --git a/lib/opds/opds.rb b/lib/opds/opds.rb index c5db525..0dccdc3 100644 --- a/lib/opds/opds.rb +++ b/lib/opds/opds.rb @@ -1,2 +1,5 @@ module OPDS + def self.access(feed) + Feed.parse_url(feed) + end end diff --git a/lib/opds/parser.rb b/lib/opds/parser.rb new file mode 100644 index 0000000..a00e55b --- /dev/null +++ b/lib/opds/parser.rb @@ -0,0 +1,38 @@ +require "nokogiri" +module OPDS + class OPDSParser + include Logging + attr_accessor :options + attr_reader :sniffed_type + def initialize(opts={}) + @sniffed_type=nil + self.options=opts.merge({}) + end + + def parse(content) + @ret=Nokogiri::XML(content) + @sniffed_type=sniff(@ret) + @ret + end + protected + def sniff(doc) + return :entry if doc.root.name=='entry' + entries = doc.xpath('/xmlns:feed/xmlns:entry',doc.root.namespaces) + if entries.size > 0 + return :acquisition if entries.all? do |entry| + entry.xpath('xmlns:link').any? do |link| + l=link.attributes['rel'] + unless l.nil? + l.value.include?('http://opds-spec.org/acquisition') + else + false + end + end + end + return :navigation + end + return nil + end + + end +end diff --git a/lib/opds/support/browser.rb b/lib/opds/support/browser.rb index ba3ad5a..f643c0e 100644 --- a/lib/opds/support/browser.rb +++ b/lib/opds/support/browser.rb @@ -2,6 +2,7 @@ module OPDS module Support class Browser + include Logging def go_to(uri) url=URI.parse(uri) @last_response=nil @@ -13,7 +14,7 @@ def go_to(uri) @last_response = http.request(req) } if status/10==30 && headers['location'] - STDERR.puts("Following redirection (code: #{status}) to #{headers['location']}") + log("Following redirection (code: #{status}) to #{headers['location']}") go_to(headers['location'].first) end end @@ -32,7 +33,7 @@ def headers end def body - @last_response.body + @last_response.body if @last_response end end diff --git a/lib/opds/support/logging.rb b/lib/opds/support/logging.rb new file mode 100644 index 0000000..cc35703 --- /dev/null +++ b/lib/opds/support/logging.rb @@ -0,0 +1,11 @@ +module OPDS + module Logging + def log(txt) + STDERR.puts("LOGGING : #{txt}") + end + + def self.log(txt) + STDERR.puts("LOGGING : #{txt}") + end + end +end