Module: PinterestDL::Extractors::PinData
- Defined in:
- lib/pinterest_dl/extractors/pin_data.rb
Constant Summary collapse
- SCRIPT_PATTERN =
%r{<script[^>]*id=["']__PWS_DATA__["'][^>]*>(.*?)</script>}m.freeze
Class Method Summary collapse
- .extract(html) ⇒ Object
- .find_pin_node(data) ⇒ Object
- .first_video_url(pin) ⇒ Object
- .parse_embedded_json(html) ⇒ Object
- .pin_like?(node) ⇒ Boolean
- .safe_parse(raw) ⇒ Object
Class Method Details
.extract(html) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/pinterest_dl/extractors/pin_data.rb', line 12 def extract(html) data = (html) return nil unless data pin = find_pin_node(data) return nil unless pin { image_url: pin.dig('images', 'orig', 'url'), video_url: first_video_url(pin), title: pin['title'] || pin['grid_title'], description: pin['description'] } end |
.find_pin_node(data) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/pinterest_dl/extractors/pin_data.rb', line 35 def find_pin_node(data) stack = [data] until stack.empty? node = stack.pop case node when Hash return node if pin_like?(node) stack.concat(node.values) when Array stack.concat(node) end end nil end |
.first_video_url(pin) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/pinterest_dl/extractors/pin_data.rb', line 56 def first_video_url(pin) video_list = pin.dig('videos', 'video_list') return nil unless video_list.is_a?(Hash) video_list.values.first&.dig('url') end |
.parse_embedded_json(html) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/pinterest_dl/extractors/pin_data.rb', line 27 def (html) html.to_s.scan(SCRIPT_PATTERN).each do |(raw)| parsed = safe_parse(raw) return parsed if parsed end nil end |
.pin_like?(node) ⇒ Boolean
51 52 53 54 |
# File 'lib/pinterest_dl/extractors/pin_data.rb', line 51 def pin_like?(node) images = node['images'] images.is_a?(Hash) && images.dig('orig', 'url') end |
.safe_parse(raw) ⇒ Object
63 64 65 66 67 |
# File 'lib/pinterest_dl/extractors/pin_data.rb', line 63 def safe_parse(raw) JSON.parse(raw) rescue JSON::ParserError nil end |