Created
December 2, 2011 05:13
-
-
Save rafapolo/1421872 to your computer and use it in GitHub Desktop.
A partir de um link, navega aleatóriamente entre os outros links interessantes da Wikipédia.
This file contains hidden or 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
require 'open-uri' | |
def html(wiki) | |
open("http://en.wikipedia.org/wiki/#{wiki}", 'User-Agent' => 'bot').read | |
end | |
def links(html) | |
html.scan(/<a href=\"\/wiki\/.+\" title=\"(\w+)\">/)[0].uniq | |
end | |
def go(word) | |
puts "Hey! Let's get something about #{word}." | |
while word do | |
if ways = links(html(word)) | |
print "\nShould I go into " | |
ways.each {|way| print "#{way}, "} | |
puts "or maybe just relax?" # random | |
random = ways[rand(ways.size)] | |
puts "#{random} sound good, come on..." | |
word = random | |
else | |
puts "Opz! No more ways over here." | |
end | |
end | |
puts "..." | |
end | |
go("Ocupation") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment