Skip to content

Instantly share code, notes, and snippets.

@emir
Created May 26, 2014 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emir/d336a364846986c5e2cb to your computer and use it in GitHub Desktop.
Save emir/d336a364846986c5e2cb to your computer and use it in GitHub Desktop.
Enmoda.com'dan WooCommerce'e ürün ekleyen Ruby ile yazılmış en tırrık script.
require "nokogiri"
require "to_slug"
require "open-uri"
require "mysql"
#GET CATEGORY'S PRODUCTS
category_url = "http://www.enmoda.com/kadin/jean/"
cat_doc = Nokogiri::HTML(open(category_url))
l = cat_doc.css('.product-list li a').map { |link| link['href'] }
#SOME CONFIG
base_url = "http://karsiyakali.dev:8888/"
con = Mysql.new 'localhost', 'root', 'root', 'shop'
p = 598 #last post_id on database
l.each do |s|
puts p, s
wiki_url = "http://www.enmoda.com" << s
doc = Nokogiri::HTML(open(wiki_url))
title = doc.at_css(".product-name").text
price = doc.at_css(".price .b").text
if doc.at_css('.product-detail ul')
detail = doc.at_css(".product-detail ul").text
else
detail = ""
end
#slug = title.to_slug
slug = s.gsub(/\//, "")
time = Date.today
#guid = http://karsiyakali.im/?post_type=product&#038;p=14
#puts "Slug #{slug}, Ürün #{title}, Fiyatı: #{price}, Detayları: #{detail}"
guid = base_url + "?post_type=product&p=" + p.to_s
con.query("INSERT INTO wp_posts (post_title, post_content, post_excerpt, to_ping, pinged, post_content_filtered, post_name,
guid, post_type, post_mime_type, post_status, post_date, post_date_gmt, post_author)
VALUES('#{title}', '#{detail}', '', '', '', '', '#{slug}', '#{guid}', 'product', '', 'publish',
'#{time}', '#{time}', 1)")
product_id = con.insert_id
con.query("INSERT INTO wp_term_relationships (object_id, term_taxonomy_id)
VALUES ('#{product_id}', '16')") #13erkek 16kadın
con.query("INSERT INTO wp_term_relationships (object_id, term_taxonomy_id)
VALUES ('#{product_id}', '20')") #14gömlek 17kadın-gömlek 19erkekjean 20kadınjean
#Featured image
path = doc.at_css('.cloud-zoom img')['src']
uri = URI.parse(path)
image_name = uri.path.split('/').last #example_123.jpg
image_ext = image_name.split('.').last #.jpg
upload_dir = "uploads/#{image_name}" #Where I would like to save
open(upload_dir, 'wb') do |file|
file << open(path).read
end
image_guid = base_url + "wp-content/" + upload_dir
deneme = "http://karsiyakali.dev:8888/wp-content/uploads/" + image_name
#images => guid, attachment, image/jpeg, post_parent(last_insert_id), post_status=inherit
con.query("INSERT INTO wp_posts (post_title, post_content, post_excerpt, to_ping, pinged, post_content_filtered, post_name,
guid, post_type, post_mime_type, post_status, post_date, post_date_gmt, post_author, post_parent)
VALUES('#{title}', '', '', '', '', '', '#{image_name}', '#{deneme}', 'attachment', 'image/jpeg', 'inherit',
'#{time}', '#{time}', 1, '#{product_id}')")
thumb_id = con.insert_id
terms = { '_thumbnail_id' => thumb_id, '_visibility' => 'visible', '_stock_status' => 'instock', 'total_sales' => 0,
'_downloadable' => 'no', '_virtual' => 'no', '_regular_price' => price, '_sale_price' => '', '_purchase_note' => '',
'_featured' => 'no', '_weight' => '', '_length' => '', '_width' => '', '_height' => '', '_sku' => 10, '_product_attributes' => '',
'_sale_price_dates_from' => '', '_sale_price_dates_to' => '', '_price' => price, '_sold_individually' => '', '_manage_stock' => 'no',
'_backorders' => 'no', '_stock' => '', '_product_image_gallery' => ''}
terms.each do |key, value|
con.query("INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
VALUES('#{product_id}', '#{key}', '#{value}')")
end
#image
con.query("INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
VALUES('#{thumb_id}', '_wp_attached_file', '#{image_name}')")
puts title
p = thumb_id + 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment