Wednesday, May 27, 2009

Better Session Script

Ryan McCuaig sent me a kick-ass re-write of my WWDC session script. This one outputs the information with much, much better formatting.

Here's the very fine script:

#!/usr/bin/env ruby
#
# This program will download the latest session JSON data
# from the WWDC website and will parse it into a plain
# HTML table so it can be viewed.
#
# Requires one of the following two gems to be installed:
#
# gem install json
# gem install json_pure
#
# Also requires installation of Haml/Sass:
#
# gem install haml
#
# This script was written by Ryan McCuaig, based on a script by Jeff LaMarche

require 'net/http'
require 'open-uri'
require 'rubygems'
require 'json'
require 'haml'
require 'sass'

JSON_FEED = 'http://developer.apple.com/wwdc/data/sessions.json'

class Session
attr_accessor :title,:focus,:start,:description,:type,:identifier, :room
def initialize(array)
@title = array["title"]
@focus = array["focus"]
@type = array["type"]
@start = Time.parse(array["time"][0]["lower"])
@stop = Time.parse(array["time"][0]["upper"])
@description = array["description"]
@identifier = array["id"]
@room = array["room"]
end
def when
start = @start.strftime("%l:%M %p")
stop = @stop.strftime("%l:%M %p")
[start,stop].map{|x| x.strip}.join("-")
end
end

json_data = open(JSON_FEED).read
parsed_data = JSON.parse(json_data)["SessionsData"]

sessions = []
parsed_data.each do |array|
sessions << Session.new(array)
end
sessions.sort! {|x,y| x.start <=> y.start}

class Array
# group_by doesn't exist before Ruby 1.8.7, drat
def group_by
hash = {}
each do |element|
key = yield(element)
if hash.has_key?(key)
hash[key] << element
else
hash[key] = [element]
end
end
hash
end unless [].respond_to?(:group_by)
end

sessions_grouped_by_day = sessions.group_by {|x| x.start.strftime("%A")}

html_template = <<HTML
!!!
%html
%head
%title Session Times
%style= css
%body
%div#content
- %w{Monday Tuesday Wednesday Thursday Friday}.each do |day|
%h1= day
- days[day].each do |session|
%h2
%div#when= session.when
%div
= session.title
%span.type= session.type
%span.focus= session.focus.join(", ")
%span.identifier= session.identifier
%span.room= session.room
%p= session.description
HTML


css_template = <<CSS
!indent = 150px
#content
:width 800px
:margin-left auto
:margin-right auto
h1,
h2,
p
:font-family Helvetica Neue, Helvetica
h1
:font-size 21px
h2
:font-size 16px
:margin-left 0px
div#when
:position relative
:bottom -3px
:width = !indent
:float left
:font
:weight normal
:size 13px
span
:font
:weight normal
:size 13px
:padding 0 0.5em 0 0.5em
&.type
:color red
&.focus
:color green
&.identifier
:color blue
&.room
:font
:weight bold
:size 14px
:color grey

p
:font-size 13px
:line-height 21px
:margin-left = !indent
CSS


html_engine = Haml::Engine.new(html_template)
css_engine = Sass::Engine.new(css_template)

File.open("/tmp/wwdc_sessions.html",'w') do |file|
file.write html_engine.render(Object.new,
:days=>sessions_grouped_by_day,:css=>css_engine.render)
end
`open /tmp/wwdc_sessions.html`

No comments:

Post a Comment