Class: PinterestDL::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/pinterest_dl/search.rb

Constant Summary collapse

S_URL =
'https://www.pinterest.com/resource/BaseSearchResource/get/'

Instance Method Summary collapse

Constructor Details

#initialize(client: PinterestDL::Client.new, config: PinterestDL.config) ⇒ Search

Returns a new instance of Search.



11
12
13
14
# File 'lib/pinterest_dl/search.rb', line 11

def initialize(client: PinterestDL::Client.new, config: PinterestDL.config)
  @client = client
  @config = config
end

Instance Method Details

#call(query, limit: 25, pages: 5) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pinterest_dl/search.rb', line 16

def call(query, limit: 25, pages: 5)
  results = []
  bookmarks = nil
  pages_fetched = 0

  loop do
    page = fetch_page(query, bookmarks: bookmarks)
    pins = page[:pins]
    results.concat(pins)
    bookmarks = page[:bookmarks]
    pages_fetched += 1

    break if results.size >= limit
    break if bookmarks.nil? || bookmarks.empty? || bookmarks == ['-end-']
    break if pages_fetched >= pages
    break if pins.empty?
  end

  results.first(limit)
end