Skip to content

Extract Van Gogh paintings #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "https://rubygems.org"

ruby ">= 3.0"

gem "byebug", ">= 11.1"
gem "ferrum", "~> 0.17.1"
gem "rspec", ">= 3.12"
49 changes: 49 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
base64 (0.3.0)
byebug (12.0.0)
concurrent-ruby (1.3.5)
diff-lcs (1.6.2)
ferrum (0.17.1)
addressable (~> 2.5)
base64 (~> 0.2)
concurrent-ruby (~> 1.1)
webrick (~> 1.7)
websocket-driver (~> 0.7)
public_suffix (6.0.2)
rspec (3.13.1)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.5)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.5)
webrick (1.9.1)
websocket-driver (0.8.0)
base64
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
byebug (>= 11.1)
ferrum (~> 0.17.1)
rspec (>= 3.12)

RUBY VERSION
ruby 3.3.0p0

BUNDLED WITH
2.5.3
38 changes: 38 additions & 0 deletions extract_van_gogh_paintings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'json'
require 'ferrum'
require 'byebug'

def extract_paintings(file_path)
browser = Ferrum::Browser.new(headless: true)
browser.goto("file://#{File.expand_path(file_path)}")

paintings = []

browser.css('div.iELo6').each do |painting_div|
link_tag = painting_div.at_css('a')
google_link = link_tag&.attribute('href')&.strip
google_link = "https://www.google.com#{google_link}" if google_link && !google_link.start_with?("http")

thumbnail_tag = link_tag.at_css('img.taFZJe')
image = thumbnail_tag&.attribute('data-src')&.strip || thumbnail_tag&.attribute('src')&.strip

painting_parent_div = painting_div.at_css('div.KHK6lb')
painting_name = painting_parent_div.at_css('div.pgNMRc')&.text&.strip
extensions_text = painting_parent_div.at_css('div.cxzHyb')&.text&.strip

painting_hash = { name: painting_name }
painting_hash[:extensions] = [extensions_text] unless extensions_text.nil? || extensions_text.empty?
painting_hash[:link] = google_link
painting_hash[:image] = image

paintings << painting_hash
end

browser.quit
{ artworks: paintings }
end

File.write(
'van-gogh-paintings-extracted.json',
JSON.pretty_generate(extract_paintings('files/van-gogh-paintings.html'))
)
68 changes: 68 additions & 0 deletions spec/extract_van_gogh_paintings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'json'

RSpec.describe 'Van Gogh Paintings Extraction' do
let(:extracted_data) { JSON.parse(File.read('./van-gogh-paintings-extracted.json'))['artworks'] }

it 'matches the expected array' do
expected_data = JSON.parse(File.read('./files/expected-array.json'))['artworks']
expect(extracted_data).to eq(expected_data)
end

it 'ensures each artwork has required keys in correct order' do
extracted_data.each do |artwork|
keys = artwork.keys
expect(keys.first).to eq('name')
expect(keys.last).to eq('image')
expect(keys).to include('link')
end
end

it 'ensures extensions key is omitted when empty' do
extracted_data.each do |artwork|
if artwork['extensions'].nil? || artwork['extensions'].empty?
expect(artwork.key?('extensions')).to be false
end
end
end

it 'ensures extensions is an array if present' do
extracted_data.each do |artwork|
if artwork.key?('extensions')
expect(artwork['extensions']).to be_an(Array)
end
end
end

it 'ensures all Google links start with https://www.google.com' do
extracted_data.each do |artwork|
expect(artwork['link']).to start_with('https://www.google.com')
end
end

it 'ensures images are present for each artwork' do
extracted_data.each do |artwork|
expect(artwork['image']).not_to be_nil
expect(artwork['image']).not_to be_empty
end
end

it 'ensures name is always present' do
extracted_data.each do |artwork|
expect(artwork['name']).not_to be_nil
expect(artwork['name']).not_to be_empty
end
end

it 'ensures image is either a URL or a base64 data string' do
extracted_data.each do |artwork|
expect(artwork['image']).to match(/^https?:\/\/|^data:/)
end
end

it 'does not include unexpected keys' do
allowed_keys = %w[name extensions link image]
extracted_data.each do |artwork|
expect(artwork.keys - allowed_keys).to be_empty
end
end
end
Loading