Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fernando Crespo
committed
May 23, 2014
1 parent
1dbe29d
commit dc7137f
Showing
5 changed files
with
81 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include README.md | ||
include requirements.txt |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
wolframalpha-cli | ||
================ | ||
# WolframAplha CLI | ||
|
||
Command Line Interface to run queries on [WolframAlpha](www.wolframaplha.com) |
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,2 @@ | ||
colorama==0.3.1 | ||
requests==2.3.0 |
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,27 @@ | ||
#coding: utf-8 | ||
import os | ||
from setuptools import setup | ||
|
||
setup( | ||
name = "wolframalpha-cli", | ||
version = "0.1", | ||
author = "Fernando Xavier de Freitas Crespo", | ||
author_email = "fernando82@gmail.com", | ||
description = ("Command Line Interface to run queries on WolframAlpha"), | ||
long_description = "".join(open('README.md').readlines()), | ||
license = "MIT", | ||
keywords = "wolframalha cli python utility", | ||
url = "https://github.com/fcrespo82/wolframalpha-cli", | ||
py_modules = ['wolframalpha'], | ||
classifiers = [ | ||
"Development Status :: 4 - Beta", | ||
"Environment :: Console", | ||
"Topic :: Utilities", | ||
"License :: OSI Approved :: MIT License", | ||
], | ||
install_requires = open('requirements.txt').readlines(), | ||
entry_points = { | ||
'console_scripts': ['wolframalpha-cli = wolframalpha:main', | ||
'wa-cli = wolframalpha: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,47 @@ | ||
#!/usr/bin/env python | ||
#coding: utf-8 | ||
|
||
import requests | ||
import sys | ||
import re | ||
import os | ||
from HTMLParser import HTMLParser | ||
from urllib2 import quote | ||
try: | ||
from colorama import init, Fore, Back, Style | ||
init() | ||
except Exception, e: | ||
class Fore(): | ||
GREEN = "** " | ||
RESET = " **" | ||
try: | ||
with open(os.path.expanduser("~/.wolfram_key"), "r") as _file: | ||
wolfram_alpha_key = "".join(_file.readlines()) | ||
except Exception, e: | ||
print("""Invalid API key! | ||
Get one at https://developer.wolframalpha.com/portal/apisignup.html""") | ||
api_key = raw_input('Enter your WolframAlpha API key: ') | ||
wolfram_alpha_key = api_key | ||
with open(os.path.expanduser("~/.wolfram_key"), "w") as _file: | ||
_file.writelines(api_key) | ||
|
||
__version__ = '0.1' | ||
|
||
def main(): | ||
if len(sys.argv) > 1: | ||
query = " ".join(sys.argv[1:]) | ||
|
||
url = u'http://api.wolframalpha.com/v2/query?input={q}&appid={API_KEY}&format=plaintext'.format(API_KEY = wolfram_alpha_key, q = quote(query)) | ||
|
||
resp = requests.get(url) | ||
|
||
for pod in re.findall(r'<pod.+?>.+?</pod>', resp.text, re.S): | ||
title = re.findall(r'<pod.+?title=[\'"](.+?)[\'"].*>', pod, re.S) | ||
parser = HTMLParser() | ||
print(Fore.GREEN + parser.unescape("".join(title).strip()) + Fore.RESET) | ||
for inner in re.findall(r'<plaintext>(.*?)</plaintext>', pod, re.S): | ||
print(parser.unescape(inner.strip())) | ||
print('') | ||
|
||
if __name__ == '__main__': | ||
main() |