Sha256: 1380508ca17e80f64ef87c94092c51cd27a5008fb65d7306c754c3225ad9f7c1

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require "instascraper/version"
require "instascraper/bookmark"
require 'mechanize'

class Instascraper
  def initialize(username, password = '')
    @agent = Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari' }

    @agent.get('http://www.instapaper.com/user/login/') do |page|
      form = page.form
      form.texts.first.value = username
      form.password = password
      form.submit
    end
  end
  
  def bookmarks(folder = nil)
    if folder
      home = @agent.get('http://www.instapaper.com/u/')
      path = home.link_with(:text => folder).href
    else
      path = '/u'
    end
    
    bookmarks = []
    more_pages = true
    current_page = nil
    
    while more_pages do
      page = @agent.get("http://www.instapaper.com#{path}/#{current_page}")

      page.parser.css('.tableViewCell').each do |bookmark|
        bookmark.css('.tableViewCellTitleLink').first['href']
        
        bookmarks << Bookmark.new(bookmark.css('.tableViewCellTitleLink').first['href'], bookmark.css('.tableViewCellTitleLink').first.text)  
      end

      if page.link_with :text => /Older items/
        current_page = (current_page ? current_page + 1 : 1 )
      else
        more_pages = false
      end
    end

    return bookmarks
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
instascraper-0.0.1 lib/instascraper.rb