Skip to content

Commit 701bb87

Browse files
committed
adds bulk skus processing for Menards
1 parent 4e6c7de commit 701bb87

File tree

7 files changed

+1198
-25
lines changed

7 files changed

+1198
-25
lines changed

app/connectors/menards.rb

+18-12
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,43 @@ def process_listings(nodes)
99
id = node[0]
1010
sku = node[1]
1111
next if Listing.record_exists?(sku)
12-
sleep(4) if index % 20 == 0
1312
puts "=== starting id: #{id} | iteration: #{index}"
14-
sleep(rand(0.1...3))
1513
visit_product_page(id)
14+
next if page_not_found?
1615
fetch_product_attributes(id)
17-
Listing.append_menards_url(@listing_attrs[:vendor_sku],
18-
@listing_attrs[:vendor_url])
16+
vendor_sku = @listing_attrs[:vendor_sku]
17+
vendor_url = @listing_attrs[:vendor_url]
18+
Listing.append_menards_url(
19+
vendor_sku,
20+
vendor_url)
21+
Listing.append_vendor_attrs(
22+
vendor_sku,
23+
@listing_attrs)
1924
end
2025
end
2126

27+
2228
def visit_product_page(menards_id)
2329
driver.visit("#{BASE_URL}p-#{menards_id}.html") unless menards_id.nil?
24-
raise PageNotFoundError if page_not_found?
2530
end
2631

2732
def fetch_product_attributes(menards_id)
28-
return if page_not_found?
2933
@listing_attrs =
3034
{ vendor: 'menards',
31-
vendor_url: driver.current_url,
32-
vendor_id: menards_id,
33-
vendor_sku: driver.doc.at('p.itemModelSku').children.last.text.strip,
34-
vendor_title: driver.doc.at('.itemCenterContent span h2').text,
35-
vendor_price: driver.doc.at('#totalItemPrice').children.first.text.strip }
35+
vendor_url: driver.current_url,
36+
vendor_id: menards_id,
37+
vendor_sku: driver.doc.at('p.itemModelSku').children.last.text.strip,
38+
vendor_title: driver.doc.at('.itemCenterContent span h2').text,
39+
vendor_price: driver.doc.at('#totalItemPrice').children.first.text.strip }
3640
end
3741

3842
private
3943

4044
def page_not_found?
4145
driver.current_url == BASE_URL ||
42-
driver.doc.at('h5.error').present?
46+
driver.doc.at('h5.error').present? ||
47+
driver.doc.at('h3.resettitle').present? &&
48+
driver.doc.at('h3.resettitle').text.include?('404 error')
4349
end
4450

4551
end

app/models/listing.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Listing < ActiveRecord::Base
1010

1111
def self.append_vendor_attrs(sku, attrs)
1212
listing = find_by(sku: sku) || new(sku: sku)
13+
listing.vendors_will_change!
1314
listing.fetch_vendor_data(attrs)
1415
listing.save!
1516
end
@@ -19,12 +20,12 @@ def self.record_exists?(sku)
1920
end
2021

2122
def fetch_vendor_data(attrs)
22-
vendors[attrs[:vendor]].merge!({
23+
vendors[attrs[:vendor]] = ({
2324
id: attrs[:vendor_id],
2425
sku: attrs[:vendor_sku],
2526
url: attrs[:vendor_url],
2627
title: attrs[:vendor_title],
27-
price: attrs[:vendor_price] }).to_json
28+
price: attrs[:vendor_price] })
2829
end
2930

3031
VENDORS.each do |vendor|

app/workers/menards_crawler.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ class MenardsCrawler
22

33
include Sidekiq::Worker
44

5-
def perform(ids)
6-
connector = Menards.new(PoltergeistCrawler.new)
7-
connector.process_listings(ids)
5+
# def perform(ids)
6+
# connector = Menards.new(PoltergeistCrawler.new)
7+
# connector.process_listings(ids)
8+
# end
9+
#
10+
def perform(id)
11+
c = Menards.new(PoltergeistCrawler.new)
12+
c.visit_product_page(id)
13+
c.fetch_menards_skus(id)
14+
Listing.append_menards_url(c.listing_attrs[:vendor_sku],
15+
c.listing_attrs[:vendor_url])
816
end
917

1018
end

config/sidekiq.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
:concurrency: 5
3+
:pidfile: tmp/pids/sidekiq.pid
4+
staging:
5+
:concurrency: 10
6+
production:
7+
:concurrency: 20
8+
:queues:
9+
- default
10+
- [myqueue, 2]

0 commit comments

Comments
 (0)