Module: PinterestDL

Defined in:
lib/pinterest-dl.rb

Constant Summary collapse

USER_AGENT =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'.freeze

Class Method Summary collapse

Class Method Details

.get_image_url(pin_url) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pinterest-dl.rb', line 22

def get_image_url(pin_url)
  html = fetch_html(pin_url)
  return nil unless html

  # 1 -> json-ld
  url = extract_json_ld(html, 'ImageObject')
  return url if url
  # 2 -> open graph
  match = html.match(%r{<meta[^>]*property="og:image"[^>]*content="([^"]+)"})
  if match
    url = match[1]
    puts "image url: #{url}"
    return url
  end
  # 3 -> direct i.pinimg.com patterns
  match = html.match(%r{https://i\.pinimg\.com/(?:originals|474x|236x|736x)/[^"\s]+})
  if match
    url = match[0]
    reset = "\e[0m"
    green = "\e[32m"
    puts "#{green}image url :#{reset} #{url}"
    return url
  end
  puts 'No found:)'
  nil
end

.get_video_url(pin_url) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pinterest-dl.rb', line 50

def get_video_url(pin_url)
  html = fetch_html(pin_url)
  return nil unless html

  # 1 -> json-ld
  url = extract_json_ld(html, 'VideoObject')
  if url
    url = url.gsub('\\u002F', '/').gsub('\\', '')
    puts "ok : #{url}"
    return url
  end
  # 2 -> Open Graph video
  match = html.match(%r{<meta[^>]*property="og:video"[^>]*content="([^"]+)"})
  if match
    url = match[1].gsub('\\u002F', '/').gsub('\\', '')
    puts "video url: #{url}"
    return url
  end
  # 3 -> contentUrl in page source
  match = html.match(/"contentUrl"\s*:\s*"([^"]+\.mp4[^"]*)"/)
  if match
    url = match[1].gsub('\\u002F', '/').gsub('\\', '')
    green = "\e[32m"
    reset = "\e[0m"
    puts "#{green}video url :#{reset} #{url}"
    return url
  end

  puts 'No found'
  nil
end

.search(query, limit: 25) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pinterest-dl.rb', line 81

def search(query, limit: 25)
  source_path = "/search/pins/?q=#{URI.encode_www_form_component(query)}"
  home_uri = URI("https://www.pinterest.com#{source_path}")
  http = Net::HTTP.new(home_uri.host, home_uri.port)
  http.use_ssl = true
  http.open_timeout = 10
  http.read_timeout = 20
  home_req = Net::HTTP::Get.new(home_uri, 'User-Agent' => USER_AGENT)
  home_resp = http.request(home_req)
  cookies = (home_resp.get_fields('set-cookie') || []).map { |c| c.split(';').first }
  cookie_header = cookies.join('; ')
  csrf = cookies.find { |c| c.start_with?('csrftoken=') }&.split('=', 2)&.last || 'undefined'
  data = {
    options: { query: query, scope: 'pins', page_size: limit },
    context: {}}.to_json
  res_uri = URI('https://www.pinterest.com/resource/BaseSearchResource/get/')
  res_uri.query = URI.encode_www_form('source_url' => source_path, 'data' => data)
  req = Net::HTTP::Get.new(res_uri)
  req['User-Agent'] = USER_AGENT
  req['Accept'] = 'application/json, text/javascript, */*, q=0.01'
  req['X-Requested-With'] = 'XMLHttpRequest'
  req['X-Pinterest-PWS-Handler'] = 'www/search/[scope].js'
  req['X-CSRFToken'] = csrf
  req['Referer'] = home_uri.to_s
  req['Cookie'] = cookie_header
  resp = http.request(req)
  unless resp.is_a?(Net::HTTPSuccess)
    puts "\e[31msearch http err!:\e[0m #{resp.code}"
    return []
  end
  json = begin
    JSON.parse(resp.body)
  rescue JSON::ParserError
    nil
  end
  return [] unless json
  raw_results = (json.dig('resource_response', 'data', 'results') || []).first(limit)
  pins = raw_results.filter_map do |r|
    id = r['id']
    next nil unless id
    orig_image = r.dig('images', 'orig', 'url')
    fallback_image = r['images']&.values&.map { |v| v.is_a?(Hash) ? v['url'] : nil }&.compact&.last
    image_url = orig_image || fallback_image
    video_url = r.dig('videos', 'video_list')&.values&.first&.dig('url')
    {
      id: id,
      pin_url: "https://www.pinterest.com/pin/#{id}/",
      title: r['title'] || r['grid_title'],
      description: r['description'],
      image_url: image_url,
      video_url: video_url
    }
  end
  green = "\e[32m"
  reset = "\e[0m"
  puts "#{green}found #{pins.size} pin(s) for \"#{query}\"#{reset}"
  pins
rescue StandardError => e
  puts "\e[31msearch err!:\e[0m #{e.message}"
  []
end

.search_images(query, limit: 25) ⇒ Object



145
146
147
# File 'lib/pinterest-dl.rb', line 145

def search_images(query, limit: 25)
  search(query, limit: limit).filter_map { |p| p[:image_url] }
end

.search_json(query, limit: 25) ⇒ Object



142
143
144
# File 'lib/pinterest-dl.rb', line 142

def search_json(query, limit: 25)
  JSON.pretty_generate(search(query, limit: limit))
end

.search_videos(query, limit: 25) ⇒ Object



148
149
150
# File 'lib/pinterest-dl.rb', line 148

def search_videos(query, limit: 25)
  search(query, limit: limit).filter_map { |p| p[:video_url] }
end