Last active
September 24, 2017 06:10
-
-
Save jayliu50/5214833e5958ad693e6e4f49acda9833 to your computer and use it in GitHub Desktop.
Given goodreads CSV and Selenium HTML Test case template, batch create test cases that can fill out forms with book's data. Usage: `convert_goodreads_csv.rb [your CSV] [your HTML template]`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'csv' | |
require 'fileutils' | |
class String | |
# colorization | |
def colorize(color_code) | |
"\e[#{color_code}m#{self}\e[0m" | |
end | |
def red | |
colorize(31) | |
end | |
def green | |
colorize(32) | |
end | |
def yellow | |
colorize(33) | |
end | |
def blue | |
colorize(34) | |
end | |
def pink | |
colorize(35) | |
end | |
def light_blue | |
colorize(36) | |
end | |
end | |
if (ARGV.length != 2) | |
puts "Usage: convert_goodreads_csv.rb [your CSV] [your HTML template]".red | |
exit | |
end | |
source = ARGV[0] | |
template = File.open(ARGV[1], 'rb') {|file| file.read} | |
dir = 'export' | |
FileUtils.mkdir_p(dir) | |
count_success = 0 | |
count_fail = 0 | |
def process_isbn(isbn) | |
return isbn.gsub(/[^\d]/, "") | |
end | |
data = CSV.foreach(source, headers:true) do |row| | |
begin | |
filename = "#{row['Title'].downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')}-#{File.basename(ARGV[1], ".*")}" | |
content = template.gsub(/\{\{\{Title\}\}\}/, row['Title']) | |
content = content.gsub(/\{\{\{Author l-f\}\}\}/, row['Author l-f']) | |
content = content.gsub(/\{\{\{ISBN13\}\}\}/, process_isbn(row['ISBN13']) || process_isbn(row['ISBN'])) unless row['ISBN13'] == nil and row['ISBN'] == nil | |
content = content.gsub(/\{\{\{Publisher\}\}\}/, row['Publisher']) unless row['Publisher'] == nil | |
content = content.gsub(/\{\{\{Year Published\}\}\}/, row['Year Published']) unless row['Year Published'] == nil | |
content = content.gsub(/\{\{\{Book Id\}\}\}/, row['Book Id']) | |
if (row['Bookshelves'] == nil) | |
path = File.join(dir, "#{filename}.html") | |
File.write(path, content) | |
puts "Written ".green + filename.yellow + " (no shelf)".blue | |
else | |
row['Bookshelves'].split(',').each do |shelf| | |
shelf = shelf.strip | |
FileUtils.mkdir_p(File.join(dir, shelf)) | |
path = File.join(dir, shelf, "#{filename}.html") | |
File.write(path, content) | |
puts "Written ".green + filename.yellow + " to shelf #{shelf.blue}" | |
end | |
end | |
rescue TypeError, NoMethodError | |
puts "Error: #{row.to_a}".red | |
count_fail += 1 | |
else | |
count_success += 1 | |
end | |
end | |
puts "Done: " + "#{count_success} succeeded. ".green + "#{count_fail} failed.".red | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment