Class: Biblionet::Extractors::Search
- Inherits:
-
BookExtractor
- Object
- Base
- BookExtractor
- Biblionet::Extractors::Search
- Defined in:
- lib/bookshark/extractors/search.rb
Instance Attribute Summary
Attributes inherited from BookExtractor
Attributes inherited from Base
#biblionet_id, #filepath, #page, #url
Instance Method Summary collapse
- #build_search_url(options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Search
constructor
A new instance of Search.
- #perform_search(options = {}) ⇒ Object
- #search_by_isbn(isbn) ⇒ Object
Methods inherited from BookExtractor
#extract_book, #load_and_extract_book, #proccess_contributors, #proccess_ddc, #proccess_details
Methods inherited from Base
#decode_text, decode_text, #load_page, #load_page_from_file, #load_page_from_url, #present?, #save_page
Methods included from FileManager
#list_directories, #list_files, #save_to
Constructor Details
#initialize(options = {}) ⇒ Search
Returns a new instance of Search.
10 11 12 |
# File 'lib/bookshark/extractors/search.rb', line 10 def initialize( = {}) perform_search() unless .empty? end |
Instance Method Details
#build_search_url(options = {}) ⇒ Object
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 |
# File 'lib/bookshark/extractors/search.rb', line 51 def build_search_url( = {}) title = present?([:title]) ? [:title].gsub(' ','+') : '' = present?([:author]) ? [:author].gsub(' ','+') : '' publisher = present?([:publisher]) ? [:publisher].gsub(' ','+') : '' category = present?([:category]) ? [:category].gsub(' ','+') : '' title_split = [:title_split] ||= '1' book_id = [:book_id] ||= '' isbn = [:isbn] ||= '' = [:author_id] ||= '' publisher_id = [:publisher_id] ||= '' category_id = [:category_id] ||= '' after_year = [:after_year] ||= '' before_year = [:before_year] ||= '' url_builder = StringBuilder.new url_builder.append('http://www.biblionet.gr/main.asp?page=results') url_builder.append('&title=') url_builder.append(title) url_builder.append('&TitleSplit=') url_builder.append(title_split) url_builder.append('&Titlesid=') url_builder.append(book_id) url_builder.append('&isbn=') url_builder.append(isbn) url_builder.append('&person=') url_builder.append() url_builder.append('&person_ID=') url_builder.append() url_builder.append('&com=') url_builder.append(publisher) url_builder.append('&com_ID=') url_builder.append(publisher_id) url_builder.append('&from=') url_builder.append(after_year) url_builder.append('&untill=') url_builder.append(before_year) url_builder.append('&subject=') url_builder.append(category) url_builder.append('&subject_ID=') url_builder.append(category_id) url_builder.build end |
#perform_search(options = {}) ⇒ Object
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 |
# File 'lib/bookshark/extractors/search.rb', line 14 def perform_search( = {}) search_url = build_search_url() load_page(URI.encode(search_url)) # Page gets loaded on @page variable. book_ids = [] # No need to operate on whole page. Just on part containing the book. content_re = /<!-- CONTENT START -->.*<!-- CONTENT END -->/m if (content_re.match(@page)).nil? puts @page end content = content_re.match(@page)[0] nodeset = Nokogiri::HTML(content) nodeset.xpath("//a[@class='booklink' and @href[contains(.,'/book/') ]]").each do |item| book_ids << item[:href].split("/")[2] end books = [] if [:results_type] == 'ids' return book_ids elsif [:results_type] == 'metadata' book_ids.each do |id| url = "http://www.biblionet.gr/book/#{id}" books << load_and_extract_book(url) end end return books end |
#search_by_isbn(isbn) ⇒ Object
46 47 48 49 |
# File 'lib/bookshark/extractors/search.rb', line 46 def search_by_isbn(isbn) results = perform_search(isbn: isbn, results_type: 'ids') book_id = results.empty? ? nil : results.first.to_i end |