Class: KindleManager::BooksAdapter

Inherits:
BaseAdapter show all
Defined in:
lib/kindle_manager/adapters/books_adapter.rb

Instance Attribute Summary

Attributes inherited from BaseAdapter

#options, #session, #store

Instance Method Summary collapse

Methods inherited from BaseAdapter

#initialize, #limit, #max_scroll_attempts

Constructor Details

This class inherits a constructor from KindleManager::BaseAdapter

Instance Method Details

#current_pageObject



55
56
57
# File 'lib/kindle_manager/adapters/books_adapter.rb', line 55

def current_page
  doc.css('#pagination a.active').first.text.to_i
end

#fetchObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/kindle_manager/adapters/books_adapter.rb', line 7

def fetch
  go_to_kindle_management_page
  begin
    load_next_kindle_list
  rescue => e
    puts "[ERROR] #{e}"
    puts e.backtrace
    puts
    puts "Investigate the error using 'client.session', 'client.doc' etc."
  end
end

#fetching_intervalObject



81
82
83
# File 'lib/kindle_manager/adapters/books_adapter.rb', line 81

def fetching_interval
  @options.fetch(:fetching_interval, 3)
end

#go_to_kindle_management_pageObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kindle_manager/adapters/books_adapter.rb', line 19

def go_to_kindle_management_page
  log "Visiting kindle management page"
  3.times do
    session.visit url_for_kindle_contents
    wait_for_selector('#content-page-title')
    if session.has_css?('#content-page-title')
      log "Page found '#{session.first('#content-page-title').text}'"
      break
    else
      
    end
  end
end

#loadObject



46
47
48
49
50
51
52
53
# File 'lib/kindle_manager/adapters/books_adapter.rb', line 46

def load
  books = []
  store.list_html_files.each do |file|
    parser = KindleManager::BooksParser.new(file)
    books += parser.parse
  end
  books.sort_by{|b| [-b.date.to_time.to_i, -b.fetched_at.to_i] }.uniq{|b| [b.asin, b.tag] }
end

#load_next_kindle_listObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kindle_manager/adapters/books_adapter.rb', line 33

def load_next_kindle_list
  wait_for_selector('#CONTENT_COUNT')
  loop do
    snapshot_page
    break if current_page == max_page
    break if limit && limit <= number_of_fetched_books
    session.first("#page-#{current_page + 1}").click
    sleep fetching_interval
  end
  log "Stopped loading. You may want to continue with 'client.session', 'client.doc' etc."
  snapshot_page
end

#max_pageObject



59
60
61
# File 'lib/kindle_manager/adapters/books_adapter.rb', line 59

def max_page
  @_max_page ||= doc.css('#pagination a').last.text.to_i
end

#number_of_fetched_booksObject



63
64
65
66
67
68
69
70
71
# File 'lib/kindle_manager/adapters/books_adapter.rb', line 63

def number_of_fetched_books
  re = (AmazonInfo.domain =~ /\.jp\z/ ? /(\d+)から(\d+)/ : / (\d+) to (\d+) /)
  wait_for_selector('#CONTENT_COUNT')
  text = doc.css('#CONTENT_COUNT').text
  log "Number of books: [#{text}]"
  m = text.match(re)
  return m[2].to_i if m.present?
  raise("Couldn't get the number of fetched books [#{text}]")
end

#snapshot_pageObject



73
74
75
76
77
78
79
# File 'lib/kindle_manager/adapters/books_adapter.rb', line 73

def snapshot_page
  if (text = doc.css('#CONTENT_COUNT').try!(:text)).present?
    log "Current page [#{text.to_s.gsub(/[[:space:]]+/, ' ').strip}]"
  end
  store.record_page
  log "Saving page"
end

#url_for_kindle_contentsObject



3
4
5
# File 'lib/kindle_manager/adapters/books_adapter.rb', line 3

def url_for_kindle_contents
  "https://www.#{ENV['AMAZON_DOMAIN']}/hz/mycd/digital-console/contentlist/booksAll/dateDsc/"
end