-
-
Save hugovk/4478474 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
''' | |
I frequently look up word meanings online. | |
It's kind of a hassle to fire up the browser, and search online after a while. | |
It's a lot easier for me to do it from the command line, when my terminal is one keyboard shortcut away. | |
I wrote this script using the wordnik api that does just that. | |
I suggest you put this somewhere in your PATH, make it executable. | |
Make sure you get a Wornik API key at http://developer.wordnik.com/ | |
Copy and paste your API key to the first and only line of $HOME/.wordnik (linux, mac) or %USERPROFILE%\.wordnik (windows) | |
Example output: | |
$ define myopic --limit=3 | |
======================================================================== | |
myopic (got 3 results, asked for 3) | |
======================================================================== | |
1: nearsighted; unable to see distant objects unaided | |
2: shortsighted; improvident | |
3: narrow minded | |
''' | |
import os | |
import sys | |
import argparse | |
import urllib2 | |
from wordnik.api.APIClient import APIClient | |
from wordnik.api.WordAPI import WordAPI | |
import wordnik.model | |
HOME = os.environ[['USERPROFILE', 'HOME'][sys.platform in ['linux2', 'darwin']]] | |
with open(os.path.join(HOME, '.wordnik')) as fh: | |
key = fh.read().strip() | |
client = APIClient(key, 'http://api.wordnik.com/v4') | |
wordAPI = WordAPI(client) | |
def die(msg): | |
print '=' * 72 | |
print 'ERROR'.center(72) | |
print '=' * 72 | |
print msg.center(72) | |
def getDefinition(word, limit): | |
input = wordnik.model.WordDefinitionsInput.WordDefinitionsInput() | |
input.word = word | |
input.limit = limit | |
try: | |
definitions = wordAPI.getDefinitions(input) | |
except urllib2.HTTPError, tb: | |
die(str(tb)) | |
return | |
print '=' * 72 | |
if definitions is None: | |
print "No definitions found" | |
return | |
print '%s (got %d results, asked for %d)'.center(72) % (word, | |
len(definitions), limit) | |
print '=' * 72 | |
for index, word in enumerate(definitions): | |
print '%d: %s' % (index + 1, word.text) | |
def shell(limit): | |
while True: | |
word = raw_input('Word: ') | |
os.system(['clear', 'cls'][sys.platform == 'win32']) | |
getDefinition(word, limit) | |
parser = argparse.ArgumentParser(description='Lookup word definitions' | |
' quickly from the command line using the Wordnik API! Place your API' | |
' key in the first line of "$HOME/.wordnik" or "%USERPROFILE$\.wordnik"' | |
' This script works on Linux, Mac, and Windows.', epilog='Developed by Luis Naranjo' | |
' <luisnaranjo733@gmail.com>') | |
parser.add_argument('word', nargs='?') | |
parser.add_argument('-l', '--limit', help='Enter a limit of search results' | |
' to display at a time.', type=int, default=3) | |
parser.add_argument('-s', '--shell', help='Enter a shell for looking up' | |
' many words at a time!', action='store_true') | |
if __name__ == '__main__': | |
args = parser.parse_args() | |
if args.word: | |
getDefinition(args.word, args.limit) | |
if args.shell: | |
print 'Use regular keyboard interrupt to exit (usually CTRL-C)' | |
try: | |
shell(args.limit) | |
except KeyboardInterrupt: | |
sys.exit(0) | |
if not args.word and not args.shell: | |
print parser.parse_args(['-h']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment