Skip to content

Instantly share code, notes, and snippets.

@emad-elsaid
Forked from mahemoff/ask-wikipedia.rb
Created February 25, 2014 14:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emad-elsaid/9210243 to your computer and use it in GitHub Desktop.
Save emad-elsaid/9210243 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'open-uri'
require 'json'
language = 'en'
article = ARGV[0] ||
begin
print 'What do you need to know? : '
URI::encode gets.chomp
end
request_url = "http://#{language}.wikipedia.org/w/api.php?action=parse&page=#{article}&format=json&prop=text&section=0&redirects"
open(request_url) do |file|
puts JSON.parse(file.read())['parse']['text'].first[1]
.gsub(/<\/?[^>]+>/, '') # strip tags
.gsub(/[[:space:]]+/, ' ') # strip whitespace
.gsub(/&#[0-9]+;/,'') # strip encoded
.gsub(/\[[0-9]+\]/,'') # strip referencing
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment