Skip to content

Instantly share code, notes, and snippets.

@abdul-shajin
Created December 18, 2019 17:48
Show Gist options
  • Save abdul-shajin/8f79f93484e7f60cb32abe9a5db3a7fe to your computer and use it in GitHub Desktop.
Save abdul-shajin/8f79f93484e7f60cb32abe9a5db3a7fe to your computer and use it in GitHub Desktop.
require 'roo'
require 'fileutils'
require 'csv'
require 'vcardio'
xlsx = Roo::Excelx.new('./cnt.xlsx')
nothing = []
@vcf_elements = [{key: :food, values: [], regex: /FOOD/i},
{key: :cold_store, values: [], regex: /COLD/i},
{key: :resturants, values: [], regex: /REST|KITCHEN|COFFEE|SANDWICH|SUBWAY|PASTRIES|PIZZA|SHAWARMA|GRILLS|MACDONALD|GRILL|FALAFEL|SANDWICHES|DAIRY|CHICKEN|BURGER/i},
{key: :sweets, values: [], regex: /SWEET/i},
{key: :markets, values: [], regex: /MARKET/i},
{key: :cafeterias, values: [], regex: /CAFE|LOUNGE/i},
{key: :trading, values: [], regex: /TRAD/i},
{key: :groceries, values: [], regex: /GROCERY|VEGETABLE|FRUITS/}]
xlsx.each_row_streaming do |row|
match = false
@vcf_elements.each do |vcf_elem|
if row[1].to_s =~ vcf_elem[:regex]
match = true
vcf_elem[:values] << row.to_a.map(&:to_s)
end
end
nothing << row.to_a.map(&:to_s) unless match
end
@vcf_elements << {key: :nothing, values: nothing}
grouped_file = File.open('grouped_file.txt', 'w')
FileUtils.mkdir_p './excel_files'
FileUtils.mkdir_p './vcards'
@vcf_elements.each do |vcf_elem|
vcf_file = File.open("./excel_files/#{vcf_elem[:key]}.csv",'w')
vcf_file.close
FileUtils.mkdir_p "./vcards/#{vcf_elem[:key]}"
CSV.open("./excel_files/#{vcf_elem[:key]}.csv", "w") do |csv|
vcf_elem[:values].each do |row|
csv << row
if (row[5].strip.length > 5) || (row[5] =~ /\// && row[5].size > 10)
vcard = VCardio::VCard.new('3.0') do
name = row[0]
address = row[3]
contact_name = row[4]
if row[5] =~ /\//
if row[5].split('/')[0].strip.length > 5
tel "+971#{row[5].split('/')[0].strip}", type: 'HOME'
end
if row[5].split('/')[1].strip.length > 5
tel "+971#{row[5].split('/')[1].strip}", type: 'WORK'
end
else
mobile_no = "+973#{row[5]}"
tel mobile_no
end
adr address
fn row[1]
org row[4]
end
time = Time.now.to_i
vcard.to_file("./vcards/#{vcf_elem[:key]}/#{row[4].to_s.gsub(/[^0-9A-Za-z]/, '')}-#{time}.vcf")
end
end
end
grouped_file.puts("-----#{vcf_elem[:key].upcase}-------")
vcf_elem[:values].each {|elem| grouped_file.puts(elem) }
grouped_file.puts("-----#{vcf_elem[:key].upcase} END-------")
end
grouped_file.close
onnilum_pedatha = File.open('onnilum_pedatha.txt', 'w')
nothing.each {|noth| onnilum_pedatha.puts(noth.join(',')) }
onnilum_pedatha.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment