Created
October 30, 2008 20:12
-
-
Save namelessjon/21127 to your computer and use it in GitHub Desktop.
xkcd application
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 "rubygems" | |
require "sinatra" | |
require "hpricot" | |
require "open-uri" | |
class XKCD | |
attr_accessor :all | |
class ComicError | |
attr_accessor :all | |
def initialize(num) | |
self.id = num.to_i | |
end | |
def to_result(cx, *args) | |
@reason = "404 - Comic Not Found - ##{self.id}" | |
end | |
end | |
class QuoteError | |
attr_accessor :all | |
def initialize(num) | |
self.id = num.to_i | |
end | |
def to_result(cx, *args) | |
@reason = "404 - Quote Not Found - ##{self.id}" | |
end | |
end | |
def self.comic(n=nil) | |
comic = {} | |
comic[:page] = "http://www.xkcd.com/#{n}" | |
xkcd = open(comic[:page], "User-Agent" => "Lenny's Site-Bot. Please contact me on irc://irc.foonetic.net/uk if there are problems") { |f| Hpricot(f) } | |
if xkcd.at("h1").to_s.match(/404 - Not Found/) | |
throw :halt, XKCD::ComicError.new(n) | |
else | |
comic[:title] = xkcd.at("h1").inner_html | |
comic[:url] = xkcd.at("img[@alt='#{comic[:title]}']")['src'] | |
comic[:text] = xkcd.at("img[@alt='#{comic[:title]}']")['title'] | |
comic[:permalink] = xkcd.at(".menuCont ~ h3").inner_html.gsub(/Permanent link to this comic: /, '') | |
comic[:id] = comic[:permalink].gsub(/http:\/\/www\.xkcd\.com\/[.*]\//){|f| f } | |
return comic | |
end | |
end | |
def self.max_comic | |
open("http://www.xkcd.com/") { |f| Hpricot(f) }.at(".menuCont ~ h3").inner_html.gsub(/Permanent link to this comic: http:\/\/xkcd.com\//, '').chop | |
end | |
def self.rand_comic | |
1 + rand(self.max_comic) | |
end | |
def self.quote(n) | |
quote = {} | |
quote[:id] = n.to_i | |
quote[:page] = "http://www.xkcdb.com/?#{quote[:id]}" | |
xkcdb = open(quote[:page], "User-Agent" => "Lenny's Site-Bot. Please contact me on irc://irc.foonetic.net/uk if there are problems") { |f| Hpricot(f)} | |
if xkcdb.at("#content").to_s.match(/No quotes were found with that ID or containing that string\./) | |
throw :halt, XKCD::QuoteError.new(n) | |
else | |
quote[:votes] = xkcdb.at("#content .quoteblock").to_s.gsub(/^[.*]\(/, '').gsub(/\)[.*]$/, '').to_i | |
quote[:text] = xkcdb.at("#content .quoteblock .quote").to_s | |
return quote | |
end | |
end | |
def self.max_quote | |
open("http://www.xkcdb.com/") { |f| Hpricot(f) }.at("#content .quoteblock .quotehead .idlink")['name'].to_i | |
end | |
def self.rand_quote | |
1 + rand(self.max_quote) | |
end | |
def self.num_string(n) | |
if n.nil? | |
"" | |
else | |
"#{n.to_i}/" | |
end | |
end | |
end | |
get '/' do | |
@comic = XKCD.comic | |
haml :index | |
end | |
get '/c/:comic' do | |
@title = "XKCD-Connection: Comic ##{params[:comic]}" | |
@comic = XKCD.comic(params[:comic]) | |
haml :comic | |
end | |
get '/q/:quote' do | |
@title = "XKCD-Connection: Quote ##{params[:quote]}" | |
@quote = XKCD.quote(params[:quote].to_i) | |
haml :quote | |
end | |
get '/rand/c' do | |
redirect "/c/#{XKCD.rand_comic}" | |
end | |
get '/rand/q' do | |
redirect "/q/#{XKCD.rand_quote}" | |
end | |
get '/style.css' do | |
content_type 'text/css', :charset => 'utf-8' | |
sass :css | |
end | |
error OpenURI::HTTPError do | |
status 404 | |
@reason = "HTTP Error" | |
haml :not_found | |
end | |
error XKCD::ComicError do | |
redirect "/rand/c" | |
end | |
error XKCD::QuoteError do | |
redirect "/rand/q" | |
end | |
use_in_file_templates! | |
__END__ | |
@@ layout | |
!!! strict | |
%html | |
%head | |
%title= @title || "XKCD-Connection" | |
%link{:rel => 'stylesheet', :href => '/style.css', :type => 'text/css'} | |
%body | |
#header | |
%h2= @title || "XKCD-Connection" | |
.nav | |
%a.home{:href=>"/"} Home | |
%a.comic{:href => "/rand/c" } Random Comic | |
%a.quote{:href => "/rand/q"} Random Quote | |
!= yield | |
#footer | |
.nav | |
%a.home{:href=>"/"} Home | |
%a.comic{:href => "/rand/c" } Random Comic | |
%a.quote{:href => "/rand/q"} Random Quote | |
%br | |
Site Copyright Samuel Elliott, 2008 | |
@@ index | |
#num= @comic[:id] | |
#title | |
%a{:href => @comic[:permalink]}= @comic[:title] | |
#image | |
%a{:href => @comic[:permalink]} | |
%img{:src => @comic[:url], :title => @comic[:title]}/ | |
#alt= @comic[:title] | |
@@ comic | |
#num= @comic[:id] | |
#title | |
%a{:href => @comic[:permalink]}= @comic[:title] | |
#image | |
%a{:href => @comic[:permalink]} | |
%img{:src => @comic[:url], :title => @comic[:text]} | |
#alt= @comic[:text] | |
@@ quote | |
#num | |
%a{:href => @quote[:page]}= @quote[:id] | |
#votes | |
%a{:href => @quote[:page]}= @quote[:votes] | |
%pre#text!= @quote[:text] | |
@@ not_found | |
%h1= @reason | |
@@ css |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment