Skip to content

Commit

Permalink
First release in pipy
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Crespo committed May 23, 2014
1 parent 1dbe29d commit dc7137f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,2 @@
include README.md
include requirements.txt
5 changes: 3 additions & 2 deletions README.md
@@ -1,2 +1,3 @@
wolframalpha-cli
================
# WolframAplha CLI

Command Line Interface to run queries on [WolframAlpha](www.wolframaplha.com)
2 changes: 2 additions & 0 deletions requirements.txt
@@ -0,0 +1,2 @@
colorama==0.3.1
requests==2.3.0
27 changes: 27 additions & 0 deletions setup.py
@@ -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']
},
)
47 changes: 47 additions & 0 deletions wolframalpha.py
@@ -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()

0 comments on commit dc7137f

Please sign in to comment.