Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'nokogiri'
- require 'open-uri'
- require 'open_uri_redirections'
- @key = 0
- @url = "https://chocomart.kz"
- @main = Nokogiri::HTML(open(@url, "User-Agent" => "Ruby/#{RUBY_VERSION}","From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/"))
- @text = @main.css("a.e-menu__item-link")
- @text.each do |f|
- @url_cat = f['href']
- @main = Nokogiri::HTML(open(@url+@url_cat, "User-Agent" => "Ruby/#{RUBY_VERSION}","From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/"))
- @cont = @main.css("div.b-category__wrapper")
- @cats = @cont.css("a.smart_navigation")
- @cats.each do |c|
- @url_pr = c['href']
- @page = 1
- while true
- @main = Nokogiri::HTML(open(@url+@url_pr+"?page="+@page.to_s, "User-Agent" => "Ruby/#{RUBY_VERSION}","From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/"))
- @cont = @main.css("a.e-product__link")
- if @cont.length == 0
- break
- end
- @page += 1
- @cont.each do |pr|
- @key += 1
- concat ((@key).to_s+" "+(pr['href']).to_s+"<br>").to_s.html_safe
- @main = Nokogiri::HTML(open(pr['href'], "User-Agent" => "Ruby/#{RUBY_VERSION}","From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/"))
- @categories = @main.css("div.breadcrumb_item")
- @parent_id = 0;
- @vkey = 0
- @categories.each do |cat|
- @vkey += 1
- if @vkey == 1
- next
- end
- @title = cat.css("span")[0].inner_html
- @title = str_simpl(@title)
- @category = ProductCategory.where(:title => @title)[0]
- if @category.blank?
- @category = ProductCategory.new
- @category.title = @title
- if @parent_id > 0
- @category.parent_id = @parent_id
- end
- @category.save();
- end # if category.blank
- @parent_id = @category.id
- end #categories loop
- #add product
- @article = @main.css("div.good_info_header h1")
- if @article.blank?
- next
- end
- @art = @main.css("div.good_info_header div.added_id")[0]['data-id']
- @product = Product.where(:choco_id => "choco_"+@art)[0]
- if @product.blank?
- @product = Product.new
- @product.service = "choco"
- @product.choco_id = @product.service+"_"+@art
- @user = rand(2)+1;
- @product.user_id = @user
- end
- @user = @product.user_id
- @product.title = str_simpl(@article.inner_html)
- @art = str_simpl(@main.css("div.q7_price span")[2].inner_html)
- @product.price = @art.to_i
- @product.sku = 10
- @product.min_order = 1
- @product.active = 1
- @img = @main.css("a.cloud-zoom/img")
- if @img.any? && @product.help_image.blank?
- @img = @img[0]['src']
- unless @img.blank?
- File.open('/home/akzhol/choco/'+@product.choco_id+'.jpg', 'wb') do |fo|
- fo.write open('https://www.chocomart.kz'+@img, :allow_redirections => :all).read
- end
- # copy('https://www.chocomart.kz'.$img, '/home/akzhol/choco/'.@product.choco_id.'.jpg');
- @product.help_image = @product.choco_id+'.jpg';
- end
- end #if img.any statement
- @product.product_category_id = @parent_id;
- if @product.save
- #added product
- @articles = @main.css('div.char_text')
- @articles.each do |article|
- @title = str_simpl(sanitize(article.css('div.char_name').to_s, :tags => []))
- @value = str_simpl(sanitize(article.css('div.char_price').to_s, :tags => []))
- @u_attr = UserAttribute.where("user_id = ? AND title = ?", @user, @title)[0]
- if @u_attr.blank?
- @u_attr = UserAttribute.new
- @u_attr.title = @title
- @u_attr.user_id = @user
- @u_attr.attribute_type_id = 1
- @u_attr.save()
- end #if u_attr not exists
- @product_attribute = ProductAttribute.where("product_id = ? AND user_attribute_id = ?", @product.id, @u_attr.id)[0]
- if @product_attribute.blank?
- @product_attribute = ProductAttribute.new
- @product_attribute.product_id = @product.id
- @product_attribute.user_attribute_id = @u_attr.id
- end #if product_attribute not exists
- @product_attribute.value = @value
- @product_attribute.save()
- concat (@product_attribute.id.to_s+" "+@title+" "+@value+"<br>").html_safe
- end #attributes loop
- concat (@product.id.to_s+"<br>").html_safe
- end #if product save
- end #each product loop
- end #while true loop
- end #each category(cats) loop
- end #each ment category loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement