Class: PinterestDL::Board

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

Constant Summary collapse

B_URL =
'https://www.pinterest.com/resource/BoardFeedResource/get/'
BOARD_URL_PATTERN =
%r{pinterest\.[a-z.]+/(?<username>[^/]+)/(?<slug>[^/]+)/?(?<section>[^/]+)?/?\z}

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Board.



12
13
14
15
# File 'lib/pinterest_dl/board.rb', line 12

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

Instance Method Details

#call(board_url, limit: 50, pages: 10) ⇒ Object



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

def call(board_url, limit: 50, pages: 10)
  match = BOARD_URL_PATTERN.match(board_url)
  raise PinterestDL::InvalidURLError, "Not a Pinterest board URL: #{board_url}" unless match

  results = []
  bookmarks = nil
  pages_fetched = 0

  loop do
    page = fetch_page(match, bookmarks: bookmarks)
    results.concat(page[: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 page[:pins].empty?
  end

  results.first(limit)
end