Class: Biblionet::Extractors::Search

Inherits:
BookExtractor show all
Defined in:
lib/bookshark/extractors/search.rb

Instance Attribute Summary

Attributes inherited from BookExtractor

#book

Attributes inherited from Base

#biblionet_id, #filepath, #page, #url

Instance Method Summary collapse

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(options = {})
  perform_search(options) unless options.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(options = {})
  title         = present?(options[:title])     ? options[:title].gsub(' ','+')     : ''
  author        = present?(options[:author])    ? options[:author].gsub(' ','+')    : ''
  publisher     = present?(options[:publisher]) ? options[:publisher].gsub(' ','+') : ''
  category      = present?(options[:category])  ? options[:category].gsub(' ','+')  : ''

  title_split   = options[:title_split]  ||= '1'
  book_id       = options[:book_id]      ||= ''
  isbn          = options[:isbn]         ||= ''        
  author_id     = options[:author_id]    ||= ''        
  publisher_id  = options[:publisher_id] ||= ''      
  category_id   = options[:category_id]  ||= ''
  after_year    = options[:after_year]   ||= ''
  before_year   = options[: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(author)
  url_builder.append('&person_ID=')
  url_builder.append(author_id)
  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(options = {})
  search_url = build_search_url(options)
  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 options[:results_type] == 'ids'
    return book_ids          
  elsif options[: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