query wiki and google from bash

Forum rules
Share your brain ;)
machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

query wiki and google from bash

Unread post by machinebacon » Tue Jun 10, 2014 3:48 pm

Don't remember where I got this snippet from, but it does what it says:

Code: Select all


#functions
function encode() { echo -n $@ | perl -pe's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg'; }
function goo() { links2 http://www.google.com/search?q="`encode $@`" ;}
function wikide() { links2 http://de.wikipedia.org/w/index.php?search="`encode $@`" ;}
function wikien() { links2 http://en.wikipedia.org/w/index.php?search="`encode $@`" ;}
or in easy

Code: Select all

wget -O- http://linuxbbq.org/wikifunction >> ~/.bashrc && bash 
query google:
goo <search terms>
query de.wikipedia.org
wikide <search term>
query en.wikipedia.org
wikien <search term>
..gnutella..

User avatar
z3bra
Window Manager
Posts: 53
Joined: Fri Jun 27, 2014 8:34 am
Location: France
Contact:

Re: query wiki and google from bash

Unread post by z3bra » Fri Jun 27, 2014 2:22 pm

How about querying the wikipedia DNS instead ? (courtesy of commandlinefu.com)

Code: Select all

wiki () {
    dig +short txt "$*".wp.dg.cx
}
Short example :

Code: Select all

─── wiki linux
"Linux ( or ) is a Unix-like computer operating system assembled under the
model of free and open source software development and distribution. The
defining component of Linux is the Linux kernel, an operating system kernel
first released 5 October 1991 by" " Linus Torvalds.
http://en.wikipedia.org/wiki/Linux"
Also, I wrote a small tool using curl and sed to fetch the top results of a
youtube search:

Code: Select all

#!/bin/sh
#
# z3bra - (c) wtfpl 2014
# perform a search on youtube and return the best result (title + link)

usage() {
    echo "`basename $0` [-htu] [-n <num>] <query>"

    test -z "$1" && return

    cat <<EOF
        -h : display this help
        -t : output titles only (default 'title - uri')
        -u : output uris only
        -n : print only <num> results (default: 3)
EOF
}

num=3
regex='^.*title="\([^"]*\)".*href="\(/watch[^"]*\)".*$'
output='\1 - http://youtube.com\2'

while getopts "hn:tu" OPT; do
    case  $OPT in
        n) num=$OPTARG;;
        t) output='\1';;
        u) output='http://youtube.com\2';;
        h) usage long; exit 0;;
        *) usage; exit 1;;
    esac
done

shift $((OPTIND - 1))

query=$(echo $@ | tr ' ' '+')
url="http://www.youtube.com/results?search_query=${query}"

curl -s "$url" | sed -n "s,$regex,$output,p" | sed ${num}q
Again, a quick example:

Code: Select all

# search "linuxBBQ roast" on youtube, and display the titles and uri of the 3
# top results (default)
─── ys linuxBBQ roast
LinuxBBQ Roast Your Own Distribution - http://youtube.com/watch?v=6TWPY2bO9Jc
LinuxBBQ Kielbasa (i686) RC1 - http://youtube.com/watch?v=U9Tivz9TCt8
LinuxBBQ Cream : Live Boot and Review! - http://youtube.com/watch?v=0GNUsZxuqIY

─── # display only 1 link, and display only the uri
─── ys -n1 -u linuxBBQ roast
http://youtube.com/watch?v=6TWPY2bO9Jc
BANGARANG, MOTHERFUCKER.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: query wiki and google from bash

Unread post by machinebacon » Fri Jun 27, 2014 2:42 pm

To answer the first question:
Because these are not the same things :) What you do with dig is to query the short description, which is not the same like opening the wiki page. I want to open the wikipedia page for a given search term, and not the short definition of it.

Thanks.
..gnutella..

User avatar
z3bra
Window Manager
Posts: 53
Joined: Fri Jun 27, 2014 8:34 am
Location: France
Contact:

Re: query wiki and google from bash

Unread post by z3bra » Fri Jun 27, 2014 2:47 pm

Oh, I see. good point then !
BANGARANG, MOTHERFUCKER.

Post Reply