Class: PintrestApi::Core
- Inherits:
-
Object
- Object
- PintrestApi::Core
- Includes:
- Capybara::DSL
- Defined in:
- lib/pintrest_api/core.rb
Overview
This class is the core of the pintrest api and handles all url visiting and such
Constant Summary collapse
- PINTREST_URL =
'http://www.pinterest.com/'
Instance Attribute Summary collapse
-
#session ⇒ Object
Returns the value of attribute session.
Class Method Summary collapse
- .click(css) ⇒ Object
- .get_with_ajax_scroll(css_selector) ⇒ Object
-
.html ⇒ Object
Returns the current session’s page.
-
.new_session ⇒ Object
Create a new PhantomJS session in Capybara.
- .scroll_page ⇒ Object
- .session_visit(url) ⇒ Object
- .stream_with_ajax_scroll(css_selector) ⇒ Object
- .visit(url) ⇒ Object
- .visit_page(user_name) ⇒ Object
Instance Attribute Details
#session ⇒ Object
Returns the value of attribute session.
11 12 13 |
# File 'lib/pintrest_api/core.rb', line 11 def session @session end |
Class Method Details
.click(css) ⇒ Object
45 46 47 |
# File 'lib/pintrest_api/core.rb', line 45 def click(css) @session.find(css).click end |
.get_with_ajax_scroll(css_selector) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/pintrest_api/core.rb', line 80 def get_with_ajax_scroll(css_selector) old_items_count = 0 items = [] until (items.count === old_items_count) && items.count > 0 old_items_count = items.count scroll_page if old_items_count > 0 new_items = Nokogiri::HTML.parse(html).css css_selector items = new_items if old_items_count === 0 || new_items.count > old_items_count puts "New Count: #{items.count}\nOld Count: #{old_items_count}" end items end |
.html ⇒ Object
Returns the current session’s page
76 77 78 |
# File 'lib/pintrest_api/core.rb', line 76 def html @session.body end |
.new_session ⇒ Object
Create a new PhantomJS session in Capybara
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pintrest_api/core.rb', line 50 def new_session # Register PhantomJS (aka poltergeist) as the driver to use .register_driver :poltergeist do |app| ::Poltergeist::Driver.new(app, timeout: 60, js_errors: false) end # Use CSS as the default selector for the find method .default_selector = :css # Start up a new thread @session = ::Session.new(:poltergeist) # Report using a particular user agent @session.driver.headers = { 'User-Agent' => "Mozilla/5.0 (Macintosh; Intel Mac OS X)" } # Return the driver's session @session end |
.scroll_page ⇒ Object
70 71 72 73 |
# File 'lib/pintrest_api/core.rb', line 70 def scroll_page @session.execute_script 'window.scrollTo(0,100000)' sleep 2 end |
.session_visit(url) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pintrest_api/core.rb', line 24 def session_visit(url) url = PINTREST_URL + url if url !~ /pinterest/ begin @session.visit url sleep 3 rescue ::Poltergeist::TimeoutError puts 'Gotten blocked by pintrest waiting 2 min to try again' sleep 120 session_visit url end end |
.stream_with_ajax_scroll(css_selector) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/pintrest_api/core.rb', line 98 def stream_with_ajax_scroll(css_selector) old_items_count = 0 items = [] until (items.count === old_items_count) && items.count > 0 old_items_count = items.count scroll_page if old_items_count > 0 items = Nokogiri::HTML.parse(html).css css_selector if old_items_count === 0 || items.count > old_items_count yield items[old_items_count..-1] if items.count > old_items_count puts "New Count: #{items.count}\nOld Count: #{old_items_count}" end items end |
.visit(url) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/pintrest_api/core.rb', line 38 def visit(url) new_session @session.visit url sleep 5 @session end |
.visit_page(user_name) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/pintrest_api/core.rb', line 17 def visit_page(user_name) new_session @session.visit PINTREST_URL + user_name sleep 5 @session end |