uggf1a
1.838K
· February 2014 ·
12715527 10153407351782957 5557639479013711635 n

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&section=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

Sign in or sign up to add your response.

6 Responses

13015
F62a36ef7d9346b46f7214ba56034e77

Cool

over 1 year ago ·
13111
0fc5485611721e78b1583657212a046a

damn, that's nice, I'll have to make something like that for Emacs!

over 1 year ago ·
13132
C11ccd0c94b964360f0ddee7959afef7

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

over 1 year ago ·
13144
12715527 10153407351782957 5557639479013711635 n

Woooooow this amazing

over 1 year ago ·
13162
Il 340x270.622268562 pge6

This is awesome. Great post.

over 1 year ago ·
13178
2c9e9f5c3878135201601a10b903909d

Really useful Thanks

over 1 year ago ·