Skip to content

Instantly share code, notes, and snippets.

@Cosmo
Last active August 29, 2015 14:17
Show Gist options
  • Save Cosmo/0bfeb0b1046c593eaa72 to your computer and use it in GitHub Desktop.
Save Cosmo/0bfeb0b1046c593eaa72 to your computer and use it in GitHub Desktop.
hackevents.co parser and calender.
# Create a file, call it "Gemfile" and write these 3 lines, then save it:
source "https://rubygems.org"
gem 'sinatra'
gem 'nokogiri'
# Run
bundle install
# from your terminal
# Create a file called "app.rb" and paste the follow lines (and run it with "ruby app.rb":
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'date'
require 'sinatra'
class Hackathon
attr_accessor :title
attr_accessor :date
attr_accessor :location
def to_s
[self.date, self.location, self.title].join(" - ")
end
def self.hackevents
doc = Nokogiri::HTML(open('http://www.hackevents.co'))
lines = doc.css('.accordion-title').map { |line| line.content }.uniq
regex = /(\d{2}\.\d{2}.\d{4})\s\-\s(\w*)[\-\s]{,4}(.*)/
hackathons = []
lines.each do |line|
line_parts = line.match(regex).to_a
if line_parts.size >= 4
hackathon = Hackathon.new
hackathon.date = Date.parse(line_parts[1])
hackathon.location = line_parts[2]
hackathon.title = line_parts[3]
hackathons << hackathon
end
end
hackathons.sort_by(&:date)
end
end
class Date
def beginning_of_week
days_to_monday = self.wday!=0 ? self.wday-1 : 6
(self - days_to_monday)
end
end
class Calendar
def self.show(data)
start_date = data.first.date.beginning_of_week
end_date = data.last.date.beginning_of_week
today = Time.now.to_date
grouped_data = data.group_by(&:date)
months = %w(Jan Feb Mär Apr Mai Jun Jul Aug Sep Okt Nov Dez)
output = ""
output << "<style>"
output << "body { font-family: 'Helvetica Neue'; font-size: 12px; }"
output << "table { width: 100%; border: 1px solid #EEEEEE; border-collapse: collapse; }"
output << "td { height: 100px; width: 14%; vertical-align: top; }"
output << "td.today { background-color: #F1F1F1; }"
output << "tr, td, th { border: 1px solid #EEEEEE; }"
output << "td, th { padding: 4px; }"
output << "p { margin: 0; padding: 0; }"
output << "p.day { }"
output << "p.event { background-color: orange; color: #FFFFFF; font-size: 12px; margin: 0 0 2px 0; }"
output << "</style>"
output << "<table>"
output << "<tr><th>Mo</th><th>Di</th><th>Mi</th><th>Do</th><th>Fr</th><th>Sa</th><th>So</th></tr>"
start_date.upto(end_date) do |date|
if date.wday == 1
output << "<tr>"
end
if date == today
output << "<td class='today'>"
else
output << "<td>"
end
output << "<p class='day'>"
output << "#{date.day}"
if date.day == 1
output << " – <strong>#{months[date.month]}</strong>"
end
output << "</p>"
if grouped_data[date]
grouped_data[date].each do |event|
output << "<p class='event'>#{event.title} - #{event.location}</p>"
end
end
output << "</td>"
if date.wday == 0
output << "</tr>"
end
end
output << "</table>"
return output
end
end
get '/hackevents' do
Calendar.show(Hackathon.hackevents)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment