Advertisement
Guest User

Chocomart

a guest
Jul 9th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1.  
  2. require 'rubygems'
  3. require 'nokogiri'
  4. require 'open-uri'
  5. require 'open_uri_redirections'
  6. @key = 0
  7.  
  8. @url = "https://chocomart.kz"
  9. @main = Nokogiri::HTML(open(@url, "User-Agent" => "Ruby/#{RUBY_VERSION}","From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/"))
  10. @text = @main.css("a.e-menu__item-link")
  11. @text.each do |f|
  12. @url_cat = f['href']
  13. @main = Nokogiri::HTML(open(@url+@url_cat, "User-Agent" => "Ruby/#{RUBY_VERSION}","From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/"))
  14. @cont = @main.css("div.b-category__wrapper")
  15. @cats = @cont.css("a.smart_navigation")
  16. @cats.each do |c|
  17. @url_pr = c['href']
  18. @page = 1
  19. while true
  20. @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/"))
  21. @cont = @main.css("a.e-product__link")
  22. if @cont.length == 0
  23. break
  24. end
  25. @page += 1
  26. @cont.each do |pr|
  27. @key += 1
  28. concat ((@key).to_s+" "+(pr['href']).to_s+"<br>").to_s.html_safe
  29. @main = Nokogiri::HTML(open(pr['href'], "User-Agent" => "Ruby/#{RUBY_VERSION}","From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/"))
  30. @categories = @main.css("div.breadcrumb_item")
  31.  
  32. @parent_id = 0;
  33. @vkey = 0
  34. @categories.each do |cat|
  35. @vkey += 1
  36. if @vkey == 1
  37. next
  38. end
  39. @title = cat.css("span")[0].inner_html
  40. @title = str_simpl(@title)
  41. @category = ProductCategory.where(:title => @title)[0]
  42. if @category.blank?
  43. @category = ProductCategory.new
  44. @category.title = @title
  45. if @parent_id > 0
  46. @category.parent_id = @parent_id
  47. end
  48. @category.save();
  49. end # if category.blank
  50. @parent_id = @category.id
  51. end #categories loop
  52.  
  53. #add product
  54. @article = @main.css("div.good_info_header h1")
  55. if @article.blank?
  56. next
  57. end
  58.  
  59. @art = @main.css("div.good_info_header div.added_id")[0]['data-id']
  60.  
  61. @product = Product.where(:choco_id => "choco_"+@art)[0]
  62. if @product.blank?
  63. @product = Product.new
  64. @product.service = "choco"
  65. @product.choco_id = @product.service+"_"+@art
  66. @user = rand(2)+1;
  67. @product.user_id = @user
  68. end
  69. @user = @product.user_id
  70. @product.title = str_simpl(@article.inner_html)
  71.  
  72. @art = str_simpl(@main.css("div.q7_price span")[2].inner_html)
  73. @product.price = @art.to_i
  74. @product.sku = 10
  75. @product.min_order = 1
  76. @product.active = 1
  77.  
  78. @img = @main.css("a.cloud-zoom/img")
  79. if @img.any? && @product.help_image.blank?
  80. @img = @img[0]['src']
  81. unless @img.blank?
  82. File.open('/home/akzhol/choco/'+@product.choco_id+'.jpg', 'wb') do |fo|
  83. fo.write open('https://www.chocomart.kz'+@img, :allow_redirections => :all).read
  84. end
  85. # copy('https://www.chocomart.kz'.$img, '/home/akzhol/choco/'.@product.choco_id.'.jpg');
  86. @product.help_image = @product.choco_id+'.jpg';
  87. end
  88. end #if img.any statement
  89.  
  90. @product.product_category_id = @parent_id;
  91.  
  92. if @product.save
  93. #added product
  94. @articles = @main.css('div.char_text')
  95. @articles.each do |article|
  96. @title = str_simpl(sanitize(article.css('div.char_name').to_s, :tags => []))
  97. @value = str_simpl(sanitize(article.css('div.char_price').to_s, :tags => []))
  98. @u_attr = UserAttribute.where("user_id = ? AND title = ?", @user, @title)[0]
  99. if @u_attr.blank?
  100. @u_attr = UserAttribute.new
  101. @u_attr.title = @title
  102. @u_attr.user_id = @user
  103. @u_attr.attribute_type_id = 1
  104. @u_attr.save()
  105. end #if u_attr not exists
  106. @product_attribute = ProductAttribute.where("product_id = ? AND user_attribute_id = ?", @product.id, @u_attr.id)[0]
  107. if @product_attribute.blank?
  108. @product_attribute = ProductAttribute.new
  109. @product_attribute.product_id = @product.id
  110. @product_attribute.user_attribute_id = @u_attr.id
  111. end #if product_attribute not exists
  112. @product_attribute.value = @value
  113. @product_attribute.save()
  114. concat (@product_attribute.id.to_s+" "+@title+" "+@value+"<br>").html_safe
  115. end #attributes loop
  116. concat (@product.id.to_s+"<br>").html_safe
  117. end #if product save
  118. end #each product loop
  119. end #while true loop
  120. end #each category(cats) loop
  121. end #each ment category loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement