Created
January 29, 2015 23:05
-
-
Save joewils/646f26794540bc3f2b84 to your computer and use it in GitHub Desktop.
Beyond The White Board to Jekyll
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
# ruby btwb-to-jekyll.rb | |
# (execute from Jekyll blog root) | |
puts 'BTWB To Jekyll' | |
require 'yaml' | |
require 'httparty' | |
require 'nokogiri' | |
# Beyond The White Board Credentials | |
# ENV['BTWB_ID'] | |
# ENV['BTWB_USERNAME'] | |
# ENV['BTWB_PASSWORD'] | |
# Today's date | |
date = Time.now.strftime("%Y-%m-%d") | |
# Get CSV Export from BTWB | |
path = 'http://beyondthewhiteboard.com/members/' | |
path << ENV['BTWB_ID'] | |
path << '/workout_sessions.csv' | |
wods = HTTParty.get(path, basic_auth: { username: ENV['BTWB_USERNAME'], password: ENV['BTWB_PASSWORD'] }) | |
# Archive CSV | |
File.open('_csv/wods.csv', 'w+') do |file| | |
file.puts wods | |
end | |
# Parse WOD Data | |
data = {} | |
header = wods.slice!(0) | |
wods.parsed_response.each do |wod| | |
wod_data = {} | |
wod.each_with_index do |value, index| | |
wod_data[header[index]] = value.gsub("\r\n","\n") if value | |
end | |
# Organize WODs by date | |
if data[wod[0]] | |
data[wod[0]].push(wod_data) | |
else | |
data[wod[0]] = [] | |
data[wod[0]].push(wod_data) | |
end | |
end | |
# Archive as YAML | |
File.open('_data/wods.yml', 'w+') do |file| | |
file.puts data.to_yaml | |
end | |
# Load Jekyll Post History | |
history_path = '_data/wod-history.yml' | |
history = YAML.load_file(history_path) | |
# Format Data | |
wods = {} | |
data.sort.each do |date,wod| | |
workouts = [] | |
results = [] | |
tags = ['wod','btwb'] | |
notes = [] | |
wod.each do |program| | |
program_tags = program['Workout'].downcase.gsub(':', ',').gsub(' and', ',').split(',').collect{|x| x.strip} | |
tags.concat(program_tags) | |
workouts.push(program['Workout']) | |
results.push(program['Formatted Result']) | |
notes.push(program['Notes']) if program['Notes'].to_s != '' | |
end | |
wods[date] = {'workouts'=>workouts,'results'=>results, 'tags'=>tags, 'notes'=>notes} | |
end | |
# Build Jekyll Post | |
wods.each_with_index do |wod,i| | |
filename = '_posts/' + wod[0] + '-wod.md' | |
wod_count = (i+101) | |
next if history[wod_count] | |
puts filename | |
post = { | |
'layout' => 'btwb', | |
'title' => 'WOD #' + wod_count.to_s, | |
'workouts' => wod[1]['workouts'], | |
'results' => wod[1]['results'], | |
'tags' => wod[1]['tags'], | |
'notes' => wod[1]['notes'] | |
} | |
history[wod_count] = filename | |
# Save Post | |
File.open(filename, 'w+') do |file| | |
file.puts post.to_yaml | |
file.puts "---\n" | |
end | |
end | |
# Save History | |
File.open(history_path, 'w+') do |file| | |
file.puts history.to_yaml | |
file.puts "---\n" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment