Skip to content

Instantly share code, notes, and snippets.

@mimosz
Created May 7, 2013 07:20
Show Gist options
  • Save mimosz/5530810 to your computer and use it in GitHub Desktop.
Save mimosz/5530810 to your computer and use it in GitHub Desktop.
达尔文计划,整理宝贝SKU配图
user = User.all[11]
def get_value(cid, properties)
prop_ids = properties.split(':')
prop = ItemProp.where(_id: "#{cid}-#{prop_ids[0]}").first
return nil if prop.nil?
prop_value = prop.prop_values.where(vid: prop_ids[1]).first
return nil if prop_value.nil?
prop_value.name
end
def get_sku(item, properties)
sku = item.skus.only(:properties, :outer_id).where(properties: Regexp.new(properties)).first
return nil if sku.nil?
sku.outer_id
end
header_row = ['淘宝ID', '商家编码', '属性', 'SKU', '图片地址']
CSV.open('nb_item_imgs.csv', "wb:GB18030", col_sep: ',') do |csv|
csv << header_row
items = user.items.only(:prop_imgs, :skus, :num_iid, :outer_id, :cid)
items.each do |item|
item.prop_imgs.each do |prop|
csv << [item.num_iid, item.outer_id, get_value(item.cid, prop.properties), get_sku(item, prop.properties), prop.url]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment