Ask wikipedia from command line using Ruby
this could be a complementary script for your rails application, if you have a rails blog and want to give the user ability to search in wikipedia within the same window, or if you want to crawl wikipedia and extract the text only, the following is a simple straightforward solution.
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
as this protip got so many views and comments more than any other protip i wrote, i decided to make it as gem
you can find it here : https://github.com/blazeeboy/askwiki
Written by Emad Elsaid
Related protips
5 Responses
Cool
Access wikipedia via DNS query:
user@server:~$ dig +short txt tron.wp.dg.cx
"Tron is a 1982 American science fiction film written and directed by Steven Lisberger and released by Buena Vista Distribution. It stars Jeff Bridges as the protagonist, Bruce Boxleitner in a dual role, and David Warner, who plays all three main antagonis" "ts. A computer programmer is transported inside the software world of a mainframe computer, where he interacts with various... http://en.wikipedia.org/wiki/Tron"
Source: http://www.commandlinefu.com/commands/view/2829/query-wikipedia-via-console-over-dns
Woooooow this amazing
This is awesome. Great post.
Really useful Thanks