Created
March 4, 2019 08:05
-
-
Save shamritskiy3468/f10d53f6b7b51b03de9853e59cc938d8 to your computer and use it in GitHub Desktop.
Shop 7490
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EducationGarfieldParser < Scripting::CustomParser | |
@@document | |
@@base_prod | |
@@ctx | |
# def initialize | |
# | |
# end | |
def parse(ctx) | |
@@ctx = ctx | |
@@base_prod = ctx.base_product | |
@@document = ctx.doc | |
if page_valid? | |
if @@base_prod | |
products = products_json | |
if products["OFFERS"] | |
products["OFFERS"].each do |single_product| | |
ctx.add_product(create_single_product(single_product)) | |
end | |
end | |
end | |
end | |
ctx.products.empty? | |
end | |
def page_valid? | |
return true if @@document.xpath('//div[contains(@class, "bx-catalog-element bx-blue")]/@itemtype').text.strip.include?('//schema.org/Product') | |
false | |
end | |
def create_single_product(product) | |
p = @@base_prod.dup | |
p[NAME] = product["NAME"] | |
p[PRICE] = product["ITEM_PRICES"].last["BASE_PRICE"] | |
p[SKU] = p[KEY] = product["ID"] | |
# not valid confirmation on page | |
# weight = separate_prod.xpath('.//@title').text.strip | |
# compare_hash = get_prices_with_weights | |
# p[NAME] += @@base_prod[NAME] + ', ' + weight | |
# p[PRICE] = compare_hash[weight] | |
# p[SKU] = separate_prod.xpath('.//@data-art').text.strip | |
# p[KEY] = p[SKU] | |
# p[PROMO_NAME] = separate_prod.xpath('.//@data-discountstatus').text.strip.include?("item_discounted") ? "Акция" : nil | |
# p[REGULAR_PRICE] = separate_prod.xpath('.//@data-old-price').text.strip.chomp('руб.') if !separate_prod.xpath('.//@data-old-price').empty? | |
# p[STOCK] = separate_prod.xpath(".//@data-availstatus").equal?("") ? "out_of stock" : "in stock" | |
# hash work attemps here | |
# end | |
p | |
end | |
def prices_from_list | |
@@document.xpath('//span[@itemprop="offers"]//meta[@itemprop="price"]//@content').each_with_object([]) { |node, arr| arr << node.text.strip } | |
end | |
def products_json | |
script = find_description_script | |
json = prepare_json(script).tr("'", "\"") | |
JSON.parse(json) | |
end | |
def multi_product_count | |
products_json["OFFERS"].size | |
end | |
def find_description_script | |
@@ctx.todo[:javascript].each_with_object([]) { |scr, result| result << scr.to_s if scr.include?("JCCatalogElement") }.first.to_s | |
end | |
def prepare_json(script) | |
script.scan(/(?<=JCCatalogElement\()[^)]+/).first | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment