-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding support for python3 and python2, added functions for easier ca…
…ching
- Loading branch information
Showing
5 changed files
with
290 additions
and
28 deletions.
There are no files selected for viewing
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
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
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 |
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
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() |
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
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() |
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