Created
February 25, 2014 14:33
-
-
Save bradoyler/9209928 to your computer and use it in GitHub Desktop.
a terminal script (Ruby) to query Wikipedia
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
## Setup steps via terminal: | |
# 1. create a .rb file : $ touch askwiki.rb | |
# 2. open file: $ open askwiki.rb | |
# 3. copy/paste below in file and close/save | |
# 4. run it: $ ruby askwiki.rb | |
require 'open-uri' | |
require 'json' | |
language = 'en' | |
print 'What do you need to know? : ' | |
article = URI::encode gets.chomp | |
request_url = "http://#{language}.wikipedia.org/w/api.php?action=parse&page=#{article}&format=json&prop=text§ion=0" | |
open(request_url) do |file| | |
content = JSON.parse(file.read())['parse']['text'].first[1] | |
content_text = content.gsub(/<\/?[^>]+>/, '') | |
content_text = content_text.gsub(/[[:space:]]+/, ' ') | |
content_text = content_text.gsub(/&#[0-9]+;/,'') | |
content_text = content_text.gsub(/\[[0-9]+\]/,'') | |
puts content_text | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment