Skip to content

Commit

Permalink
adding support for python3 and python2, added functions for easier ca…
Browse files Browse the repository at this point in the history
…ching
  • Loading branch information
adrianc-a committed Feb 10, 2014
1 parent 71299b8 commit 593759f
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 28 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ Baseball is a bat-and-ball game played between two teams of nine players who tak
```

# installation
__Note:__ Python 3.3 or higher is required
## via the install script
If you're on linux/os x and want a quick install simply run the the install script

__For python2__:
`curl https://raw2.github.com/adrianc-a/whatis/master/install.sh | bash`

__For python3__:
`curl https://raw2.github.com/adrianc-a/whatis/master/python3/install.sh | bash`

## manually
If you want to do this manually the installation is still pretty simple

1. download the `whatis.py` however you want
1. download the `whatis.py` however you want (and for whatever version of python
you want

2. change the name to `whatis` and run `chmod +x whatis` to make it executable

Expand Down
22 changes: 22 additions & 0 deletions python3/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
if [ ! -d $HOME/bin ]; then
mkdir $HOME/bin
fi
curl https://raw.github.com/adrianc-a/whatis/master/python3/whatis.py > $HOME/bin/whatis
chmod +x $HOME/bin/whatis

if [[ ! $PATH == *$HOME/bin* ]]; then
add_to_path="export PATH=$HOME/bin:$PATH"
if [ -d $HOME/.bashrc ]; then
cp $HOME/.bashrc $HOME/.saved_bash
echo $add_to_path >> $HOME/.bashrc
elif [ -d $HOME/.bash_profile ]; then
cp $HOME/.bash_profile $HOME/.saved_bash
echo $add_to_path >> $HOME/.bash_profile
fi
echo "$HOME/bin was added to your PATH variable successfully"
echo "If there was something that went wrong, your previous bash config"
echo "file was saved to $HOME/.saved_bash you may do with this as you wish"
else
echo "Installation successful"
fi
117 changes: 117 additions & 0 deletions python3/whatis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env python3

from sys import argv
import urllib.request, urllib.error, urllib.parse
from urllib.error import URLError
import json
import os

__author__ = 'Adrian Chmielewski-Anders'


def get(url):
return urllib.request.urlopen(url).read().decode('utf-8')


def cache_def(where, what):
f = open(where, 'w', encoding='utf-8')
f.write(what)
f.close()


def read_cache(location):
f = open(location, 'r', encoding='utf-8')
definition = f.read()
f.close()
return definition


def remove_tags(html):
no_tags = ''
intag = False
for i in html:
if i == '<':
intag = True
elif intag and i == '>':
intag = False
elif not intag:
no_tags += i
return no_tags


def did_you_mean(html):
index = html.find('<ul>')
if not index == -1:
return 'Did you mean: \n' + remove_tags(
html[index: html.find('</ul>')]) + '\n?'


def wiki(what):
cache = os.getenv('HOME') + '/.whatis/wiki/' + what
if os.path.exists(cache):
return read_cache(cache)
try:
html = get('http://en.wikipedia.org/wiki/' + what)
except URLError:
return urban(what)
index = html.find('<p>')

if not index == -1:
definition = remove_tags(html[index:html.find('</p>')])
if definition[len(what):] == ' may refer to:':
return did_you_mean(html)
cache_def(cache, definition)
return definition + '\nRead more at: http://en.wikipedia.org/' \
'wiki/' + what
return urban(what)


def urban(word, user=0):
user = int(user)
cache = os.getenv('HOME') + '/.whatis/urban/' + word + '_' + str(user)
if os.path.exists(cache):
return read_cache(cache)
try:
j = get(
'http://api.urbandictionary.com/v0/define?term='
+ word)
except URLError:
return 'Error, invalid URL'
definitions = json.loads(j)
if definitions['result_type'] == 'no_results':
return "There were no results found for " + word
if user >= len(definitions['list']):
user = len(definitions['list']) - 1
return_string = 'Definition:\n'
return_string += definitions['list'][user]['definition']
return_string += '\nExample:\n' + definitions['list'][user]['example']
cache_def(cache, return_string)
return return_string


def gen_args(arr, add_char='_'):
args = ''
for arg in arr:
if arg == arr[0]:
args += arg
else:
args += add_char + arg
return args


def main():
if not os.path.exists(os.getenv('HOME') + '/.whatis/wiki'):
os.makedirs(os.getenv('HOME') + '/.whatis/wiki')
if not os.path.exists(os.getenv('HOME') + '/.whatis/urban'):
os.makedirs(os.getenv('HOME') + '/.whatis/urban')
if argv[1] == '-u':
if argv[2] == '-n':
print(urban(gen_args(argv[4:], '+'), argv[3]))
else:
print(urban(gen_args(argv[2:], '+')))
else:
print(wiki(gen_args(argv[1:])))


if __name__ == '__main__':
main()
117 changes: 117 additions & 0 deletions python3/whatis.py.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env python

from sys import argv
import urllib2
from urllib2 import URLError
import json
import os

__author__ = 'Adrian Chmielewski-Anders'


def get(url):
return urllib2.urlopen(url).read().decode('utf-8')


def cache_def(where, what):
f = open(where, 'w')
f.write(what.encode('utf-8'))
f.close()


def read_cache(location):
f = open(location, 'r')
definition = f.read().decode(encoding='utf-8')
f.close()
return definition


def remove_tags(html):
no_tags = ''
intag = False
for i in html:
if i == '<':
intag = True
elif intag and i == '>':
intag = False
elif not intag:
no_tags += i
return no_tags


def did_you_mean(html):
index = html.find('<ul>')
if not index == -1:
return 'Did you mean: \n' + remove_tags(
html[index: html.find('</ul>')]) + '\n?'


def wiki(what):
cache = os.getenv('HOME') + '/.whatis/wiki/' + what
if os.path.exists(cache):
return read_cache(cache)
try:
html = get('http://en.wikipedia.org/wiki/' + what)
except URLError:
return urban(what)
index = html.find('<p>')

if not index == -1:
definition = remove_tags(html[index:html.find('</p>')])
if definition[len(what):] == ' may refer to:':
return did_you_mean(html)
cache_def(cache, definition)
return definition + '\nRead more at: http://en.wikipedia.org/' \
'wiki/' + what
return urban(what)


def urban(word, user=0):
user = int(user)
cache = os.getenv('HOME') + '/.whatis/urban/' + word + '_' + str(user)
if os.path.exists(cache):
return read_cache(cache)
try:
j = get(
'http://api.urbandictionary.com/v0/define?term='
+ word)
except URLError:
return 'Error, invalid URL'
definitions = json.loads(j)
if definitions['result_type'] == 'no_results':
return "There were no results found for " + word
if user >= len(definitions['list']):
user = len(definitions['list']) - 1
return_string = 'Definition:\n'
return_string += definitions['list'][user]['definition']
return_string += '\nExample:\n' + definitions['list'][user]['example']
cache_def(cache, return_string)
return return_string


def gen_args(arr, add_char='_'):
args = ''
for arg in arr:
if arg == arr[0]:
args += arg
else:
args += add_char + arg
return args


def main():
if not os.path.exists(os.getenv('HOME') + '/.whatis/wiki'):
os.makedirs(os.getenv('HOME') + '/.whatis/wiki')
if not os.path.exists(os.getenv('HOME') + '/.whatis/urban'):
os.makedirs(os.getenv('HOME') + '/.whatis/urban')
if argv[1] == '-u':
if argv[2] == '-n':
print urban(gen_args(argv[4:], '+'), argv[3])
else:
print urban(gen_args(argv[2:], '+'))
else:
print wiki(gen_args(argv[1:]))


if __name__ == '__main__':
main()
54 changes: 28 additions & 26 deletions whatis.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
#!/usr/bin/env python3
#!/usr/bin/env python

from sys import argv
from urllib.error import HTTPError
import urllib.request
import urllib.error
import urllib2
from urllib2 import URLError
import json
import os

__author__ = 'Adrian Chmielewski-Anders'


def get(url):
return urllib.request.urlopen(url).read().decode('utf-8')
return urllib2.urlopen(url).read().decode('utf-8')


def cache_def(where, what):
f = open(where, 'w')
f.write(what.encode('utf-8'))
f.close()


def read_cache(location):
f = open(location, 'r')
definition = f.read().decode(encoding='utf-8')
f.close()
return definition


def remove_tags(html):
Expand All @@ -37,41 +49,33 @@ def did_you_mean(html):
def wiki(what):
cache = os.getenv('HOME') + '/.whatis/wiki/' + what
if os.path.exists(cache):
f = open(cache, 'r', encoding='utf-8')
definition = f.read()
f.close()
return definition
return read_cache(cache)
try:
html = get('http://en.wikipedia.org/wiki/' + what)
except HTTPError:
except URLError:
return urban(what)
index = html.find('<p>')

if not index == -1:
definition = remove_tags(html[index:html.find('</p>')])
if definition[len(what):] == ' may refer to:':
return did_you_mean(html)
definition += '\nRead more at: http://en.wikipedia.org/wiki/' + what
f = open(cache, 'w', encoding='utf-8')
f.write(definition)
f.close()
return definition
cache_def(cache, definition)
return definition + '\nRead more at: http://en.wikipedia.org/' \
'wiki/' + what
return urban(what)


def urban(word, user=0):
user = int(user)
cache = os.getenv('HOME') + '/.whatis/urban/' + word + '_' + str(user)
if os.path.exists(cache):
f = open(cache, 'r', encoding='utf-8')
definition = f.read()
f.close()
return definition
return read_cache(cache)
try:
j = get(
'http://api.urbandictionary.com/v0/define?term='
+ word)
except HTTPError:
except URLError:
return 'Error, invalid URL'
definitions = json.loads(j)
if definitions['result_type'] == 'no_results':
Expand All @@ -81,9 +85,7 @@ def urban(word, user=0):
return_string = 'Definition:\n'
return_string += definitions['list'][user]['definition']
return_string += '\nExample:\n' + definitions['list'][user]['example']
f = open(cache, 'w', encoding='utf-8')
f.write(return_string)
f.close()
cache_def(cache, return_string)
return return_string


Expand All @@ -104,11 +106,11 @@ def main():
os.makedirs(os.getenv('HOME') + '/.whatis/urban')
if argv[1] == '-u':
if argv[2] == '-n':
print(urban(gen_args(argv[4:], '+'), argv[3]))
print urban(gen_args(argv[4:], '+'), argv[3])
else:
print(urban(gen_args(argv[2:], '+')))
print urban(gen_args(argv[2:], '+'))
else:
print(wiki(gen_args(argv[1:])))
print wiki(gen_args(argv[1:]))


if __name__ == '__main__':
Expand Down

0 comments on commit 593759f

Please sign in to comment.