class KindleManager::HighlightsAdapter
Constants
- KINDLE_HIGHLIGHT_URL
Attributes
failed_library_ids[RW]
library_ids[RW]
loaded_library_ids[RW]
Public Instance Methods
check_library_scroll()
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 40 def check_library_scroll if session.first('#library .kp-notebook-scroller-addon').present? scroll_top = session.evaluate_script("$('#library .kp-notebook-scroller-addon').get(0).scrollTop") scroll_height = session.evaluate_script("$('#library .kp-notebook-scroller-addon').get(0).scrollHeight") offset_height = session.evaluate_script("$('#library .kp-notebook-scroller-addon').get(0).offsetHeight") log "Scroll top:#{scroll_top} height:#{scroll_height} offset_height:#{offset_height}" scroll_top else log "Couldn't find the node '#library .kp-notebook-scroller-addon'" 0 end end
fetch()
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 7 def fetch go_to_kindle_highlights_page fetch_library_ids fetch_kindle_highlights end
fetch_book_with_highlights(library_id)
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 67 def fetch_book_with_highlights(library_id) log "Fetching highlights for the book #{library_id}" session.first("##{library_id}").click wait_for_selector('#annotations .kp-notebook-annotation-container', wait_time: 10) title = doc.css('#annotations .kp-notebook-annotation-container h3.kp-notebook-metadata').try!(:text) highlights_count, notes_count = fetch_highlights_and_notes snapshot_page("Saving page for [#{title}] (#{library_id}) highlights:#{highlights_count} notes:#{notes_count}") if title.present? self.loaded_library_ids << library_id else self.failed_library_ids << library_id log "[ERROR] Failed to load #{library_id} or this book doesn't have any highlights and notes" end end
fetch_highlights_and_notes()
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 82 def fetch_highlights_and_notes highlights_count = notes_count = nil 10.times do sleep(1) highlights_count = doc.css('#annotations .kp-notebook-annotation-container #kp-notebook-highlights-count').try!(:text) notes_count = doc.css('#annotations .kp-notebook-annotation-container #kp-notebook-notes-count').try!(:text) break if highlights_count != '--' && notes_count != '--' end [highlights_count, notes_count] end
fetch_kindle_highlights()
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 57 def fetch_kindle_highlights library_ids.each_with_index do |library_id,i| break if limit && limit < i+1 next if loaded_library_ids.include?(library_id) fetch_book_with_highlights(library_id) end report_failed_ids snapshot_page end
fetch_library_ids()
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 23 def fetch_library_ids last_scroll_top = check_library_scroll 20.times do scroll_library_pane(last_scroll_top + 20000) sleep(2) new_scroll_top = check_library_scroll break if limit && limit < doc.css('#library #kp-notebook-library > .a-row').size break if last_scroll_top == new_scroll_top last_scroll_top = new_scroll_top end snapshot_page self.library_ids = doc.css('#library #kp-notebook-library > .a-row').map{|e| e['id'] } self.loaded_library_ids ||= [] self.failed_library_ids ||= [] log "Number of library ids is #{library_ids.size}" end
go_to_kindle_highlights_page()
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 13 def go_to_kindle_highlights_page unless session.current_url == KINDLE_HIGHLIGHT_URL log "Visiting kindle highlights page" session.visit KINDLE_HIGHLIGHT_URL end wait_for_selector('#library') check_library_scroll snapshot_page end
load()
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 97 def load books = [] store.list_html_files.each do |file| parser = KindleManager::HighlightsParser.new(file) books += parser.parse end books.reject(&:invalid?).sort_by{|b| [-b.last_annotated_on.to_time.to_i, -b.fetched_at.to_i] }.uniq(&:asin) end
report_failed_ids()
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 93 def report_failed_ids log("May have failed with #{failed_library_ids.inspect}. Retry with client.adapter.session.first('#B000000000').click") if failed_library_ids.size > 0 end
scroll_library_pane(target_scroll_top)
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 53 def scroll_library_pane(target_scroll_top) session.evaluate_script("$('#library .kp-notebook-scroller-addon').get(0).scrollTop = #{target_scroll_top}") end
snapshot_page(message = nil)
click to toggle source
# File lib/kindle_manager/adapters/highlights_adapter.rb, line 106 def snapshot_page(message = nil) store.record_page log(message.presence || "Saving page") end