Skip to content

Instantly share code, notes, and snippets.

@siers
Created February 28, 2013 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siers/5056104 to your computer and use it in GitHub Desktop.
Save siers/5056104 to your computer and use it in GitHub Desktop.
Fetches and assembles timetable from school's website into a more usable form.
#!/usr/bin/env ruby
require 'rubygems'
require 'pry'
require 'nokogiri'
require 'mini_magick'
require 'digest'
W = 412
H = 600
def template(text)
<<-HTML
<!DOCTYPE html>
<html>
<head>
<title>saraksts</title>
<meta charset="utf-8" />
</head>
<style>
img {
float: left;
padding: 10px;
display: block;
border-left: 2px dotted #666;
width: #{ W / 3 }px;
height: #{ H / 3 }px;
}
img:first-child { border: none; }
img:hover { position: relative; width: #{ W + 20 }px; height: #{ H + 20 }px; }
</style>
<body>
#{ text }
</body>
</html>
HTML
end
def inform(text)
yield
ensure
puts text
end
def curl(link)
inform "<-- Downloading #{ link }" do
%x{curl 2>/dev/null #{ link }}
end
end
def parse(link)
inform "--> Parsing #{ link }" do
Nokogiri::HTML::Document.parse(curl link)
end
end
def get_table(opts)
filename, link = opts.first
image = nil
inform ":: Searching for table's image" do
image = parse(link).css('p img').first.attributes["src"].value
end
crop(image, filename)
filename
end
def crop(from, to)
inform ":: Cropping" do
image = MiniMagick::Image.open(from)
image.crop("#{ W }x#{ H }+0+425")
inform "--> Saving to #{ to }" do
image.write(to)
end
end
end
doc = parse('http://www.rskola.lv')
days = doc.search("[text()*='diena']").select { |n| n.children.first.text =~ /[A-Z][a-z]+diena/ }
pages = days.map { |n| [n.children.first.text, n.attributes["href"].value] }
html = ''
pages.each do |day, page|
filename = "#{ day }-#{ Digest::MD5.hexdigest(page) }.jpg"
get_table(filename => page) unless File.exists?(filename)
html << "<img src='#{ filename }' alt='#{ day }' />"
end
inform "--> Generating HTML" do
File::open('index.html', 'w').write(template(html))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment