<commit_before># encoding: UTF-8
require 'csv'

class ImportTiresFromGane
  
  def initialize()
    @agent = Mechanize.new
    @final = "#{Rails.root}/vendor/products/listado-neumaticos.csv"
    @total = @no_leidos = []
    @taxons = Spree::Taxon.where(:parent_id => 2).map {|x| x.name}
    @tubes = %w(TL TT RU)
    @widths = Spree::TireWidth.all.map {|x| x.name}
    @series = Spree::TireSerial.all.map {|x| x.name}
    @llantas = Spree::TireInnertube.all.map {|x| x.name}
    @vel = Spree::TireSpeedCode.all.map {|x| x.name}
    @marcas = CSV.read("#{Rails.root}/db/datas/rodaben-marcas.csv").map {|x| x[0]}
  end
  
  def run
    #if login
      #read_from_gane
      #export_to_csv
      load_from_csv
    #end
  end
  
  def read_from_gane
    str = "http://galaicoasturianadeneumaticos.distritok.com/sqlcommerce/disenos/plantilla1/seccion/Catalogo.jsp?idIdioma=&idTienda=50&cPath=61"
    sch = ".//table[@class='result']//tr//td[@class='result_right']//a[@title='Siguiente ']"
    links = []
    until str.empty?
      page = @agent.get(str)
      page.search(".//table[@class='tableBox_output']//tr").each do |d|
        t = d.search("td[@width='900']//a").text
        r = d.search("td//span[@class='linCat']").map {|x| x.text}
        unless r.empty?
          if r[1] == "Consultar"
              p = k = pf = s = 0
          else
            s = r[0]
            p = r[1].to_s.delete("€").lstrip
            k = r[2].to_s.delete("%").lstrip
            pf = r[3].to_s.delete("€").lstrip
            puts "Stock es #{p}. PVP final es #{pf}"
          end
          @total << [t, s, p, k, pf]
        end
      end
      links.clear
      page.search(sch).each do |link|
        links << link[:href]
      end
      links = links.uniq
      str = links[0].to_s
      puts str
    end
  end
  
  def export_to_csv
    CSV.open(@final, "wb") do |row|
      @total.each do |element|
        #[nombre, stock, precio, descuento, precio final]
        row << element
      end
    end
  end
  
  def load_from_csv
    # [ancho, serie, llanta, vel, tube, marca, gr]
    result = []
    i = j =0
    hoy = Date.today
    productos = Spree::Product.find_by_sql("Select name from spree_products;").map {|x| x.name}.flatten
    CSV.foreach(@final) do |row|
      unless row[0].blank?
        if productos.include?(row[0]) # producto existe
          articulo = Spree::Product.find_by_name(row[0])
          articulo.update_attributes(
            :count_on_hand => set_stock(row[1]),
            :cost_price => row[4],
            :price => row[4] * 1.05 #falta de poner el precio de venta segun cliente
          )
          # actualizar los precios
        else
          result = read_format(row[0])
          i += 1
          # crear uno nuevo
          product = Spree::Product.new
          product.name = row[0]
          product.permalink = row[0].downcase.gsub(/\s+/, '-').gsub(/[^a-zA-Z0-9_]+/, '-')
          product.sku = hoy.year.to_s + hoy.month.to_s + hoy.day.to_s + "-" + i.to_s
          product.available_on = hoy - 1.day
          product.count_on_hand = set_stock(row[1])
          product.price = row[4] * 1.05 #falta de poner el precio de venta segun cliente
          product.cost_price = row[4]
          product.tire_width_id = set_width(result)
          product.tire_serial_id = set_serial(result)
          product.tire_innertube_id = set_innertube(result)
          product.tire_speed_code_id = set_speed_code(result)
          product.tire_rf = false
          product.tire_gr = result[6]
          product.tire_season = set_season(result)
          product.taxons << Spree::Taxon.find(set_catalog) #cargar categoria
          product.taxons << Spree::Taxon.find(set_brand(result)) #cargar marca
          if product.save!
            puts "Creado articulo #{row[0]}"
            j += 1
          end
        end
      end
    end
  end
  
  private
  
  def login
    login = '929'
    password = '<PASSWORD>'
    direccion = 'http://galaicoasturianadeneumaticos.distritok.com/'
    page = @agent.get(direccion)
    gane_form = page.form('login')
    gane_form.login = login
    gane_form.password = password
    gane_form.submit
  end
  
  def read_format(rueda)
    rueda = rueda.to_s
    if rueda =~ %r{(\d+)(?:/|:)(\d+)(?:\s|:)(\D)(?:\s|:)(\d+)(?:\s|:)(\D+)(?:\s|:)(\d+[A-Z])} #1
      g = [$1,$2,$3,$4,$5,$6]
      ancho = g[0]
      serie = g[1]
      llanta = g[3]
      tube = read_tube(g[4])
      vel = g[5].scan(/[A-Z]+/)[0]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\d+)(?:/|:)(\d+)(?:\s|:)(\D)(?:\s|:)(\d+[A-Z])(?:\s|:)(\D+)(?:\s|:)(\d+)(?:/|:)(\d+[A-Z])} #2
      g = [$1,$2,$3,$4,$5,$6,$7]
      ancho = g[0]
      serie = g[1]
      llanta = g[3]
      tube = g[4]
      vel = g[6].scan(/[A-Z]+/)[0]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\S+)(?:-|:)(\S+)(?:\s|:)(\D+)(?:\s|:)} #3
      g = [$1,$2,$3,$4]
      ancho = g[0]
      if ancho =~ %r{(\d+)(?:/|:)(\d+)}
        ancho_nuevo = [$1,$2]
        ancho = ancho_nuevo[1] 
      end
      serie = nil
      llanta = g[1]
      tube = g[2]
      vel = nil
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, true]
    elsif rueda =~ %r{(\S+)(?:\s|:)([A-Z])(?:\s|:)(\d+)(?:\s|:)([A-Z]+)(?:\s|:)(\d+)(?:/|:)(\d+[A-Z])} #5
      g = [$1,$2,$3,$4,$5,$6]
      ancho = g[0]
      serie = g[2]
      llanta = nil
      tube = g[3]
      vel = g[5].scan(/[A-Z]+/)[0]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\d+)(?:/|:)(\d+)(?:\s|:)(\D)(?:\s|:)(\d+)(?:\s|:)(\D+)(?:\s|:)([A-Z]+)} #6
      g = [$1,$2,$3,$4,$5,$6]
      ancho = g[0]
      serie = g[1]
      llanta = g[3]
      tube = g[4]
      vel = g[5]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\d+)(?:\s|:)(\D)(?:\s|:)(\d+)(?:\s|:)(\D+)(?:\s|:)(\d+[A-Z])} #7
      g = [$1,$2,$3,$4,$5]
      ancho = g[0]
      serie = nil
      llanta = g[2]
      tube = g[3]
      vel = g[4].scan(/[A-Z]+/)[0]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\d+)(?:\s|:)(\D)(?:\s|:)(\d+[A-Z])(?:\s|:)(\D+)(?:\s|:)(\d+)(?:/|:)(\d+[A-Z])} #8
      g = [$1,$2,$3,$4,$5,$6]
      ancho = g[0]
      serie = nil
      llanta = g[2]
      tube = g[3]
      vel = g[5].scan(/[A-Z]+/)[0]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\d+)(?:/|:)(\d+)(?:\s|:)(\D)(?:\s|:)(\S+)(?:\s|:)(\D+)(?:\s|:)(\d+)(?:/|:)(\d+[A-Z])} #9
      g = [$1,$2,$3,$4,$5,$6,$7]
      ancho = g[0]
      serie = g[1]
      llanta = g[3]<commit_msg>mejoras en speed_codes<commit_after># encoding: UTF-8
require 'csv'

class ImportTiresFromGane
  
  def initialize()
    @agent = Mechanize.new
    @final = "#{Rails.root}/vendor/products/listado-neumaticos.csv"
    @total = @no_leidos = []
    @taxons = Spree::Taxon.where(:parent_id => 2).map {|x| x.name}
    @tubes = %w(TL TT RU)
    @widths = Spree::TireWidth.all.map {|x| x.name}
    @series = Spree::TireSerial.all.map {|x| x.name}
    @llantas = Spree::TireInnertube.all.map {|x| x.name}
    @vel = Spree::TireSpeedCode.all.map {|x| x.name}
    @marcas = CSV.read("#{Rails.root}/db/datas/rodaben-marcas.csv").map {|x| x[0]}
  end
  
  def run
    #if login
      #read_from_gane
      #export_to_csv
      load_from_csv
    #end
  end
  
  def read_from_gane
    str = "http://galaicoasturianadeneumaticos.distritok.com/sqlcommerce/disenos/plantilla1/seccion/Catalogo.jsp?idIdioma=&idTienda=50&cPath=61"
    sch = ".//table[@class='result']//tr//td[@class='result_right']//a[@title='Siguiente ']"
    links = []
    until str.empty?
      page = @agent.get(str)
      page.search(".//table[@class='tableBox_output']//tr").each do |d|
        t = d.search("td[@width='900']//a").text
        r = d.search("td//span[@class='linCat']").map {|x| x.text}
        unless r.empty?
          if r[1] == "Consultar"
              p = k = pf = s = 0
          else
            s = r[0]
            p = r[1].to_s.delete("€").lstrip
            k = r[2].to_s.delete("%").lstrip
            pf = r[3].to_s.delete("€").lstrip
            puts "Stock es #{p}. PVP final es #{pf}"
          end
          @total << [t, s, p, k, pf]
        end
      end
      links.clear
      page.search(sch).each do |link|
        links << link[:href]
      end
      links = links.uniq
      str = links[0].to_s
      puts str
    end
  end
  
  def export_to_csv
    CSV.open(@final, "wb") do |row|
      @total.each do |element|
        #[nombre, stock, precio, descuento, precio final]
        row << element
      end
    end
  end
  
  def load_from_csv
    # [ancho, serie, llanta, vel, tube, marca, gr]
    result = []
    i = j =0
    hoy = Date.today
    productos = Spree::Product.find_by_sql("Select name from spree_products;").map {|x| x.name}.flatten
    CSV.foreach(@final) do |row|
      unless row[0].blank?
        if productos.include?(row[0]) # producto existe
          articulo = Spree::Product.find_by_name(row[0])
          articulo.update_attributes(
            :count_on_hand => set_stock(row[1]),
            :cost_price => row[4],
            :price => row[4] * 1.05 #falta de poner el precio de venta segun cliente
          )
          # actualizar los precios
        else
          result = read_format(row[0])
          i += 1
          # crear uno nuevo
          product = Spree::Product.new
          product.name = row[0]
          product.permalink = row[0].downcase.gsub(/\s+/, '-').gsub(/[^a-zA-Z0-9_]+/, '-')
          product.sku = hoy.year.to_s + hoy.month.to_s + hoy.day.to_s + "-" + i.to_s
          product.available_on = hoy - 1.day
          product.count_on_hand = set_stock(row[1])
          product.price = row[4] * 1.05 #falta de poner el precio de venta segun cliente
          product.cost_price = row[4]
          product.tire_width_id = set_width(result)
          product.tire_serial_id = set_serial(result)
          product.tire_innertube_id = set_innertube(result)
          product.tire_speed_code_id = set_speed_code(result)
          product.tire_rf = false
          product.tire_gr = result[6]
          product.tire_season = set_season(result)
          product.taxons << Spree::Taxon.find(set_catalog) #cargar categoria
          product.taxons << Spree::Taxon.find(set_brand(result)) #cargar marca
          if product.save!
            puts "Creado articulo #{row[0]}"
            j += 1
          end
        end
      end
    end
  end
  
  private
  
  def login
    login = '929'
    password = '<PASSWORD>'
    direccion = 'http://galaicoasturianadeneumaticos.distritok.com/'
    page = @agent.get(direccion)
    gane_form = page.form('login')
    gane_form.login = login
    gane_form.password = password
    gane_form.submit
  end
  
  def read_format(rueda)
    rueda = rueda.to_s
    if rueda =~ %r{(\d+)(?:/|:)(\d+)(?:\s|:)(\D)(?:\s|:)(\d+)(?:\s|:)(\D+)(?:\s|:)(\d+[A-Z])} #1
      g = [$1,$2,$3,$4,$5,$6]
      ancho = g[0]
      serie = g[1]
      llanta = g[3]
      tube = read_tube(g[4])
      vel = g[5].scan(/[A-Z]+/)[0]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\d+)(?:/|:)(\d+)(?:\s|:)(\D)(?:\s|:)(\d+[A-Z])(?:\s|:)(\D+)(?:\s|:)(\d+)(?:/|:)(\d+[A-Z])} #2
      g = [$1,$2,$3,$4,$5,$6,$7]
      ancho = g[0]
      serie = g[1]
      llanta = g[3]
      tube = g[4]
      vel = g[6].scan(/[A-Z]+/)[0]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\S+)(?:-|:)(\S+)(?:\s|:)(\D+)(?:\s|:)} #3
      g = [$1,$2,$3,$4]
      ancho = g[0]
      if ancho =~ %r{(\d+)(?:/|:)(\d+)}
        ancho_nuevo = [$1,$2]
        ancho = ancho_nuevo[1] 
      end
      serie = nil
      llanta = g[1]
      tube = g[2]
      vel = nil
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, true]
    elsif rueda =~ %r{(\S+)(?:\s|:)([A-Z])(?:\s|:)(\d+)(?:\s|:)([A-Z]+)(?:\s|:)(\d+)(?:/|:)(\d+[A-Z])} #5
      g = [$1,$2,$3,$4,$5,$6]
      ancho = g[0]
      serie = g[2]
      llanta = nil
      tube = g[3]
      vel = g[5].scan(/[A-Z]+/)[0]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\d+)(?:/|:)(\d+)(?:\s|:)(\D)(?:\s|:)(\d+)(?:\s|:)(\D+)(?:\s|:)([A-Z]+)} #6
      g = [$1,$2,$3,$4,$5,$6]
      ancho = g[0]
      serie = g[1]
      llanta = g[3]
      tube = g[4]
      vel = g[5]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\d+)(?:\s|:)(\D)(?:\s|:)(\d+)(?:\s|:)(\D+)(?:\s|:)(\d+[A-Z]+)} #7
      g = [$1,$2,$3,$4,$5]
      ancho = g[0]
      serie = nil
      llanta = g[2]
      tube = g[3]
      vel = g[4].scan(/[A-Z]+/)[0]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\d+)(?:\s|:)(\D)(?:\s|:)(\d+[A-Z])(?:\s|:)(\D+)(?:\s|:)(\d+)(?:/|:)(\d+[A-Z])} #8
      g = [$1,$2,$3,$4,$5,$6]
      ancho = g[0]
      serie = nil
      llanta = g[2]
      tube = g[3]
      vel = g[5].scan(/[A-Z]+/)[0]
      marca = read_taxon(rueda)
      [ancho, serie, llanta, vel, tube, marca, false]
    elsif rueda =~ %r{(\d+)(?:/|:)(\d+)(?:\s|:)(\D)(?:\s|:)(\S+)(?:\s|:)(\D+)(?:\s|:)(\d+)(?:/|:)(\d+[A-Z])} #9
      g = [$1,$2,$3,$4,$5,$6,$7]
      ancho = g[0]
      serie = g[1]
      llanta = g[3]
{'fraction_non_alphanumeric': 0.22306670602125148, 'fraction_numerical': 0.021103896103896104, 'mean_word_length': 2.177725674091442, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '750249', 'n_tokens_mistral': 5810, 'n_tokens_neox': 5739, 'n_words': 981}